blob: 78ae88005b86643d330df13f3b6a25b925483e5d [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,
1112 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 Etuaho43364892017-02-13 16:00:12 +00001119 checkBindingIsValid(line, type);
1120
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
1124 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1127 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001128 if (type.isArrayOfArrays())
1129 {
1130 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1131 identifier.c_str());
1132 return false;
1133 }
1134 else if (static_cast<int>(type.getOutermostArraySize()) ==
1135 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001137 if (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 Etuaho8a176262016-08-16 14:23:01 +03001159 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 Etuaho7c3848e2015-11-04 13:19:17 +02001873 if (variable->getConstPointer())
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 Etuaho55bde912017-10-25 13:41:13 +03001917 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 Etuaho376f1b52015-04-13 13:23:41 +03001924 if (type.isUnsizedArray())
1925 {
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();
1931 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho195be942017-12-04 23:40:14 +02001933
1934 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001935 if (!declareVariable(line, identifier, type, &variable))
1936 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Olli Etuahob0c645e2015-05-12 14:25:36 +03001940 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001941 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001942 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001943 {
1944 // Error message does not completely match behavior with ESSL 1.00, but
1945 // we want to steer developers towards only using constant expressions.
1946 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001947 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 }
1949 if (globalInitWarning)
1950 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001951 warning(
1952 line,
1953 "global variable initializers should be constant expressions "
1954 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1955 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001956 }
1957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 //
1959 // identifier must be of type constant, a global, or a temporary
1960 //
1961 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301962 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 error(line, " cannot initialize this type of qualifier ",
1965 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001966 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 }
1968 //
1969 // test for and propagate constant
1970 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Arun Patole7e7e68d2015-05-22 12:02:25 +05301972 if (qualifier == EvqConst)
1973 {
1974 if (qualifier != initializer->getType().getQualifier())
1975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001976 std::stringstream reasonStream;
1977 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1978 << "'";
1979 std::string reason = reasonStream.str();
1980 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001981 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001983 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (type != initializer->getType())
1985 {
1986 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001987 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001988 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001989 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001990 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001991
1992 // Save the constant folded value to the variable if possible. For example array
1993 // initializers are not folded, since that way copying the array literal to multiple places
1994 // in the shader is avoided.
1995 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1996 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301997 if (initializer->getAsConstantUnion())
1998 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001999 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002000 ASSERT(*initNode == nullptr);
2001 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302002 }
2003 else if (initializer->getAsSymbolNode())
2004 {
Olli Etuahob6af22b2017-12-15 14:05:44 +02002005 const TVariable &var = initializer->getAsSymbolNode()->variable();
2006 const TConstantUnion *constArray = var.getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002007 if (constArray)
2008 {
2009 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002010 ASSERT(*initNode == nullptr);
2011 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002012 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002013 }
2014 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002015
Olli Etuaho195be942017-12-04 23:40:14 +02002016 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002017 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002018 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2019 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002020 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002021 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024
Olli Etuaho914b79a2017-06-19 16:03:19 +03002025 return true;
2026}
2027
2028TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2029 const TString &identifier,
2030 TIntermTyped *initializer,
2031 const TSourceLoc &loc)
2032{
2033 checkIsScalarBool(loc, pType);
2034 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002035 TType type(pType);
2036 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002037 {
2038 // The initializer is valid. The init condition needs to have a node - either the
2039 // initializer node, or a constant node in case the initialized variable is const and won't
2040 // be recorded in the AST.
2041 if (initNode == nullptr)
2042 {
2043 return initializer;
2044 }
2045 else
2046 {
2047 TIntermDeclaration *declaration = new TIntermDeclaration();
2048 declaration->appendDeclarator(initNode);
2049 return declaration;
2050 }
2051 }
2052 return nullptr;
2053}
2054
2055TIntermNode *TParseContext::addLoop(TLoopType type,
2056 TIntermNode *init,
2057 TIntermNode *cond,
2058 TIntermTyped *expr,
2059 TIntermNode *body,
2060 const TSourceLoc &line)
2061{
2062 TIntermNode *node = nullptr;
2063 TIntermTyped *typedCond = nullptr;
2064 if (cond)
2065 {
2066 typedCond = cond->getAsTyped();
2067 }
2068 if (cond == nullptr || typedCond)
2069 {
Olli Etuahocce89652017-06-19 16:04:09 +03002070 if (type == ELoopDoWhile)
2071 {
2072 checkIsScalarBool(line, typedCond);
2073 }
2074 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2075 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2076 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2077 !typedCond->isVector()));
2078
Olli Etuaho3ec75682017-07-05 17:02:55 +03002079 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002080 node->setLine(line);
2081 return node;
2082 }
2083
Olli Etuahocce89652017-06-19 16:04:09 +03002084 ASSERT(type != ELoopDoWhile);
2085
Olli Etuaho914b79a2017-06-19 16:03:19 +03002086 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2087 ASSERT(declaration);
2088 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2089 ASSERT(declarator->getLeft()->getAsSymbolNode());
2090
2091 // The condition is a declaration. In the AST representation we don't support declarations as
2092 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2093 // the loop.
2094 TIntermBlock *block = new TIntermBlock();
2095
2096 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2097 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2098 block->appendStatement(declareCondition);
2099
2100 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2101 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002102 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002103 block->appendStatement(loop);
2104 loop->setLine(line);
2105 block->setLine(line);
2106 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107}
2108
Olli Etuahocce89652017-06-19 16:04:09 +03002109TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2110 TIntermNodePair code,
2111 const TSourceLoc &loc)
2112{
Olli Etuaho56229f12017-07-10 14:16:33 +03002113 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002114
2115 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002116 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002117 {
2118 if (cond->getAsConstantUnion()->getBConst(0) == true)
2119 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002120 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002121 }
2122 else
2123 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002124 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002125 }
2126 }
2127
Olli Etuaho3ec75682017-07-05 17:02:55 +03002128 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002129 node->setLine(loc);
2130
2131 return node;
2132}
2133
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002134void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2135{
2136 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2137 typeSpecifier->getBasicType());
2138
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002139 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002140 {
2141 error(typeSpecifier->getLine(), "not supported", "first-class array");
2142 typeSpecifier->clearArrayness();
2143 }
2144}
2145
Martin Radev70866b82016-07-22 15:27:42 +03002146TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302147 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002148{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002149 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002150
Martin Radev70866b82016-07-22 15:27:42 +03002151 TPublicType returnType = typeSpecifier;
2152 returnType.qualifier = typeQualifier.qualifier;
2153 returnType.invariant = typeQualifier.invariant;
2154 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002155 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002156 returnType.precision = typeSpecifier.precision;
2157
2158 if (typeQualifier.precision != EbpUndefined)
2159 {
2160 returnType.precision = typeQualifier.precision;
2161 }
2162
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2164 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002165
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2167 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002168
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002170
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002171 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002172 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002173 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002174 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002175 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002176 returnType.clearArrayness();
2177 }
2178
Martin Radev70866b82016-07-22 15:27:42 +03002179 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002181 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002183 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002184 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002185
Martin Radev70866b82016-07-22 15:27:42 +03002186 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002187 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002188 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002189 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002190 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002191 }
2192 }
2193 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002194 {
Martin Radev70866b82016-07-22 15:27:42 +03002195 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002196 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002197 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002198 }
Martin Radev70866b82016-07-22 15:27:42 +03002199 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2200 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002201 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002202 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2203 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002204 }
Martin Radev70866b82016-07-22 15:27:42 +03002205 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002206 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002207 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002208 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002209 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002210 }
2211
2212 return returnType;
2213}
2214
Olli Etuaho856c4972016-08-08 11:38:39 +03002215void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2216 const TPublicType &type,
2217 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002218{
2219 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002220 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002221 {
2222 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002223 }
2224
2225 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2226 switch (qualifier)
2227 {
2228 case EvqVertexIn:
2229 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002230 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002231 {
2232 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002234 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 return;
2236 case EvqFragmentOut:
2237 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002238 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002239 {
2240 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002242 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002243 return;
2244 default:
2245 break;
2246 }
2247
2248 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2249 // restrictions.
2250 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002251 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2252 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2254 {
2255 error(qualifierLocation, "must use 'flat' interpolation here",
2256 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 }
2258
Martin Radev4a9cd802016-09-01 16:51:51 +03002259 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002260 {
2261 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2262 // These restrictions are only implied by the ESSL 3.00 spec, but
2263 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002264 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 {
2266 error(qualifierLocation, "cannot be an array of structures",
2267 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002268 }
2269 if (type.isStructureContainingArrays())
2270 {
2271 error(qualifierLocation, "cannot be a structure containing an array",
2272 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002273 }
2274 if (type.isStructureContainingType(EbtStruct))
2275 {
2276 error(qualifierLocation, "cannot be a structure containing a structure",
2277 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002278 }
2279 if (type.isStructureContainingType(EbtBool))
2280 {
2281 error(qualifierLocation, "cannot be a structure containing a bool",
2282 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002283 }
2284 }
2285}
2286
Martin Radev2cc85b32016-08-05 16:22:53 +03002287void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2288{
2289 if (qualifier.getType() == QtStorage)
2290 {
2291 const TStorageQualifierWrapper &storageQualifier =
2292 static_cast<const TStorageQualifierWrapper &>(qualifier);
2293 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2294 !symbolTable.atGlobalLevel())
2295 {
2296 error(storageQualifier.getLine(),
2297 "Local variables can only use the const storage qualifier.",
2298 storageQualifier.getQualifierString().c_str());
2299 }
2300 }
2301}
2302
Olli Etuaho43364892017-02-13 16:00:12 +00002303void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002304 const TSourceLoc &location)
2305{
Jiajia Qinbc585152017-06-23 15:42:17 +08002306 const std::string reason(
2307 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2308 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002309 if (memoryQualifier.readonly)
2310 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002311 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002312 }
2313 if (memoryQualifier.writeonly)
2314 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002315 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002316 }
Martin Radev049edfa2016-11-11 14:35:37 +02002317 if (memoryQualifier.coherent)
2318 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002319 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002320 }
2321 if (memoryQualifier.restrictQualifier)
2322 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002323 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002324 }
2325 if (memoryQualifier.volatileQualifier)
2326 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002327 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002328 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002329}
2330
jchen104cdac9e2017-05-08 11:01:20 +08002331// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2332// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002333void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2334 const TSourceLoc &loc,
2335 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002336{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002337 if (!IsAtomicCounter(type->getBasicType()))
2338 {
2339 return;
2340 }
2341
2342 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2343 : kAtomicCounterSize;
2344 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2345 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002346 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002347 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002348 {
2349 offset = bindingState.appendSpan(size);
2350 }
2351 else
2352 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002353 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002354 }
2355 if (offset == -1)
2356 {
2357 error(loc, "Offset overlapping", "atomic counter");
2358 return;
2359 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002360 layoutQualifier.offset = offset;
2361 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002362}
2363
Olli Etuaho454c34c2017-10-25 16:35:56 +03002364void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2365 const char *token,
2366 TType *type)
2367{
2368 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2369 {
2370 if (type->isArray() && type->getOutermostArraySize() == 0u)
2371 {
2372 // Set size for the unsized geometry shader inputs if they are declared after a valid
2373 // input primitive declaration.
2374 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2375 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002376 ASSERT(mGlInVariableWithArraySize != nullptr);
2377 type->sizeOutermostUnsizedArray(
2378 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002379 }
2380 else
2381 {
2382 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2383 // An input can be declared without an array size if there is a previous layout
2384 // which specifies the size.
2385 error(location,
2386 "Missing a valid input primitive declaration before declaring an unsized "
2387 "array input",
2388 token);
2389 }
2390 }
2391 else if (type->isArray())
2392 {
2393 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2394 }
2395 else
2396 {
2397 error(location, "Geometry shader input variable must be declared as an array", token);
2398 }
2399 }
2400}
2401
Olli Etuaho13389b62016-10-16 11:48:18 +01002402TIntermDeclaration *TParseContext::parseSingleDeclaration(
2403 TPublicType &publicType,
2404 const TSourceLoc &identifierOrTypeLocation,
2405 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002406{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002407 TType type(publicType);
2408 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2409 mDirectiveHandler.pragma().stdgl.invariantAll)
2410 {
2411 TQualifier qualifier = type.getQualifier();
2412
2413 // The directive handler has already taken care of rejecting invalid uses of this pragma
2414 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2415 // affected variable declarations:
2416 //
2417 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2418 // elsewhere, in TranslatorGLSL.)
2419 //
2420 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2421 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2422 // the way this is currently implemented we have to enable this compiler option before
2423 // parsing the shader and determining the shading language version it uses. If this were
2424 // implemented as a post-pass, the workaround could be more targeted.
2425 //
2426 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2427 // the specification, but there are desktop OpenGL drivers that expect that this is the
2428 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2429 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2430 {
2431 type.setInvariant(true);
2432 }
2433 }
2434
Olli Etuaho454c34c2017-10-25 16:35:56 +03002435 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002436
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002437 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2438 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002439
Olli Etuahobab4c082015-04-24 16:38:49 +03002440 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002441 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002442
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002444 if (emptyDeclaration)
2445 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002446 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 // In most cases we don't need to create a symbol node for an empty declaration.
2448 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2449 if (type.getBasicType() == EbtStruct)
2450 {
Olli Etuaho195be942017-12-04 23:40:14 +02002451 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002452 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002453 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002454 }
jchen104cdac9e2017-05-08 11:01:20 +08002455 else if (IsAtomicCounter(publicType.getBasicType()))
2456 {
2457 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2458 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002459 }
2460 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002461 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002462 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuaho55bde912017-10-25 13:41:13 +03002464 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002465
Olli Etuaho55bc9052017-10-25 17:33:06 +03002466 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002467
Olli Etuaho2935c582015-04-08 14:32:06 +03002468 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002469 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002470 {
Olli Etuaho195be942017-12-04 23:40:14 +02002471 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002472 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002473 }
2474
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 TIntermDeclaration *declaration = new TIntermDeclaration();
2476 declaration->setLine(identifierOrTypeLocation);
2477 if (symbol)
2478 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002479 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002480 declaration->appendDeclarator(symbol);
2481 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002482 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002483}
2484
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002485TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2486 TPublicType &elementType,
2487 const TSourceLoc &identifierLocation,
2488 const TString &identifier,
2489 const TSourceLoc &indexLocation,
2490 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002491{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002492 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002493
Olli Etuaho55bde912017-10-25 13:41:13 +03002494 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002495 identifierLocation);
2496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuaho55bde912017-10-25 13:41:13 +03002499 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002500
Olli Etuaho55bde912017-10-25 13:41:13 +03002501 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002502 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002503
Olli Etuaho454c34c2017-10-25 16:35:56 +03002504 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002505
2506 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2507
Olli Etuaho55bc9052017-10-25 17:33:06 +03002508 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510 TIntermDeclaration *declaration = new TIntermDeclaration();
2511 declaration->setLine(identifierLocation);
2512
Olli Etuaho195be942017-12-04 23:40:14 +02002513 TVariable *variable = nullptr;
2514 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 {
Olli Etuaho195be942017-12-04 23:40:14 +02002516 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002517 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 declaration->appendDeclarator(symbol);
2519 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002520
Olli Etuaho13389b62016-10-16 11:48:18 +01002521 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002522}
2523
Olli Etuaho13389b62016-10-16 11:48:18 +01002524TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2525 const TSourceLoc &identifierLocation,
2526 const TString &identifier,
2527 const TSourceLoc &initLocation,
2528 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002529{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002530 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002531
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002532 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2533 identifierLocation);
2534
2535 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002536
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 TIntermDeclaration *declaration = new TIntermDeclaration();
2538 declaration->setLine(identifierLocation);
2539
2540 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002541 TType type(publicType);
2542 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002543 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002544 if (initNode)
2545 {
2546 declaration->appendDeclarator(initNode);
2547 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002548 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002550}
2551
Olli Etuaho13389b62016-10-16 11:48:18 +01002552TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002553 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002554 const TSourceLoc &identifierLocation,
2555 const TString &identifier,
2556 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002557 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002558 const TSourceLoc &initLocation,
2559 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002561 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002562
Olli Etuaho55bde912017-10-25 13:41:13 +03002563 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 identifierLocation);
2565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuaho55bde912017-10-25 13:41:13 +03002568 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569
Olli Etuaho55bde912017-10-25 13:41:13 +03002570 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002571 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 TIntermDeclaration *declaration = new TIntermDeclaration();
2574 declaration->setLine(identifierLocation);
2575
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002576 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002577 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002578 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002579 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002580 if (initNode)
2581 {
2582 declaration->appendDeclarator(initNode);
2583 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002584 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002585
2586 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002587}
2588
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002589TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002590 const TTypeQualifierBuilder &typeQualifierBuilder,
2591 const TSourceLoc &identifierLoc,
2592 const TString *identifier,
2593 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002594{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002595 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002596
Martin Radev70866b82016-07-22 15:27:42 +03002597 if (!typeQualifier.invariant)
2598 {
2599 error(identifierLoc, "Expected invariant", identifier->c_str());
2600 return nullptr;
2601 }
2602 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2603 {
2604 return nullptr;
2605 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606 if (!symbol)
2607 {
2608 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002609 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002610 }
Martin Radev70866b82016-07-22 15:27:42 +03002611 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002612 {
Martin Radev70866b82016-07-22 15:27:42 +03002613 error(identifierLoc, "invariant declaration specifies qualifier",
2614 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002615 }
Martin Radev70866b82016-07-22 15:27:42 +03002616 if (typeQualifier.precision != EbpUndefined)
2617 {
2618 error(identifierLoc, "invariant declaration specifies precision",
2619 getPrecisionString(typeQualifier.precision));
2620 }
2621 if (!typeQualifier.layoutQualifier.isEmpty())
2622 {
2623 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2624 }
2625
2626 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002627 if (!variable)
2628 {
2629 return nullptr;
2630 }
Martin Radev70866b82016-07-22 15:27:42 +03002631 const TType &type = variable->getType();
2632
2633 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2634 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002635 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002636
2637 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2638
Olli Etuaho195be942017-12-04 23:40:14 +02002639 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002640 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002641
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002642 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002643}
2644
Olli Etuaho13389b62016-10-16 11:48:18 +01002645void TParseContext::parseDeclarator(TPublicType &publicType,
2646 const TSourceLoc &identifierLocation,
2647 const TString &identifier,
2648 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002649{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002650 // If the declaration starting this declarator list was empty (example: int,), some checks were
2651 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002652 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002653 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002654 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2655 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002656 }
2657
Olli Etuaho856c4972016-08-08 11:38:39 +03002658 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002659
Olli Etuaho43364892017-02-13 16:00:12 +00002660 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002661
2662 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2663
Olli Etuaho55bde912017-10-25 13:41:13 +03002664 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2665
Olli Etuaho55bc9052017-10-25 17:33:06 +03002666 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2667
Olli Etuaho195be942017-12-04 23:40:14 +02002668 TVariable *variable = nullptr;
2669 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002670 {
Olli Etuaho195be942017-12-04 23:40:14 +02002671 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002672 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002673 declarationOut->appendDeclarator(symbol);
2674 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002675}
2676
Olli Etuaho55bde912017-10-25 13:41:13 +03002677void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 const TSourceLoc &identifierLocation,
2679 const TString &identifier,
2680 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002681 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002682 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002683{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002684 // If the declaration starting this declarator list was empty (example: int,), some checks were
2685 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002686 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002687 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002688 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002689 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002690 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002691
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002693
Olli Etuaho55bde912017-10-25 13:41:13 +03002694 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002695 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002696 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002697 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002698
Olli Etuaho454c34c2017-10-25 16:35:56 +03002699 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2700
Olli Etuaho55bde912017-10-25 13:41:13 +03002701 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2702
Olli Etuaho55bc9052017-10-25 17:33:06 +03002703 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002704
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002705 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002706 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002707 {
Olli Etuaho195be942017-12-04 23:40:14 +02002708 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002709 symbol->setLine(identifierLocation);
2710 declarationOut->appendDeclarator(symbol);
2711 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002712 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002713}
2714
Olli Etuaho13389b62016-10-16 11:48:18 +01002715void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2716 const TSourceLoc &identifierLocation,
2717 const TString &identifier,
2718 const TSourceLoc &initLocation,
2719 TIntermTyped *initializer,
2720 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002721{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002722 // If the declaration starting this declarator list was empty (example: int,), some checks were
2723 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002724 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002725 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002726 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2727 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002728 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002729
Olli Etuaho856c4972016-08-08 11:38:39 +03002730 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002731
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002733 TType type(publicType);
2734 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002735 {
2736 //
2737 // build the intermediate representation
2738 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002739 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002740 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002741 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002742 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002743 }
2744}
2745
Olli Etuaho55bde912017-10-25 13:41:13 +03002746void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 const TSourceLoc &identifierLocation,
2748 const TString &identifier,
2749 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002750 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002751 const TSourceLoc &initLocation,
2752 TIntermTyped *initializer,
2753 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002755 // If the declaration starting this declarator list was empty (example: int,), some checks were
2756 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002757 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002758 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002759 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002760 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002761 }
2762
Olli Etuaho55bde912017-10-25 13:41:13 +03002763 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764
Olli Etuaho55bde912017-10-25 13:41:13 +03002765 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766
Olli Etuaho55bde912017-10-25 13:41:13 +03002767 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002768 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769
2770 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002771 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002772 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773 {
2774 if (initNode)
2775 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002776 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002777 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778 }
2779}
2780
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002781TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2782{
2783 // It's simpler to parse an empty statement as a constant expression rather than having a
2784 // different type of node just for empty statements, that will be pruned from the AST anyway.
2785 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2786 node->setLine(location);
2787 return node;
2788}
2789
jchen104cdac9e2017-05-08 11:01:20 +08002790void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2791 const TSourceLoc &location)
2792{
2793 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2794 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2795 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2796 {
2797 error(location, "Requires both binding and offset", "layout");
2798 return;
2799 }
2800 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2801}
2802
Olli Etuahocce89652017-06-19 16:04:09 +03002803void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2804 const TPublicType &type,
2805 const TSourceLoc &loc)
2806{
2807 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2808 !getFragmentPrecisionHigh())
2809 {
2810 error(loc, "precision is not supported in fragment shader", "highp");
2811 }
2812
2813 if (!CanSetDefaultPrecisionOnType(type))
2814 {
2815 error(loc, "illegal type argument for default precision qualifier",
2816 getBasicString(type.getBasicType()));
2817 return;
2818 }
2819 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2820}
2821
Shaob5cc1192017-07-06 10:47:20 +08002822bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2823{
2824 switch (typeQualifier.layoutQualifier.primitiveType)
2825 {
2826 case EptLines:
2827 case EptLinesAdjacency:
2828 case EptTriangles:
2829 case EptTrianglesAdjacency:
2830 return typeQualifier.qualifier == EvqGeometryIn;
2831
2832 case EptLineStrip:
2833 case EptTriangleStrip:
2834 return typeQualifier.qualifier == EvqGeometryOut;
2835
2836 case EptPoints:
2837 return true;
2838
2839 default:
2840 UNREACHABLE();
2841 return false;
2842 }
2843}
2844
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002845void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2846 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002847{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002848 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002849 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002850 TSymbol *glPerVertex = symbolTable.findBuiltIn("gl_PerVertex", 310);
2851 TInterfaceBlock *glPerVertexBlock = static_cast<TInterfaceBlock *>(glPerVertex);
2852 TType glInType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2853 glInType.makeArray(inputArraySize);
2854 mGlInVariableWithArraySize =
2855 new TVariable(&symbolTable, NewPoolTString("gl_in"), glInType, SymbolType::BuiltIn,
2856 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002857 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002858 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002859 {
2860 error(line,
2861 "Array size or input primitive declaration doesn't match the size of earlier sized "
2862 "array inputs.",
2863 "layout");
2864 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002865}
2866
Shaob5cc1192017-07-06 10:47:20 +08002867bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2868{
2869 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2870
2871 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2872
2873 if (layoutQualifier.maxVertices != -1)
2874 {
2875 error(typeQualifier.line,
2876 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2877 return false;
2878 }
2879
2880 // Set mGeometryInputPrimitiveType if exists
2881 if (layoutQualifier.primitiveType != EptUndefined)
2882 {
2883 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2884 {
2885 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2886 return false;
2887 }
2888
2889 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2890 {
2891 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002892 setGeometryShaderInputArraySize(
2893 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2894 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002895 }
2896 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2897 {
2898 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2899 "layout");
2900 return false;
2901 }
2902 }
2903
2904 // Set mGeometryInvocations if exists
2905 if (layoutQualifier.invocations > 0)
2906 {
2907 if (mGeometryShaderInvocations == 0)
2908 {
2909 mGeometryShaderInvocations = layoutQualifier.invocations;
2910 }
2911 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2912 {
2913 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2914 "layout");
2915 return false;
2916 }
2917 }
2918
2919 return true;
2920}
2921
2922bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2923{
2924 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2925
2926 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2927
2928 if (layoutQualifier.invocations > 0)
2929 {
2930 error(typeQualifier.line,
2931 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2932 return false;
2933 }
2934
2935 // Set mGeometryOutputPrimitiveType if exists
2936 if (layoutQualifier.primitiveType != EptUndefined)
2937 {
2938 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2939 {
2940 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2941 return false;
2942 }
2943
2944 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2945 {
2946 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2947 }
2948 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2949 {
2950 error(typeQualifier.line,
2951 "primitive doesn't match earlier output primitive declaration", "layout");
2952 return false;
2953 }
2954 }
2955
2956 // Set mGeometryMaxVertices if exists
2957 if (layoutQualifier.maxVertices > -1)
2958 {
2959 if (mGeometryShaderMaxVertices == -1)
2960 {
2961 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2962 }
2963 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2964 {
2965 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2966 "layout");
2967 return false;
2968 }
2969 }
2970
2971 return true;
2972}
2973
Martin Radev70866b82016-07-22 15:27:42 +03002974void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002975{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002976 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002977 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002978
Martin Radev70866b82016-07-22 15:27:42 +03002979 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2980 typeQualifier.line);
2981
Jamie Madillc2128ff2016-07-04 10:26:17 -04002982 // It should never be the case, but some strange parser errors can send us here.
2983 if (layoutQualifier.isEmpty())
2984 {
2985 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002986 return;
2987 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002988
Martin Radev802abe02016-08-04 17:48:32 +03002989 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002990 {
Olli Etuaho43364892017-02-13 16:00:12 +00002991 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002992 return;
2993 }
2994
Olli Etuaho43364892017-02-13 16:00:12 +00002995 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2996
2997 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002998
2999 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3000
Andrei Volykhina5527072017-03-22 16:46:30 +03003001 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3002
jchen104cdac9e2017-05-08 11:01:20 +08003003 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3004
Qin Jiajiaca68d982017-09-18 16:41:56 +08003005 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3006 typeQualifier.qualifier);
3007
Martin Radev802abe02016-08-04 17:48:32 +03003008 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003009 {
Martin Radev802abe02016-08-04 17:48:32 +03003010 if (mComputeShaderLocalSizeDeclared &&
3011 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3012 {
3013 error(typeQualifier.line, "Work group size does not match the previous declaration",
3014 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003015 return;
3016 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003017
Martin Radev802abe02016-08-04 17:48:32 +03003018 if (mShaderVersion < 310)
3019 {
3020 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003021 return;
3022 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003023
Martin Radev4c4c8e72016-08-04 12:25:34 +03003024 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003025 {
3026 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003027 return;
3028 }
3029
3030 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3031 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3032
3033 const TConstantUnion *maxComputeWorkGroupSizeData =
3034 maxComputeWorkGroupSize->getConstPointer();
3035
3036 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3037 {
3038 if (layoutQualifier.localSize[i] != -1)
3039 {
3040 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3041 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3042 if (mComputeShaderLocalSize[i] < 1 ||
3043 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3044 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003045 std::stringstream reasonStream;
3046 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3047 << maxComputeWorkGroupSizeValue;
3048 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003049
Olli Etuaho4de340a2016-12-16 09:32:03 +00003050 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003051 return;
3052 }
3053 }
3054 }
3055
3056 mComputeShaderLocalSizeDeclared = true;
3057 }
Shaob5cc1192017-07-06 10:47:20 +08003058 else if (typeQualifier.qualifier == EvqGeometryIn)
3059 {
3060 if (mShaderVersion < 310)
3061 {
3062 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3063 return;
3064 }
3065
3066 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3067 {
3068 return;
3069 }
3070 }
3071 else if (typeQualifier.qualifier == EvqGeometryOut)
3072 {
3073 if (mShaderVersion < 310)
3074 {
3075 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3076 "layout");
3077 return;
3078 }
3079
3080 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3081 {
3082 return;
3083 }
3084 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003085 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3086 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003087 {
3088 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3089 // specification.
3090 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3091 {
3092 error(typeQualifier.line, "Number of views does not match the previous declaration",
3093 "layout");
3094 return;
3095 }
3096
3097 if (layoutQualifier.numViews == -1)
3098 {
3099 error(typeQualifier.line, "No num_views specified", "layout");
3100 return;
3101 }
3102
3103 if (layoutQualifier.numViews > mMaxNumViews)
3104 {
3105 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3106 "layout");
3107 return;
3108 }
3109
3110 mNumViews = layoutQualifier.numViews;
3111 }
Martin Radev802abe02016-08-04 17:48:32 +03003112 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003113 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003114 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003115 {
Martin Radev802abe02016-08-04 17:48:32 +03003116 return;
3117 }
3118
Jiajia Qinbc585152017-06-23 15:42:17 +08003119 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003120 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003121 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003122 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003123 return;
3124 }
3125
3126 if (mShaderVersion < 300)
3127 {
3128 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3129 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003130 return;
3131 }
3132
Olli Etuaho09b04a22016-12-15 13:30:26 +00003133 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003134
3135 if (layoutQualifier.matrixPacking != EmpUnspecified)
3136 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003137 if (typeQualifier.qualifier == EvqUniform)
3138 {
3139 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3140 }
3141 else if (typeQualifier.qualifier == EvqBuffer)
3142 {
3143 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3144 }
Martin Radev802abe02016-08-04 17:48:32 +03003145 }
3146
3147 if (layoutQualifier.blockStorage != EbsUnspecified)
3148 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003149 if (typeQualifier.qualifier == EvqUniform)
3150 {
3151 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3152 }
3153 else if (typeQualifier.qualifier == EvqBuffer)
3154 {
3155 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3156 }
Martin Radev802abe02016-08-04 17:48:32 +03003157 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003158 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003159}
3160
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003161TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3162 const TFunction &function,
3163 const TSourceLoc &location,
3164 bool insertParametersToSymbolTable)
3165{
Olli Etuahobed35d72017-12-20 16:36:26 +02003166 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003167
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003168 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 prototype->setLine(location);
3170
3171 for (size_t i = 0; i < function.getParamCount(); i++)
3172 {
3173 const TConstParameter &param = function.getParam(i);
3174
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003175 TIntermSymbol *symbol = nullptr;
3176
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003177 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3178 // be used for unused args).
3179 if (param.name != nullptr)
3180 {
Olli Etuaho195be942017-12-04 23:40:14 +02003181 TVariable *variable =
3182 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3183 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003184 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003185 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 {
Olli Etuaho195be942017-12-04 23:40:14 +02003187 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003188 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003189 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003190 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003192 // Unsized type of a named parameter should have already been checked and sanitized.
3193 ASSERT(!param.type->isUnsizedArray());
3194 }
3195 else
3196 {
3197 if (param.type->isUnsizedArray())
3198 {
3199 error(location, "function parameter array must be sized at compile time", "[]");
3200 // We don't need to size the arrays since the parameter is unnamed and hence
3201 // inaccessible.
3202 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003203 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003204 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3207 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003208 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003209 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003210 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003211 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003212 symbol->setLine(location);
3213 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003214 }
3215 return prototype;
3216}
3217
Olli Etuaho16c745a2017-01-16 17:02:27 +00003218TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3219 const TFunction &parsedFunction,
3220 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003221{
Olli Etuaho476197f2016-10-11 13:59:08 +01003222 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3223 // first declaration. Either way the instance in the symbol table is used to track whether the
3224 // function is declared multiple times.
3225 TFunction *function = static_cast<TFunction *>(
3226 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3227 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003228 {
3229 // ESSL 1.00.17 section 4.2.7.
3230 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3231 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003232 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003233 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003234
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003235 TIntermFunctionPrototype *prototype =
3236 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003237
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003238 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003239
3240 if (!symbolTable.atGlobalLevel())
3241 {
3242 // ESSL 3.00.4 section 4.2.4.
3243 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003244 }
3245
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 return prototype;
3247}
3248
Olli Etuaho336b1472016-10-05 16:37:55 +01003249TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003250 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003251 TIntermBlock *functionBody,
3252 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003254 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003255 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3256 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003258 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259 }
3260
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261 if (functionBody == nullptr)
3262 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003263 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003264 functionBody->setLine(location);
3265 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003266 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003267 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003268 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003269
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003270 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003271 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003272}
3273
Olli Etuaho476197f2016-10-11 13:59:08 +01003274void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3275 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003276 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003277{
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 ASSERT(function);
3279 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003280 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003281 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003282
3283 if (builtIn)
3284 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003285 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003286 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003287 else
Jamie Madill185fb402015-06-12 15:48:48 -04003288 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 TFunction *prevDec = static_cast<TFunction *>(
3290 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3291
3292 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3293 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3294 // occurance.
3295 if (*function != prevDec)
3296 {
3297 // Swap the parameters of the previous declaration to the parameters of the function
3298 // definition (parameter names may differ).
3299 prevDec->swapParameters(**function);
3300
3301 // The function definition will share the same symbol as any previous declaration.
3302 *function = prevDec;
3303 }
3304
3305 if ((*function)->isDefined())
3306 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003307 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003308 }
3309
3310 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003311 }
Jamie Madill185fb402015-06-12 15:48:48 -04003312
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003313 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003314 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003315 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003316
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003317 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003318 setLoopNestingLevel(0);
3319}
3320
Jamie Madillb98c3a82015-07-23 14:26:04 -04003321TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003322{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003323 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003324 // We don't know at this point whether this is a function definition or a prototype.
3325 // The definition production code will check for redefinitions.
3326 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003327 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003328 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3329 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003330 //
3331 TFunction *prevDec =
3332 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303333
Olli Etuahod80f2942017-11-06 12:44:45 +02003334 for (size_t i = 0u; i < function->getParamCount(); ++i)
3335 {
3336 auto &param = function->getParam(i);
3337 if (param.type->isStructSpecifier())
3338 {
3339 // ESSL 3.00.6 section 12.10.
3340 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003341 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 }
3343 }
3344
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003345 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003346 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303347 {
Martin Radevda6254b2016-12-14 17:00:36 +02003348 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303349 // Therefore overloading or redefining builtin functions is an error.
3350 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003351 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303352 }
3353 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003354 {
3355 if (prevDec->getReturnType() != function->getReturnType())
3356 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003357 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003358 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003359 }
3360 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003362 if (prevDec->getParam(i).type->getQualifier() !=
3363 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location,
3366 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003367 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003368 }
3369 }
3370 }
3371
3372 //
3373 // Check for previously declared variables using the same name.
3374 //
Olli Etuahobed35d72017-12-20 16:36:26 +02003375 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 if (prevSym)
3377 {
3378 if (!prevSym->isFunction())
3379 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003380 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
3382 }
3383 else
3384 {
3385 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003386 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003387 }
3388
3389 // We're at the inner scope level of the function's arguments and body statement.
3390 // Add the function prototype to the surrounding scope instead.
3391 symbolTable.getOuterLevel()->insert(function);
3392
Olli Etuaho78d13742017-01-18 13:06:10 +00003393 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003394 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003395 {
3396 if (function->getParamCount() > 0)
3397 {
3398 error(location, "function cannot take any parameter(s)", "main");
3399 }
3400 if (function->getReturnType().getBasicType() != EbtVoid)
3401 {
3402 error(location, "main function cannot return a value",
3403 function->getReturnType().getBasicString());
3404 }
3405 }
3406
Jamie Madill185fb402015-06-12 15:48:48 -04003407 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003408 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3409 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003410 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3411 //
3412 return function;
3413}
3414
Olli Etuaho9de84a52016-06-14 17:36:01 +03003415TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3416 const TString *name,
3417 const TSourceLoc &location)
3418{
3419 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3420 {
3421 error(location, "no qualifiers allowed for function return",
3422 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423 }
3424 if (!type.layoutQualifier.isEmpty())
3425 {
3426 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003427 }
jchen10cc2a10e2017-05-03 14:05:12 +08003428 // make sure an opaque type is not involved as well...
3429 std::string reason(getBasicString(type.getBasicType()));
3430 reason += "s can't be function return values";
3431 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003432 if (mShaderVersion < 300)
3433 {
3434 // Array return values are forbidden, but there's also no valid syntax for declaring array
3435 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003436 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003437
3438 if (type.isStructureContainingArrays())
3439 {
3440 // ESSL 1.00.17 section 6.1 Function Definitions
3441 error(location, "structures containing arrays can't be function return values",
3442 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003443 }
3444 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003445
3446 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003447 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003448}
3449
Olli Etuahocce89652017-06-19 16:04:09 +03003450TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3451{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003452 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003453 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3454 // really only interested in the mangled name of the function to look up the actual function
3455 // from the symbol table. If we just had the name string and the types of the parameters that
3456 // would be enough, but TFunction carries a lot of extra information in addition to that.
3457 // Besides function calls we do have to store constructor calls in the same data structure, for
3458 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003459 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003460}
3461
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003462TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003463{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003464 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003465 {
3466 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3467 "[]");
3468 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003469 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003470 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003471 error(publicType.getLine(), "constructor can't be a structure definition",
3472 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003473 }
3474
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003475 TType *type = new TType(publicType);
3476 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003477 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003478 error(publicType.getLine(), "cannot construct this type",
3479 getBasicString(publicType.getBasicType()));
3480 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003481 }
3482
Olli Etuaho0c371002017-12-13 17:00:25 +04003483 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003484}
3485
Olli Etuaho55bde912017-10-25 13:41:13 +03003486void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3487 const char *errorMessage,
3488 const char *token,
3489 TType *arrayType)
3490{
3491 if (arrayType->isUnsizedArray())
3492 {
3493 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003494 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003495 }
3496}
3497
3498TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003499 const TString *name,
3500 const TSourceLoc &nameLoc)
3501{
Olli Etuaho55bde912017-10-25 13:41:13 +03003502 ASSERT(type);
3503 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3504 type);
3505 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003506 {
3507 error(nameLoc, "illegal use of type 'void'", name->c_str());
3508 }
3509 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003510 TParameter param = {name, type};
3511 return param;
3512}
3513
Olli Etuaho55bde912017-10-25 13:41:13 +03003514TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3515 const TString *name,
3516 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003517{
Olli Etuaho55bde912017-10-25 13:41:13 +03003518 TType *type = new TType(publicType);
3519 return parseParameterDeclarator(type, name, nameLoc);
3520}
3521
3522TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3523 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003524 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003525 const TSourceLoc &arrayLoc,
3526 TPublicType *elementType)
3527{
3528 checkArrayElementIsNotArray(arrayLoc, *elementType);
3529 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003530 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003531 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003532}
3533
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003534bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3535 TType type,
3536 const TSourceLoc &line)
3537{
3538 if (arguments->empty())
3539 {
3540 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3541 return false;
3542 }
3543 for (TIntermNode *arg : *arguments)
3544 {
3545 TIntermTyped *element = arg->getAsTyped();
3546 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003547 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3548 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003549 {
3550 error(line, "constructing from a non-dereferenced array", "constructor");
3551 return false;
3552 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003553 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 {
3555 if (dimensionalityFromElement == 1u)
3556 {
3557 error(line, "implicitly sized array of arrays constructor argument is not an array",
3558 "constructor");
3559 }
3560 else
3561 {
3562 error(line,
3563 "implicitly sized array of arrays constructor argument dimensionality is too "
3564 "low",
3565 "constructor");
3566 }
3567 return false;
3568 }
3569 }
3570 return true;
3571}
3572
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573// This function is used to test for the correctness of the parameters passed to various constructor
3574// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003575//
Olli Etuaho856c4972016-08-08 11:38:39 +03003576// 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 +00003577//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003578TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003579 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303580 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003581{
Olli Etuaho856c4972016-08-08 11:38:39 +03003582 if (type.isUnsizedArray())
3583 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003584 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003587 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003588 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3590 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003591 if (type.getOutermostArraySize() == 0u)
3592 {
3593 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3594 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003595 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003596 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003597 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003598 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003599 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003600 }
3601 }
3602 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003603 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003604
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003605 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003606 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003607 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003609
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003610 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003611 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003612
Olli Etuaho765924f2018-01-04 12:48:36 +02003613 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003614}
3615
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003616//
3617// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003618// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003619//
Olli Etuaho13389b62016-10-16 11:48:18 +01003620TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003621 const TTypeQualifierBuilder &typeQualifierBuilder,
3622 const TSourceLoc &nameLine,
3623 const TString &blockName,
3624 TFieldList *fieldList,
3625 const TString *instanceName,
3626 const TSourceLoc &instanceLine,
3627 TIntermTyped *arrayIndex,
3628 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003629{
Olli Etuaho856c4972016-08-08 11:38:39 +03003630 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003631
Olli Etuaho77ba4082016-12-16 12:01:18 +00003632 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003633
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003636 error(typeQualifier.line,
3637 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3638 "3.10",
3639 getQualifierString(typeQualifier.qualifier));
3640 }
3641 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3642 {
3643 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003644 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003645 }
3646
Martin Radev70866b82016-07-22 15:27:42 +03003647 if (typeQualifier.invariant)
3648 {
3649 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3650 }
3651
Jiajia Qinbc585152017-06-23 15:42:17 +08003652 if (typeQualifier.qualifier != EvqBuffer)
3653 {
3654 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3655 }
Olli Etuaho43364892017-02-13 16:00:12 +00003656
jchen10af713a22017-04-19 09:10:56 +08003657 // add array index
3658 unsigned int arraySize = 0;
3659 if (arrayIndex != nullptr)
3660 {
3661 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3662 }
3663
3664 if (mShaderVersion < 310)
3665 {
3666 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3667 }
3668 else
3669 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003670 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3671 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003672 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003673
Andrei Volykhina5527072017-03-22 16:46:30 +03003674 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3675
Jamie Madill099c0f32013-06-20 11:55:52 -04003676 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003677 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003678 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3679 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003680
Jamie Madill099c0f32013-06-20 11:55:52 -04003681 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3682 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003683 if (typeQualifier.qualifier == EvqUniform)
3684 {
3685 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3686 }
3687 else if (typeQualifier.qualifier == EvqBuffer)
3688 {
3689 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3690 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003691 }
3692
Jamie Madill1566ef72013-06-20 11:55:54 -04003693 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3694 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003695 if (typeQualifier.qualifier == EvqUniform)
3696 {
3697 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3698 }
3699 else if (typeQualifier.qualifier == EvqBuffer)
3700 {
3701 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3702 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003703 }
3704
Olli Etuaho856c4972016-08-08 11:38:39 +03003705 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003706
Martin Radev2cc85b32016-08-05 16:22:53 +03003707 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3708
Jamie Madill98493dd2013-07-08 14:39:03 -04003709 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303710 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003712 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303713 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003714 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 {
jchen10cc2a10e2017-05-03 14:05:12 +08003716 std::string reason("unsupported type - ");
3717 reason += fieldType->getBasicString();
3718 reason += " types are not allowed in interface blocks";
3719 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003720 }
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 switch (qualifier)
3724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003726 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003728 if (typeQualifier.qualifier == EvqBuffer)
3729 {
3730 error(field->line(), "invalid qualifier on shader storage block member",
3731 getQualifierString(qualifier));
3732 }
3733 break;
3734 case EvqBuffer:
3735 if (typeQualifier.qualifier == EvqUniform)
3736 {
3737 error(field->line(), "invalid qualifier on uniform block member",
3738 getQualifierString(qualifier));
3739 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 break;
3741 default:
3742 error(field->line(), "invalid qualifier on interface block member",
3743 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003744 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003745 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003746
Martin Radev70866b82016-07-22 15:27:42 +03003747 if (fieldType->isInvariant())
3748 {
3749 error(field->line(), "invalid qualifier on interface block member", "invariant");
3750 }
3751
Jamie Madilla5efff92013-06-06 11:56:47 -04003752 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003753 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003754 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003755 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003756
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003758 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003759 error(field->line(), "invalid layout qualifier: cannot be used here",
3760 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003761 }
3762
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003764 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003765 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003766 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003767 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003768 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003769 warning(field->line(),
3770 "extraneous layout qualifier: only has an effect on matrix types",
3771 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003772 }
3773
Jamie Madill98493dd2013-07-08 14:39:03 -04003774 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003775
Olli Etuahoebee5b32017-11-23 12:56:32 +02003776 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3777 typeQualifier.qualifier != EvqBuffer)
3778 {
3779 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3780 checkIsNotUnsizedArray(field->line(),
3781 "array members of interface blocks must specify a size",
3782 field->name().c_str(), field->type());
3783 }
3784
Jiajia Qinbc585152017-06-23 15:42:17 +08003785 if (typeQualifier.qualifier == EvqBuffer)
3786 {
3787 // set memory qualifiers
3788 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3789 // qualified with a memory qualifier, it is as if all of its members were declared with
3790 // the same memory qualifier.
3791 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3792 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3793 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3794 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3795 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3796 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3797 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3798 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3799 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3800 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3801 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003802 }
3803
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003804 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3805 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003806 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3807 {
3808 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3809 }
3810
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003811 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3812 if (arrayIndex != nullptr)
3813 {
3814 interfaceBlockType.makeArray(arraySize);
3815 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003816
Olli Etuaho195be942017-12-04 23:40:14 +02003817 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003818 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3819 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003820 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003821 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3822 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003823
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003824 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003825 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003826 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003827 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3828 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303830 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003831
3832 // set parent pointer of the field variable
3833 fieldType->setInterfaceBlock(interfaceBlock);
3834
Olli Etuaho195be942017-12-04 23:40:14 +02003835 TVariable *fieldVariable =
3836 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3837 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003838 {
3839 fieldVariable->setQualifier(typeQualifier.qualifier);
3840 }
3841 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303842 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003843 error(field->line(), "redefinition of an interface block member name",
3844 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003845 }
3846 }
3847 }
3848 else
3849 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003850 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003851
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003852 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003853 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303854 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003855 error(instanceLine, "redefinition of an interface block instance name",
3856 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003858 }
3859
Olli Etuaho195be942017-12-04 23:40:14 +02003860 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3861 blockSymbol->setLine(typeQualifier.line);
3862 TIntermDeclaration *declaration = new TIntermDeclaration();
3863 declaration->appendDeclarator(blockSymbol);
3864 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003865
3866 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003867 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003868}
3869
Olli Etuaho383b7912016-08-05 11:22:59 +03003870void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003872 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873
3874 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003875 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303876 if (mStructNestingLevel > 1)
3877 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003878 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003880}
3881
3882void TParseContext::exitStructDeclaration()
3883{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003884 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885}
3886
Olli Etuaho8a176262016-08-16 14:23:01 +03003887void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888{
Jamie Madillacb4b812016-11-07 13:50:29 -05003889 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003891 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003892 }
3893
Arun Patole7e7e68d2015-05-22 12:02:25 +05303894 if (field.type()->getBasicType() != EbtStruct)
3895 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003896 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003897 }
3898
3899 // We're already inside a structure definition at this point, so add
3900 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303901 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3902 {
Jamie Madill41a49272014-03-18 16:10:13 -04003903 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003904 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3905 {
3906 // This may happen in case there are nested struct definitions. While they are also
3907 // invalid GLSL, they don't cause a syntax error.
3908 reasonStream << "Struct nesting";
3909 }
3910 else
3911 {
3912 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003913 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003914 }
3915 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003916 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003917 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003918 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003919 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003920}
3921
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003922//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003923// Parse an array index expression
3924//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3926 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303927 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3930 {
3931 if (baseExpression->getAsSymbolNode())
3932 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303933 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02003934 baseExpression->getAsSymbolNode()->getName().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 }
3936 else
3937 {
3938 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3939 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003940
Olli Etuaho3ec75682017-07-05 17:02:55 +03003941 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003943
Jiawei Shaod8105a02017-08-08 09:54:36 +08003944 if (baseExpression->getQualifier() == EvqPerVertexIn)
3945 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003946 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003947 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3948 {
3949 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3950 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3951 }
3952 }
3953
Jamie Madill21c1e452014-12-29 11:33:41 -05003954 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3955
Olli Etuaho36b05142015-11-12 13:10:42 +02003956 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3957 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3958 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3959 // index is a constant expression.
3960 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3961 {
3962 if (baseExpression->isInterfaceBlock())
3963 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003964 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003965 switch (baseExpression->getQualifier())
3966 {
3967 case EvqPerVertexIn:
3968 break;
3969 case EvqUniform:
3970 case EvqBuffer:
3971 error(location,
3972 "array indexes for uniform block arrays and shader storage block arrays "
3973 "must be constant integral expressions",
3974 "[");
3975 break;
3976 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003977 // We can reach here only in error cases.
3978 ASSERT(mDiagnostics->numErrors() > 0);
3979 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003980 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003981 }
3982 else if (baseExpression->getQualifier() == EvqFragmentOut)
3983 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003984 error(location,
3985 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003986 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003987 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3988 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003989 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003990 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003991 }
3992
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003993 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003994 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003995 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3996 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3997 // constant fold expressions that are not constant expressions). The most compatible way to
3998 // handle this case is to report a warning instead of an error and force the index to be in
3999 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004000 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004001 int index = 0;
4002 if (indexConstantUnion->getBasicType() == EbtInt)
4003 {
4004 index = indexConstantUnion->getIConst(0);
4005 }
4006 else if (indexConstantUnion->getBasicType() == EbtUInt)
4007 {
4008 index = static_cast<int>(indexConstantUnion->getUConst(0));
4009 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004010
4011 int safeIndex = -1;
4012
Olli Etuahoebee5b32017-11-23 12:56:32 +02004013 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4016 safeIndex = 0;
4017 }
4018
4019 if (!baseExpression->getType().isUnsizedArray())
4020 {
4021 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004022 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004023 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004024 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004025 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4026 {
4027 outOfRangeError(outOfRangeIndexIsError, location,
4028 "array index for gl_FragData must be zero when "
4029 "GL_EXT_draw_buffers is disabled",
4030 "[]");
4031 safeIndex = 0;
4032 }
4033 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004034 }
4035 // Only do generic out-of-range check if similar error hasn't already been reported.
4036 if (safeIndex < 0)
4037 {
4038 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004039 {
4040 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4041 baseExpression->getOutermostArraySize(),
4042 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004043 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004044 else if (baseExpression->isMatrix())
4045 {
4046 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4047 baseExpression->getType().getCols(),
4048 "matrix field selection out of range");
4049 }
4050 else
4051 {
4052 ASSERT(baseExpression->isVector());
4053 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4054 baseExpression->getType().getNominalSize(),
4055 "vector field selection out of range");
4056 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004057 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004058
Olli Etuahoebee5b32017-11-23 12:56:32 +02004059 ASSERT(safeIndex >= 0);
4060 // Data of constant unions can't be changed, because it may be shared with other
4061 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4062 // sanitized object.
4063 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4064 {
4065 TConstantUnion *safeConstantUnion = new TConstantUnion();
4066 safeConstantUnion->setIConst(safeIndex);
4067 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4068 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4069 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004070
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071 TIntermBinary *node =
4072 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4073 node->setLine(location);
4074 return node->fold(mDiagnostics);
4075 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004077
4078 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4079 node->setLine(location);
4080 // Indirect indexing can never be constant folded.
4081 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004082}
4083
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4085 const TSourceLoc &location,
4086 int index,
4087 int arraySize,
4088 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004089{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004090 // Should not reach here with an unsized / runtime-sized array.
4091 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004092 // A negative index should already have been checked.
4093 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004096 std::stringstream reasonStream;
4097 reasonStream << reason << " '" << index << "'";
4098 std::string token = reasonStream.str();
4099 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004100 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004101 }
4102 return index;
4103}
4104
Jamie Madillb98c3a82015-07-23 14:26:04 -04004105TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4106 const TSourceLoc &dotLocation,
4107 const TString &fieldString,
4108 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 if (baseExpression->isArray())
4111 {
4112 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004113 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
4115
4116 if (baseExpression->isVector())
4117 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 TVector<int> fieldOffsets;
4119 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4120 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 fieldOffsets.resize(1);
4123 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4126 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127
Olli Etuaho765924f2018-01-04 12:48:36 +02004128 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 else if (baseExpression->getBasicType() == EbtStruct)
4131 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304132 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004133 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 {
4135 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 }
4138 else
4139 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004142 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004144 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 {
4146 fieldFound = true;
4147 break;
4148 }
4149 }
4150 if (fieldFound)
4151 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004152 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004153 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004154 TIntermBinary *node =
4155 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4156 node->setLine(dotLocation);
4157 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158 }
4159 else
4160 {
4161 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 }
4164 }
4165 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004166 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304168 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004169 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 {
4171 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004172 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 }
4174 else
4175 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004178 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004180 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004181 {
4182 fieldFound = true;
4183 break;
4184 }
4185 }
4186 if (fieldFound)
4187 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004188 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004189 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004190 TIntermBinary *node =
4191 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4192 node->setLine(dotLocation);
4193 // Indexing interface blocks can never be constant folded.
4194 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 }
4196 else
4197 {
4198 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004199 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 }
4201 }
4202 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004203 else
4204 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004205 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004207 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304208 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004209 }
4210 else
4211 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004213 " field selection requires structure, vector, or interface block on left hand "
4214 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304215 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004216 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004217 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004218 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004219}
4220
Jamie Madillb98c3a82015-07-23 14:26:04 -04004221TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4222 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004223{
Jamie Madill2f294c92017-11-20 14:47:26 -05004224 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225
4226 if (qualifierType == "shared")
4227 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004228 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004229 {
4230 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4231 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004232 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004233 }
4234 else if (qualifierType == "packed")
4235 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004236 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004237 {
4238 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4239 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004240 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004242 else if (qualifierType == "std430")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.blockStorage = EbsStd430;
4246 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 else if (qualifierType == "std140")
4248 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004249 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004250 }
4251 else if (qualifierType == "row_major")
4252 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004253 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 }
4255 else if (qualifierType == "column_major")
4256 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004257 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004258 }
4259 else if (qualifierType == "location")
4260 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004261 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4262 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004263 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004264 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004265 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004266 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4267 {
4268 qualifier.yuv = true;
4269 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004270 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004271 else if (qualifierType == "rgba32f")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifRGBA32F;
4275 }
4276 else if (qualifierType == "rgba16f")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA16F;
4280 }
4281 else if (qualifierType == "r32f")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifR32F;
4285 }
4286 else if (qualifierType == "rgba8")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA8;
4290 }
4291 else if (qualifierType == "rgba8_snorm")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4295 }
4296 else if (qualifierType == "rgba32i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA32I;
4300 }
4301 else if (qualifierType == "rgba16i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA16I;
4305 }
4306 else if (qualifierType == "rgba8i")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA8I;
4310 }
4311 else if (qualifierType == "r32i")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifR32I;
4315 }
4316 else if (qualifierType == "rgba32ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifRGBA32UI;
4320 }
4321 else if (qualifierType == "rgba16ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifRGBA16UI;
4325 }
4326 else if (qualifierType == "rgba8ui")
4327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.imageInternalFormat = EiifRGBA8UI;
4330 }
4331 else if (qualifierType == "r32ui")
4332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.imageInternalFormat = EiifR32UI;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptPoints;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLines;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptLinesAdjacency;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptTriangles;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptTrianglesAdjacency;
4365 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004366 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptLineStrip;
4371 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004372 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4373 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004374 {
4375 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4376 qualifier.primitiveType = EptTriangleStrip;
4377 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004378
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379 else
4380 {
4381 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004382 }
4383
Jamie Madilla5efff92013-06-06 11:56:47 -04004384 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004385}
4386
Martin Radev802abe02016-08-04 17:48:32 +03004387void TParseContext::parseLocalSize(const TString &qualifierType,
4388 const TSourceLoc &qualifierTypeLine,
4389 int intValue,
4390 const TSourceLoc &intValueLine,
4391 const std::string &intValueString,
4392 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004393 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004394{
Olli Etuaho856c4972016-08-08 11:38:39 +03004395 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004396 if (intValue < 1)
4397 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004398 std::stringstream reasonStream;
4399 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4400 std::string reason = reasonStream.str();
4401 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004402 }
4403 (*localSize)[index] = intValue;
4404}
4405
Olli Etuaho09b04a22016-12-15 13:30:26 +00004406void TParseContext::parseNumViews(int intValue,
4407 const TSourceLoc &intValueLine,
4408 const std::string &intValueString,
4409 int *numViews)
4410{
4411 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4412 // specification.
4413 if (intValue < 1)
4414 {
4415 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4416 }
4417 *numViews = intValue;
4418}
4419
Shaob5cc1192017-07-06 10:47:20 +08004420void TParseContext::parseInvocations(int intValue,
4421 const TSourceLoc &intValueLine,
4422 const std::string &intValueString,
4423 int *numInvocations)
4424{
4425 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4426 // it doesn't make sense to accept invocations <= 0.
4427 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4428 {
4429 error(intValueLine,
4430 "out of range: invocations must be in the range of [1, "
4431 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4432 intValueString.c_str());
4433 }
4434 else
4435 {
4436 *numInvocations = intValue;
4437 }
4438}
4439
4440void TParseContext::parseMaxVertices(int intValue,
4441 const TSourceLoc &intValueLine,
4442 const std::string &intValueString,
4443 int *maxVertices)
4444{
4445 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4446 // it doesn't make sense to accept max_vertices < 0.
4447 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4448 {
4449 error(
4450 intValueLine,
4451 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4452 intValueString.c_str());
4453 }
4454 else
4455 {
4456 *maxVertices = intValue;
4457 }
4458}
4459
Jamie Madillb98c3a82015-07-23 14:26:04 -04004460TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4461 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004462 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304463 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464{
Jamie Madill2f294c92017-11-20 14:47:26 -05004465 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466
Martin Radev802abe02016-08-04 17:48:32 +03004467 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468
Martin Radev802abe02016-08-04 17:48:32 +03004469 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004471 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472 if (intValue < 0)
4473 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004474 error(intValueLine, "out of range: location must be non-negative",
4475 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 }
4477 else
4478 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004479 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004480 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482 }
Olli Etuaho43364892017-02-13 16:00:12 +00004483 else if (qualifierType == "binding")
4484 {
4485 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4486 if (intValue < 0)
4487 {
4488 error(intValueLine, "out of range: binding must be non-negative",
4489 intValueString.c_str());
4490 }
4491 else
4492 {
4493 qualifier.binding = intValue;
4494 }
4495 }
jchen104cdac9e2017-05-08 11:01:20 +08004496 else if (qualifierType == "offset")
4497 {
4498 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4499 if (intValue < 0)
4500 {
4501 error(intValueLine, "out of range: offset must be non-negative",
4502 intValueString.c_str());
4503 }
4504 else
4505 {
4506 qualifier.offset = intValue;
4507 }
4508 }
Martin Radev802abe02016-08-04 17:48:32 +03004509 else if (qualifierType == "local_size_x")
4510 {
4511 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4512 &qualifier.localSize);
4513 }
4514 else if (qualifierType == "local_size_y")
4515 {
4516 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4517 &qualifier.localSize);
4518 }
4519 else if (qualifierType == "local_size_z")
4520 {
4521 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4522 &qualifier.localSize);
4523 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004524 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004525 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004526 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4527 {
4528 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4529 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004530 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004531 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4532 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004533 {
4534 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4535 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004536 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4537 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004538 {
4539 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4540 }
4541
Martin Radev802abe02016-08-04 17:48:32 +03004542 else
4543 {
4544 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004545 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004546
Jamie Madilla5efff92013-06-06 11:56:47 -04004547 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004548}
4549
Olli Etuaho613b9592016-09-05 12:05:53 +03004550TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4551{
4552 return new TTypeQualifierBuilder(
4553 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4554 mShaderVersion);
4555}
4556
Olli Etuahocce89652017-06-19 16:04:09 +03004557TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4558 const TSourceLoc &loc)
4559{
4560 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4561 return new TStorageQualifierWrapper(qualifier, loc);
4562}
4563
4564TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4565{
4566 if (getShaderType() == GL_VERTEX_SHADER)
4567 {
4568 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4569 }
4570 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4571}
4572
4573TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4574{
4575 if (declaringFunction())
4576 {
4577 return new TStorageQualifierWrapper(EvqIn, loc);
4578 }
Shaob5cc1192017-07-06 10:47:20 +08004579
4580 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004581 {
Shaob5cc1192017-07-06 10:47:20 +08004582 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004583 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004584 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004585 {
4586 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4587 }
4588 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004589 }
Shaob5cc1192017-07-06 10:47:20 +08004590 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004591 {
Shaob5cc1192017-07-06 10:47:20 +08004592 if (mShaderVersion < 300)
4593 {
4594 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4595 }
4596 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004597 }
Shaob5cc1192017-07-06 10:47:20 +08004598 case GL_COMPUTE_SHADER:
4599 {
4600 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4601 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004602 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004603 {
4604 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4605 }
4606 default:
4607 {
4608 UNREACHABLE();
4609 return new TStorageQualifierWrapper(EvqLast, loc);
4610 }
Olli Etuahocce89652017-06-19 16:04:09 +03004611 }
Olli Etuahocce89652017-06-19 16:04:09 +03004612}
4613
4614TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4615{
4616 if (declaringFunction())
4617 {
4618 return new TStorageQualifierWrapper(EvqOut, loc);
4619 }
Shaob5cc1192017-07-06 10:47:20 +08004620 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004621 {
Shaob5cc1192017-07-06 10:47:20 +08004622 case GL_VERTEX_SHADER:
4623 {
4624 if (mShaderVersion < 300)
4625 {
4626 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4627 }
4628 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4629 }
4630 case GL_FRAGMENT_SHADER:
4631 {
4632 if (mShaderVersion < 300)
4633 {
4634 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4635 }
4636 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4637 }
4638 case GL_COMPUTE_SHADER:
4639 {
4640 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4641 return new TStorageQualifierWrapper(EvqLast, loc);
4642 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004643 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004644 {
4645 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4646 }
4647 default:
4648 {
4649 UNREACHABLE();
4650 return new TStorageQualifierWrapper(EvqLast, loc);
4651 }
Olli Etuahocce89652017-06-19 16:04:09 +03004652 }
Olli Etuahocce89652017-06-19 16:04:09 +03004653}
4654
4655TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4656{
4657 if (!declaringFunction())
4658 {
4659 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4660 }
4661 return new TStorageQualifierWrapper(EvqInOut, loc);
4662}
4663
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004665 TLayoutQualifier rightQualifier,
4666 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004667{
Martin Radevc28888b2016-07-22 15:27:42 +03004668 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004669 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004670}
4671
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004673{
4674 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004676}
4677
Olli Etuahod5f44c92017-11-29 17:15:40 +02004678TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4679 const TSourceLoc &loc,
4680 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004681{
4682 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004683 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004684}
4685
Olli Etuaho722bfb52017-10-26 17:00:11 +03004686void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4687 const TFieldList::const_iterator end,
4688 const TString &name,
4689 const TSourceLoc &location)
4690{
4691 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4692 {
4693 if ((*fieldIter)->name() == name)
4694 {
4695 error(location, "duplicate field name in structure", name.c_str());
4696 }
4697 }
4698}
4699
4700TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4701{
4702 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4703 ++fieldIter)
4704 {
4705 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4706 location);
4707 }
4708 return fields;
4709}
4710
Olli Etuaho4de340a2016-12-16 09:32:03 +00004711TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4712 const TFieldList *newlyAddedFields,
4713 const TSourceLoc &location)
4714{
4715 for (TField *field : *newlyAddedFields)
4716 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004717 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4718 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004719 processedFields->push_back(field);
4720 }
4721 return processedFields;
4722}
4723
Martin Radev70866b82016-07-22 15:27:42 +03004724TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4725 const TTypeQualifierBuilder &typeQualifierBuilder,
4726 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004727 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004728{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004729 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004730
Martin Radev70866b82016-07-22 15:27:42 +03004731 typeSpecifier->qualifier = typeQualifier.qualifier;
4732 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004733 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004734 typeSpecifier->invariant = typeQualifier.invariant;
4735 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304736 {
Martin Radev70866b82016-07-22 15:27:42 +03004737 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004738 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004740}
4741
Jamie Madillb98c3a82015-07-23 14:26:04 -04004742TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004744{
Martin Radev4a9cd802016-09-01 16:51:51 +03004745 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4746 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004747
Olli Etuahod5f44c92017-11-29 17:15:40 +02004748 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004749 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004750
Martin Radev4a9cd802016-09-01 16:51:51 +03004751 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004752
Olli Etuahod5f44c92017-11-29 17:15:40 +02004753 TFieldList *fieldList = new TFieldList();
4754
4755 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304756 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004757 TType *type = new TType(typeSpecifier);
4758 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304759 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004760 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004761 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004762 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004764
Olli Etuahod5f44c92017-11-29 17:15:40 +02004765 TField *field = new TField(type, declarator->name(), declarator->line());
4766 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4767 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768 }
4769
Olli Etuahod5f44c92017-11-29 17:15:40 +02004770 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004771}
4772
Martin Radev4a9cd802016-09-01 16:51:51 +03004773TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4774 const TSourceLoc &nameLine,
4775 const TString *structName,
4776 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004777{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004778 SymbolType structSymbolType = SymbolType::UserDefined;
4779 if (structName == nullptr)
4780 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004781 structSymbolType = SymbolType::Empty;
4782 }
4783 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784
Jamie Madill9b820842015-02-12 10:40:10 -05004785 // Store a bool in the struct if we're at global scope, to allow us to
4786 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004787 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004788
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004789 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004790 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004791 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004792 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304793 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004794 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 }
4796 }
4797
4798 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004799 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004800 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004801 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004802 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004803 switch (qualifier)
4804 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004805 case EvqGlobal:
4806 case EvqTemporary:
4807 break;
4808 default:
4809 error(field.line(), "invalid qualifier on struct member",
4810 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004811 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004812 }
Martin Radev70866b82016-07-22 15:27:42 +03004813 if (field.type()->isInvariant())
4814 {
4815 error(field.line(), "invalid qualifier on struct member", "invariant");
4816 }
jchen104cdac9e2017-05-08 11:01:20 +08004817 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4818 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004819 {
4820 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4821 }
4822
Olli Etuahoebee5b32017-11-23 12:56:32 +02004823 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4824 field.name().c_str(), field.type());
4825
Olli Etuaho43364892017-02-13 16:00:12 +00004826 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4827
4828 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004829
4830 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004831 }
4832
Martin Radev4a9cd802016-09-01 16:51:51 +03004833 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004834 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004835 exitStructDeclaration();
4836
Martin Radev4a9cd802016-09-01 16:51:51 +03004837 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004838}
4839
Jamie Madillb98c3a82015-07-23 14:26:04 -04004840TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004841 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004843{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004845 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004846 init->isVector())
4847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004848 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4849 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004850 return nullptr;
4851 }
4852
Olli Etuaho923ecef2017-10-11 12:01:38 +03004853 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004854 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004855 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004856 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004857 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004858 }
4859
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004860 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4861 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004862 return node;
4863}
4864
4865TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4866{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 if (mSwitchNestingLevel == 0)
4868 {
4869 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 return nullptr;
4871 }
4872 if (condition == nullptr)
4873 {
4874 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 return nullptr;
4876 }
4877 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004878 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 {
4880 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 }
4882 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004883 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4884 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4885 // fold in case labels.
4886 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 {
4888 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004890 TIntermCase *node = new TIntermCase(condition);
4891 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004892 return node;
4893}
4894
4895TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4896{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004897 if (mSwitchNestingLevel == 0)
4898 {
4899 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004900 return nullptr;
4901 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004902 TIntermCase *node = new TIntermCase(nullptr);
4903 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004904 return node;
4905}
4906
Jamie Madillb98c3a82015-07-23 14:26:04 -04004907TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4908 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004909 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004910{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004911 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004912
4913 switch (op)
4914 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004915 case EOpLogicalNot:
4916 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4917 child->isVector())
4918 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004919 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920 return nullptr;
4921 }
4922 break;
4923 case EOpBitwiseNot:
4924 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4925 child->isMatrix() || child->isArray())
4926 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004927 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004928 return nullptr;
4929 }
4930 break;
4931 case EOpPostIncrement:
4932 case EOpPreIncrement:
4933 case EOpPostDecrement:
4934 case EOpPreDecrement:
4935 case EOpNegative:
4936 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004937 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4938 child->getBasicType() == EbtBool || child->isArray() ||
4939 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004940 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004942 return nullptr;
4943 }
4944 // Operators for built-ins are already type checked against their prototype.
4945 default:
4946 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004947 }
4948
Jiajia Qinbc585152017-06-23 15:42:17 +08004949 if (child->getMemoryQualifier().writeonly)
4950 {
4951 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4952 return nullptr;
4953 }
4954
Olli Etuahof119a262016-08-19 15:54:22 +03004955 TIntermUnary *node = new TIntermUnary(op, child);
4956 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004957
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004958 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004959}
4960
Olli Etuaho09b22472015-02-11 11:47:26 +02004961TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4962{
Olli Etuahocce89652017-06-19 16:04:09 +03004963 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004964 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004965 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004966 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 return child;
4968 }
4969 return node;
4970}
4971
Jamie Madillb98c3a82015-07-23 14:26:04 -04004972TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4973 TIntermTyped *child,
4974 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004975{
Olli Etuaho856c4972016-08-08 11:38:39 +03004976 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004977 return addUnaryMath(op, child, loc);
4978}
4979
Olli Etuaho765924f2018-01-04 12:48:36 +02004980TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4981{
4982 // If we can, we should return the folded version of the expression for subsequent parsing. This
4983 // enables folding the containing expression during parsing as well, instead of the separate
4984 // FoldExpressions() step where folding nested expressions requires multiple full AST
4985 // traversals.
4986
4987 // Even if folding fails the fold() functions return some node representing the expression,
4988 // typically the original node. So "folded" can be assumed to be non-null.
4989 TIntermTyped *folded = expression->fold(mDiagnostics);
4990 ASSERT(folded != nullptr);
4991 if (folded->getQualifier() == expression->getQualifier())
4992 {
4993 // We need this expression to have the correct qualifier when validating the consuming
4994 // expression. So we can only return the folded node from here in case it has the same
4995 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4996 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4997 // 1. (true ? 1.0 : non_constant)
4998 // 2. (non_constant, 1.0)
4999 return folded;
5000 }
5001 return expression;
5002}
5003
Jamie Madillb98c3a82015-07-23 14:26:04 -04005004bool TParseContext::binaryOpCommonCheck(TOperator op,
5005 TIntermTyped *left,
5006 TIntermTyped *right,
5007 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005008{
jchen10b4cf5652017-05-05 18:51:17 +08005009 // Check opaque types are not allowed to be operands in expressions other than array indexing
5010 // and structure member selection.
5011 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5012 {
5013 switch (op)
5014 {
5015 case EOpIndexDirect:
5016 case EOpIndexIndirect:
5017 break;
5018 case EOpIndexDirectStruct:
5019 UNREACHABLE();
5020
5021 default:
5022 error(loc, "Invalid operation for variables with an opaque type",
5023 GetOperatorString(op));
5024 return false;
5025 }
5026 }
jchen10cc2a10e2017-05-03 14:05:12 +08005027
Jiajia Qinbc585152017-06-23 15:42:17 +08005028 if (right->getMemoryQualifier().writeonly)
5029 {
5030 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5031 return false;
5032 }
5033
5034 if (left->getMemoryQualifier().writeonly)
5035 {
5036 switch (op)
5037 {
5038 case EOpAssign:
5039 case EOpInitialize:
5040 case EOpIndexDirect:
5041 case EOpIndexIndirect:
5042 case EOpIndexDirectStruct:
5043 case EOpIndexDirectInterfaceBlock:
5044 break;
5045 default:
5046 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5047 return false;
5048 }
5049 }
5050
Olli Etuaho244be012016-08-18 15:26:02 +03005051 if (left->getType().getStruct() || right->getType().getStruct())
5052 {
5053 switch (op)
5054 {
5055 case EOpIndexDirectStruct:
5056 ASSERT(left->getType().getStruct());
5057 break;
5058 case EOpEqual:
5059 case EOpNotEqual:
5060 case EOpAssign:
5061 case EOpInitialize:
5062 if (left->getType() != right->getType())
5063 {
5064 return false;
5065 }
5066 break;
5067 default:
5068 error(loc, "Invalid operation for structs", GetOperatorString(op));
5069 return false;
5070 }
5071 }
5072
Olli Etuaho94050052017-05-08 14:17:44 +03005073 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5074 {
5075 switch (op)
5076 {
5077 case EOpIndexDirectInterfaceBlock:
5078 ASSERT(left->getType().getInterfaceBlock());
5079 break;
5080 default:
5081 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5082 return false;
5083 }
5084 }
5085
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005086 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005087 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005088 error(loc, "array / non-array mismatch", GetOperatorString(op));
5089 return false;
5090 }
5091
5092 if (left->isArray())
5093 {
5094 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005095 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005096 {
5097 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5098 return false;
5099 }
5100
Olli Etuahoe79904c2015-03-18 16:56:42 +02005101 switch (op)
5102 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005103 case EOpEqual:
5104 case EOpNotEqual:
5105 case EOpAssign:
5106 case EOpInitialize:
5107 break;
5108 default:
5109 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5110 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005111 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005112 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005113 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005114 {
5115 error(loc, "array size mismatch", GetOperatorString(op));
5116 return false;
5117 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005118 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005119
5120 // Check ops which require integer / ivec parameters
5121 bool isBitShift = false;
5122 switch (op)
5123 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005124 case EOpBitShiftLeft:
5125 case EOpBitShiftRight:
5126 case EOpBitShiftLeftAssign:
5127 case EOpBitShiftRightAssign:
5128 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5129 // check that the basic type is an integer type.
5130 isBitShift = true;
5131 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5132 {
5133 return false;
5134 }
5135 break;
5136 case EOpBitwiseAnd:
5137 case EOpBitwiseXor:
5138 case EOpBitwiseOr:
5139 case EOpBitwiseAndAssign:
5140 case EOpBitwiseXorAssign:
5141 case EOpBitwiseOrAssign:
5142 // It is enough to check the type of only one operand, since later it
5143 // is checked that the operand types match.
5144 if (!IsInteger(left->getBasicType()))
5145 {
5146 return false;
5147 }
5148 break;
5149 default:
5150 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005151 }
5152
5153 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5154 // So the basic type should usually match.
5155 if (!isBitShift && left->getBasicType() != right->getBasicType())
5156 {
5157 return false;
5158 }
5159
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005160 // Check that:
5161 // 1. Type sizes match exactly on ops that require that.
5162 // 2. Restrictions for structs that contain arrays or samplers are respected.
5163 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005164 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005165 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 case EOpAssign:
5167 case EOpInitialize:
5168 case EOpEqual:
5169 case EOpNotEqual:
5170 // ESSL 1.00 sections 5.7, 5.8, 5.9
5171 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5172 {
5173 error(loc, "undefined operation for structs containing arrays",
5174 GetOperatorString(op));
5175 return false;
5176 }
5177 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5178 // we interpret the spec so that this extends to structs containing samplers,
5179 // similarly to ESSL 1.00 spec.
5180 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5181 left->getType().isStructureContainingSamplers())
5182 {
5183 error(loc, "undefined operation for structs containing samplers",
5184 GetOperatorString(op));
5185 return false;
5186 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005187
Olli Etuahoe1805592017-01-02 16:41:20 +00005188 if ((left->getNominalSize() != right->getNominalSize()) ||
5189 (left->getSecondarySize() != right->getSecondarySize()))
5190 {
5191 error(loc, "dimension mismatch", GetOperatorString(op));
5192 return false;
5193 }
5194 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005195 case EOpLessThan:
5196 case EOpGreaterThan:
5197 case EOpLessThanEqual:
5198 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005199 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005200 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005201 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005202 return false;
5203 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005204 break;
5205 case EOpAdd:
5206 case EOpSub:
5207 case EOpDiv:
5208 case EOpIMod:
5209 case EOpBitShiftLeft:
5210 case EOpBitShiftRight:
5211 case EOpBitwiseAnd:
5212 case EOpBitwiseXor:
5213 case EOpBitwiseOr:
5214 case EOpAddAssign:
5215 case EOpSubAssign:
5216 case EOpDivAssign:
5217 case EOpIModAssign:
5218 case EOpBitShiftLeftAssign:
5219 case EOpBitShiftRightAssign:
5220 case EOpBitwiseAndAssign:
5221 case EOpBitwiseXorAssign:
5222 case EOpBitwiseOrAssign:
5223 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5224 {
5225 return false;
5226 }
5227
5228 // Are the sizes compatible?
5229 if (left->getNominalSize() != right->getNominalSize() ||
5230 left->getSecondarySize() != right->getSecondarySize())
5231 {
5232 // If the nominal sizes of operands do not match:
5233 // One of them must be a scalar.
5234 if (!left->isScalar() && !right->isScalar())
5235 return false;
5236
5237 // In the case of compound assignment other than multiply-assign,
5238 // the right side needs to be a scalar. Otherwise a vector/matrix
5239 // would be assigned to a scalar. A scalar can't be shifted by a
5240 // vector either.
5241 if (!right->isScalar() &&
5242 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5243 return false;
5244 }
5245 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005246 default:
5247 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005248 }
5249
Olli Etuahod6b14282015-03-17 14:31:35 +02005250 return true;
5251}
5252
Olli Etuaho1dded802016-08-18 18:13:13 +03005253bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5254 const TType &left,
5255 const TType &right)
5256{
5257 switch (op)
5258 {
5259 case EOpMul:
5260 case EOpMulAssign:
5261 return left.getNominalSize() == right.getNominalSize() &&
5262 left.getSecondarySize() == right.getSecondarySize();
5263 case EOpVectorTimesScalar:
5264 return true;
5265 case EOpVectorTimesScalarAssign:
5266 ASSERT(!left.isMatrix() && !right.isMatrix());
5267 return left.isVector() && !right.isVector();
5268 case EOpVectorTimesMatrix:
5269 return left.getNominalSize() == right.getRows();
5270 case EOpVectorTimesMatrixAssign:
5271 ASSERT(!left.isMatrix() && right.isMatrix());
5272 return left.isVector() && left.getNominalSize() == right.getRows() &&
5273 left.getNominalSize() == right.getCols();
5274 case EOpMatrixTimesVector:
5275 return left.getCols() == right.getNominalSize();
5276 case EOpMatrixTimesScalar:
5277 return true;
5278 case EOpMatrixTimesScalarAssign:
5279 ASSERT(left.isMatrix() && !right.isMatrix());
5280 return !right.isVector();
5281 case EOpMatrixTimesMatrix:
5282 return left.getCols() == right.getRows();
5283 case EOpMatrixTimesMatrixAssign:
5284 ASSERT(left.isMatrix() && right.isMatrix());
5285 // We need to check two things:
5286 // 1. The matrix multiplication step is valid.
5287 // 2. The result will have the same number of columns as the lvalue.
5288 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5289
5290 default:
5291 UNREACHABLE();
5292 return false;
5293 }
5294}
5295
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5297 TIntermTyped *left,
5298 TIntermTyped *right,
5299 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005300{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005301 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005302 return nullptr;
5303
Olli Etuahofc1806e2015-03-17 13:03:11 +02005304 switch (op)
5305 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005306 case EOpEqual:
5307 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 case EOpLessThan:
5309 case EOpGreaterThan:
5310 case EOpLessThanEqual:
5311 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 break;
5313 case EOpLogicalOr:
5314 case EOpLogicalXor:
5315 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005316 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5317 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005318 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319 {
5320 return nullptr;
5321 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005322 // Basic types matching should have been already checked.
5323 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005324 break;
5325 case EOpAdd:
5326 case EOpSub:
5327 case EOpDiv:
5328 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005329 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5330 !right->getType().getStruct());
5331 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005332 {
5333 return nullptr;
5334 }
5335 break;
5336 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005337 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5338 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005339 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005340 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005341 {
5342 return nullptr;
5343 }
5344 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005345 default:
5346 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005347 }
5348
Olli Etuaho1dded802016-08-18 18:13:13 +03005349 if (op == EOpMul)
5350 {
5351 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5352 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5353 {
5354 return nullptr;
5355 }
5356 }
5357
Olli Etuaho3fdec912016-08-18 15:08:06 +03005358 TIntermBinary *node = new TIntermBinary(op, left, right);
5359 node->setLine(loc);
5360
Olli Etuaho3fdec912016-08-18 15:08:06 +03005361 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005362 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005363}
5364
Jamie Madillb98c3a82015-07-23 14:26:04 -04005365TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5366 TIntermTyped *left,
5367 TIntermTyped *right,
5368 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005369{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005370 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005371 if (node == 0)
5372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005373 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5374 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005375 return left;
5376 }
5377 return node;
5378}
5379
Jamie Madillb98c3a82015-07-23 14:26:04 -04005380TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5381 TIntermTyped *left,
5382 TIntermTyped *right,
5383 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005384{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005385 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005386 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005387 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005388 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5389 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005390 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005391 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005392 }
5393 return node;
5394}
5395
Olli Etuaho13389b62016-10-16 11:48:18 +01005396TIntermBinary *TParseContext::createAssign(TOperator op,
5397 TIntermTyped *left,
5398 TIntermTyped *right,
5399 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005400{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005401 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005402 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005403 if (op == EOpMulAssign)
5404 {
5405 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5406 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5407 {
5408 return nullptr;
5409 }
5410 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005411 TIntermBinary *node = new TIntermBinary(op, left, right);
5412 node->setLine(loc);
5413
Olli Etuaho3fdec912016-08-18 15:08:06 +03005414 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005415 }
5416 return nullptr;
5417}
5418
Jamie Madillb98c3a82015-07-23 14:26:04 -04005419TIntermTyped *TParseContext::addAssign(TOperator op,
5420 TIntermTyped *left,
5421 TIntermTyped *right,
5422 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005423{
Olli Etuahocce89652017-06-19 16:04:09 +03005424 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005425 TIntermTyped *node = createAssign(op, left, right, loc);
5426 if (node == nullptr)
5427 {
5428 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005429 return left;
5430 }
5431 return node;
5432}
5433
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005434TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5435 TIntermTyped *right,
5436 const TSourceLoc &loc)
5437{
Corentin Wallez0d959252016-07-12 17:26:32 -04005438 // WebGL2 section 5.26, the following results in an error:
5439 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005440 if (mShaderSpec == SH_WEBGL2_SPEC &&
5441 (left->isArray() || left->getBasicType() == EbtVoid ||
5442 left->getType().isStructureContainingArrays() || right->isArray() ||
5443 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005444 {
5445 error(loc,
5446 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5447 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005448 }
5449
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005450 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5451 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5452 commaNode->getTypePointer()->setQualifier(resultQualifier);
Olli Etuaho765924f2018-01-04 12:48:36 +02005453
5454 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005455}
5456
Olli Etuaho49300862015-02-20 14:54:49 +02005457TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5458{
5459 switch (op)
5460 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461 case EOpContinue:
5462 if (mLoopNestingLevel <= 0)
5463 {
5464 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 }
5466 break;
5467 case EOpBreak:
5468 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5469 {
5470 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005471 }
5472 break;
5473 case EOpReturn:
5474 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5475 {
5476 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005477 }
5478 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005479 case EOpKill:
5480 if (mShaderType != GL_FRAGMENT_SHADER)
5481 {
5482 error(loc, "discard supported in fragment shaders only", "discard");
5483 }
5484 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005485 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005486 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005487 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005488 }
Olli Etuahocce89652017-06-19 16:04:09 +03005489 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005490}
5491
Jamie Madillb98c3a82015-07-23 14:26:04 -04005492TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005493 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005494 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005495{
Olli Etuahocce89652017-06-19 16:04:09 +03005496 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005497 {
Olli Etuahocce89652017-06-19 16:04:09 +03005498 ASSERT(op == EOpReturn);
5499 mFunctionReturnsValue = true;
5500 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5501 {
5502 error(loc, "void function cannot return a value", "return");
5503 }
5504 else if (*mCurrentFunctionType != expression->getType())
5505 {
5506 error(loc, "function return is not matching type:", "return");
5507 }
Olli Etuaho49300862015-02-20 14:54:49 +02005508 }
Olli Etuahocce89652017-06-19 16:04:09 +03005509 TIntermBranch *node = new TIntermBranch(op, expression);
5510 node->setLine(loc);
5511 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005512}
5513
Martin Radev84aa2dc2017-09-11 15:51:02 +03005514void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5515{
5516 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005517 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005518 bool isTextureGather = (name == "textureGather");
5519 bool isTextureGatherOffset = (name == "textureGatherOffset");
5520 if (isTextureGather || isTextureGatherOffset)
5521 {
5522 TIntermNode *componentNode = nullptr;
5523 TIntermSequence *arguments = functionCall->getSequence();
5524 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5525 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5526 ASSERT(sampler != nullptr);
5527 switch (sampler->getBasicType())
5528 {
5529 case EbtSampler2D:
5530 case EbtISampler2D:
5531 case EbtUSampler2D:
5532 case EbtSampler2DArray:
5533 case EbtISampler2DArray:
5534 case EbtUSampler2DArray:
5535 if ((isTextureGather && arguments->size() == 3u) ||
5536 (isTextureGatherOffset && arguments->size() == 4u))
5537 {
5538 componentNode = arguments->back();
5539 }
5540 break;
5541 case EbtSamplerCube:
5542 case EbtISamplerCube:
5543 case EbtUSamplerCube:
5544 ASSERT(!isTextureGatherOffset);
5545 if (arguments->size() == 3u)
5546 {
5547 componentNode = arguments->back();
5548 }
5549 break;
5550 case EbtSampler2DShadow:
5551 case EbtSampler2DArrayShadow:
5552 case EbtSamplerCubeShadow:
5553 break;
5554 default:
5555 UNREACHABLE();
5556 break;
5557 }
5558 if (componentNode)
5559 {
5560 const TIntermConstantUnion *componentConstantUnion =
5561 componentNode->getAsConstantUnion();
5562 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5563 {
5564 error(functionCall->getLine(), "Texture component must be a constant expression",
5565 name.c_str());
5566 }
5567 else
5568 {
5569 int component = componentConstantUnion->getIConst(0);
5570 if (component < 0 || component > 3)
5571 {
5572 error(functionCall->getLine(), "Component must be in the range [0;3]",
5573 name.c_str());
5574 }
5575 }
5576 }
5577 }
5578}
5579
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005580void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5581{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005582 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005583 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005584 TIntermNode *offset = nullptr;
5585 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005586 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005587 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5588 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5589 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005590 {
5591 offset = arguments->back();
5592 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005593 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005594 {
5595 // A bias parameter might follow the offset parameter.
5596 ASSERT(arguments->size() >= 3);
5597 offset = (*arguments)[2];
5598 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005599 else if (name == "textureGatherOffset")
5600 {
5601 ASSERT(arguments->size() >= 3u);
5602 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5603 ASSERT(sampler != nullptr);
5604 switch (sampler->getBasicType())
5605 {
5606 case EbtSampler2D:
5607 case EbtISampler2D:
5608 case EbtUSampler2D:
5609 case EbtSampler2DArray:
5610 case EbtISampler2DArray:
5611 case EbtUSampler2DArray:
5612 offset = (*arguments)[2];
5613 break;
5614 case EbtSampler2DShadow:
5615 case EbtSampler2DArrayShadow:
5616 offset = (*arguments)[3];
5617 break;
5618 default:
5619 UNREACHABLE();
5620 break;
5621 }
5622 useTextureGatherOffsetConstraints = true;
5623 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005624 if (offset != nullptr)
5625 {
5626 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5627 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5628 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005629 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005630 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005631 }
5632 else
5633 {
5634 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5635 size_t size = offsetConstantUnion->getType().getObjectSize();
5636 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005637 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5638 : mMinProgramTexelOffset;
5639 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5640 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005641 for (size_t i = 0u; i < size; ++i)
5642 {
5643 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005644 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005645 {
5646 std::stringstream tokenStream;
5647 tokenStream << offsetValue;
5648 std::string token = tokenStream.str();
5649 error(offset->getLine(), "Texture offset value out of valid range",
5650 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005651 }
5652 }
5653 }
5654 }
5655}
5656
Jiajia Qina3106c52017-11-03 09:39:39 +08005657void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5658{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005659 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005660 const TString &functionName = functionCall->getFunction()->name();
5661 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005662 {
5663 TIntermSequence *arguments = functionCall->getSequence();
5664 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5665
5666 if (IsBufferOrSharedVariable(memNode))
5667 {
5668 return;
5669 }
5670
5671 while (memNode->getAsBinaryNode())
5672 {
5673 memNode = memNode->getAsBinaryNode()->getLeft();
5674 if (IsBufferOrSharedVariable(memNode))
5675 {
5676 return;
5677 }
5678 }
5679
5680 error(memNode->getLine(),
5681 "The value passed to the mem argument of an atomic memory function does not "
5682 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005683 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005684 }
5685}
5686
Martin Radev2cc85b32016-08-05 16:22:53 +03005687// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5688void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5689{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005690 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005691 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005692
5693 if (name.compare(0, 5, "image") == 0)
5694 {
5695 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005696 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005697
Olli Etuaho485eefd2017-02-14 17:40:06 +00005698 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005699
5700 if (name.compare(5, 5, "Store") == 0)
5701 {
5702 if (memoryQualifier.readonly)
5703 {
5704 error(imageNode->getLine(),
5705 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005706 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005707 }
5708 }
5709 else if (name.compare(5, 4, "Load") == 0)
5710 {
5711 if (memoryQualifier.writeonly)
5712 {
5713 error(imageNode->getLine(),
5714 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005715 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005716 }
5717 }
5718 }
5719}
5720
5721// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5722void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5723 const TFunction *functionDefinition,
5724 const TIntermAggregate *functionCall)
5725{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005726 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005727
5728 const TIntermSequence &arguments = *functionCall->getSequence();
5729
5730 ASSERT(functionDefinition->getParamCount() == arguments.size());
5731
5732 for (size_t i = 0; i < arguments.size(); ++i)
5733 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005734 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5735 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005736 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5737 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5738
5739 if (IsImage(functionArgumentType.getBasicType()))
5740 {
5741 const TMemoryQualifier &functionArgumentMemoryQualifier =
5742 functionArgumentType.getMemoryQualifier();
5743 const TMemoryQualifier &functionParameterMemoryQualifier =
5744 functionParameterType.getMemoryQualifier();
5745 if (functionArgumentMemoryQualifier.readonly &&
5746 !functionParameterMemoryQualifier.readonly)
5747 {
5748 error(functionCall->getLine(),
5749 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005750 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005751 }
5752
5753 if (functionArgumentMemoryQualifier.writeonly &&
5754 !functionParameterMemoryQualifier.writeonly)
5755 {
5756 error(functionCall->getLine(),
5757 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005758 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005759 }
Martin Radev049edfa2016-11-11 14:35:37 +02005760
5761 if (functionArgumentMemoryQualifier.coherent &&
5762 !functionParameterMemoryQualifier.coherent)
5763 {
5764 error(functionCall->getLine(),
5765 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005766 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005767 }
5768
5769 if (functionArgumentMemoryQualifier.volatileQualifier &&
5770 !functionParameterMemoryQualifier.volatileQualifier)
5771 {
5772 error(functionCall->getLine(),
5773 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005774 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005775 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005776 }
5777 }
5778}
5779
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005781{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005782 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005783}
5784
5785TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005786 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005787 TIntermNode *thisNode,
5788 const TSourceLoc &loc)
5789{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005790 if (thisNode != nullptr)
5791 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005792 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005793 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005794
5795 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005796 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005797 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005798 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005799 }
5800 else
5801 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005802 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005803 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005804 }
5805}
5806
Olli Etuahobed35d72017-12-20 16:36:26 +02005807TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 TIntermSequence *arguments,
5809 TIntermNode *thisNode,
5810 const TSourceLoc &loc)
5811{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 TIntermTyped *typedThis = thisNode->getAsTyped();
5813 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5814 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5815 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005816 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005817 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005818 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005819 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 }
5821 else if (!arguments->empty())
5822 {
5823 error(loc, "method takes no parameters", "length");
5824 }
5825 else if (typedThis == nullptr || !typedThis->isArray())
5826 {
5827 error(loc, "length can only be called on arrays", "length");
5828 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005829 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5830 mGeometryShaderInputPrimitiveType == EptUndefined)
5831 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005832 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005833 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5834 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 else
5836 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005837 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5838 node->setLine(loc);
5839 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005841 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842}
5843
Olli Etuahobed35d72017-12-20 16:36:26 +02005844TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005845 TIntermSequence *arguments,
5846 const TSourceLoc &loc)
5847{
5848 // First find by unmangled name to check whether the function name has been
5849 // hidden by a variable name or struct typename.
5850 // If a function is found, check for one with a matching argument list.
5851 bool builtIn;
Olli Etuahobed35d72017-12-20 16:36:26 +02005852 const TSymbol *symbol = symbolTable.find(name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005853 if (symbol != nullptr && !symbol->isFunction())
5854 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005855 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856 }
5857 else
5858 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005859 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005860 mShaderVersion, &builtIn);
5861 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005862 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005863 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 }
5865 else
5866 {
5867 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005868 //
5869 // A declared function.
5870 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005871 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005872 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005873 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005874 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005875 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005876 if (builtIn && op != EOpNull)
5877 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005878 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005879 if (fnCandidate->getParamCount() == 1)
5880 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005882 TIntermNode *unaryParamNode = arguments->front();
5883 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005884 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005885 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005886 }
5887 else
5888 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005890 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892
5893 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005894 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305895
Olli Etuaho765924f2018-01-04 12:48:36 +02005896 // See if we can constant fold a built-in. Note that this may be possible
5897 // even if it is not const-qualified.
5898 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005899 }
5900 }
5901 else
5902 {
5903 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005904 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005906 // If builtIn == false, the function is user defined - could be an overloaded
5907 // built-in as well.
5908 // if builtIn == true, it's a builtIn function with no op associated with it.
5909 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005910 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005911 {
Olli Etuahofe486322017-03-21 09:30:54 +00005912 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005913 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005914 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005915 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005916 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005917 }
5918 else
5919 {
Olli Etuahofe486322017-03-21 09:30:54 +00005920 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005921 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005922 }
5923
Jiajia Qinbc585152017-06-23 15:42:17 +08005924 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005925
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005926 callNode->setLine(loc);
5927
5928 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005929 }
5930 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005931 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005932
5933 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005934 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005935}
5936
Jamie Madillb98c3a82015-07-23 14:26:04 -04005937TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005938 TIntermTyped *trueExpression,
5939 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005940 const TSourceLoc &loc)
5941{
Olli Etuaho56229f12017-07-10 14:16:33 +03005942 if (!checkIsScalarBool(loc, cond))
5943 {
5944 return falseExpression;
5945 }
Olli Etuaho52901742015-04-15 13:42:45 +03005946
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005947 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005948 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005949 std::stringstream reasonStream;
5950 reasonStream << "mismatching ternary operator operand types '"
5951 << trueExpression->getCompleteString() << " and '"
5952 << falseExpression->getCompleteString() << "'";
5953 std::string reason = reasonStream.str();
5954 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005955 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005956 }
Olli Etuahode318b22016-10-25 16:18:25 +01005957 if (IsOpaqueType(trueExpression->getBasicType()))
5958 {
5959 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005960 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005961 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5962 // Note that structs containing opaque types don't need to be checked as structs are
5963 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005964 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005965 return falseExpression;
5966 }
5967
Jiajia Qinbc585152017-06-23 15:42:17 +08005968 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5969 falseExpression->getMemoryQualifier().writeonly)
5970 {
5971 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5972 return falseExpression;
5973 }
5974
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005975 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005976 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005977 // ESSL 3.00.6 section 5.7:
5978 // Ternary operator support is optional for arrays. No certainty that it works across all
5979 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5980 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005981 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005982 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005983 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005984 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005985 }
Olli Etuaho94050052017-05-08 14:17:44 +03005986 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5987 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005988 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005989 return falseExpression;
5990 }
5991
Corentin Wallez0d959252016-07-12 17:26:32 -04005992 // WebGL2 section 5.26, the following results in an error:
5993 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005994 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005995 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005996 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005997 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005998 }
5999
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006000 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
6001 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02006002 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03006003}
Olli Etuaho49300862015-02-20 14:54:49 +02006004
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006005//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006006// Parse an array of strings using yyparse.
6007//
6008// Returns 0 for success.
6009//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006010int PaParseStrings(size_t count,
6011 const char *const string[],
6012 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306013 TParseContext *context)
6014{
Yunchao He4f285442017-04-21 12:15:49 +08006015 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006016 return 1;
6017
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006018 if (glslang_initialize(context))
6019 return 1;
6020
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006021 int error = glslang_scan(count, string, length, context);
6022 if (!error)
6023 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006024
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006025 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006026
alokp@chromium.org6b495712012-06-29 00:06:58 +00006027 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006028}
Jamie Madill45bcc782016-11-07 13:58:48 -05006029
6030} // namespace sh