blob: dc7ba1c371c085248a1312c4cfec35d81ac1b80e [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 Etuaho95ed1942018-02-01 14:01:19 +0200698 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200701 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 Etuaho95ed1942018-02-01 14:01:19 +0200707 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 Etuaho95ed1942018-02-01 14:01:19 +0200734 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 Etuaho95ed1942018-02-01 14:01:19 +0200741 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 Etuaho95ed1942018-02-01 14:01:19 +0200759 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300760 {
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 Etuaho95ed1942018-02-01 14:01:19 +0200769 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;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200790 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300791 {
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 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200824 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300825 {
826 error(line, "constructing matrix from matrix can only take one argument",
827 "constructor");
828 return false;
829 }
830 }
831 else
832 {
833 if (size != 1 && size < type.getObjectSize())
834 {
835 error(line, "not enough data provided for construction", "constructor");
836 return false;
837 }
838 if (overFull)
839 {
840 error(line, "too many arguments", "constructor");
841 return false;
842 }
843 }
844 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300845
Olli Etuaho8a176262016-08-16 14:23:01 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if a void variable has been declared and raise an error message for
850// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851//
852// returns true in case of an error
853//
Olli Etuaho856c4972016-08-08 11:38:39 +0300854bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400855 const TString &identifier,
856 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 if (type == EbtVoid)
859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho8a176262016-08-16 14:23:01 +0300864 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
Jamie Madillb98c3a82015-07-23 14:26:04 -0400867// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300868// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300869bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300871 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000873 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300874 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
Jamie Madillb98c3a82015-07-23 14:26:04 -0400879// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300880// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300881void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
Martin Radev4a9cd802016-09-01 16:51:51 +0300883 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000885 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
jchen10cc2a10e2017-05-03 14:05:12 +0800889bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
890 const TTypeSpecifierNonArray &pType,
891 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 if (pType.type == EbtStruct)
894 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300895 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530896 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000897 std::stringstream reasonStream;
898 reasonStream << reason << " (structure contains a sampler)";
899 std::string reasonStr = reasonStream.str();
900 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 // only samplers need to be checked from structs, since other opaque types can't be struct
904 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530906 }
jchen10cc2a10e2017-05-03 14:05:12 +0800907 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530908 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000909 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
Olli Etuaho856c4972016-08-08 11:38:39 +0300916void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
917 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400918{
919 if (pType.layoutQualifier.location != -1)
920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "location must only be specified for a single input or output variable",
922 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400923 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400924}
925
Olli Etuaho856c4972016-08-08 11:38:39 +0300926void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
927 const TLayoutQualifier &layoutQualifier)
928{
929 if (layoutQualifier.location != -1)
930 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000931 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
932 if (mShaderVersion >= 310)
933 {
934 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800935 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000936 }
937 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300938 }
939}
940
Qin Jiajiaca68d982017-09-18 16:41:56 +0800941void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
942 const TLayoutBlockStorage &blockStorage,
943 const TQualifier &qualifier)
944{
945 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
946 {
947 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
948 }
949}
950
Martin Radev2cc85b32016-08-05 16:22:53 +0300951void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
952 TQualifier qualifier,
953 const TType &type)
954{
Martin Radev2cc85b32016-08-05 16:22:53 +0300955 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800956 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530957 {
jchen10cc2a10e2017-05-03 14:05:12 +0800958 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300963unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000966
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200967 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
968 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
969 // fold as array size.
970 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000971 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000972 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400977
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 if (constant->getBasicType() == EbtUInt)
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981 }
982 else
983 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400988 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 }
994
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 {
997 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400999 }
1000
1001 // The size of arrays is restricted here to prevent issues further down the
1002 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1003 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1004 const unsigned int sizeLimit = 65536;
1005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 {
1008 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001011
1012 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001016bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1017 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018{
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 if ((elementQualifier.qualifier == EvqAttribute) ||
1020 (elementQualifier.qualifier == EvqVertexIn) ||
1021 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001022 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 TType(elementQualifier).getQualifierString());
1025 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
Olli Etuaho8a176262016-08-16 14:23:01 +03001031// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001032bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1033 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001035 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001036 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001037 error(line, "cannot declare arrays of arrays",
1038 TType(elementType).getCompleteString().c_str());
1039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001041 return true;
1042}
1043
1044// Check if this qualified element type can be formed into an array. This is only called when array
1045// brackets are associated with an identifier in a declaration, like this:
1046// float a[2];
1047// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1048// are associated with the type, like this:
1049// float[2] a;
1050bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1051 const TPublicType &elementType)
1052{
1053 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1054 {
1055 return false;
1056 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001057 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1058 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1059 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001060 // Geometry shader requires each user-defined input be declared as arrays or inside input
1061 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1062 // interface matching, such variables and blocks are treated as though they were not declared
1063 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001064 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001065 sh::IsVarying(elementType.qualifier) &&
1066 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001067 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001068 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001069 TType(elementType).getCompleteString().c_str());
1070 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001071 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001072 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001076void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1077 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001078 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho3739d232015-04-08 12:23:44 +03001080 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001081 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001082 {
1083 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001084 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001085
1086 // Generate informative error messages for ESSL1.
1087 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001088 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001089 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091 "structures containing arrays may not be declared constant since they cannot be "
1092 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001094 }
1095 else
1096 {
1097 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1098 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001100 // This will make the type sized if it isn't sized yet.
1101 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1102 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
Olli Etuaho2935c582015-04-08 14:32:06 +03001105// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106// and update the symbol table.
1107//
Olli Etuaho2935c582015-04-08 14:32:06 +03001108// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110bool TParseContext::declareVariable(const TSourceLoc &line,
1111 const TString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001112 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114{
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho195be942017-12-04 23:40:14 +02001117 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1118
Olli Etuahob60d30f2018-01-16 12:31:06 +02001119 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001120
Olli Etuaho856c4972016-08-08 11:38:39 +03001121 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
Olli Etuaho2935c582015-04-08 14:32:06 +03001123 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahob60d30f2018-01-16 12:31:06 +02001124 if (type->isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1127 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001128 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001129 {
1130 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1131 identifier.c_str());
1132 return false;
1133 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001134 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001135 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001137 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001139 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 }
1141 }
1142 else
1143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1145 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
Olli Etuaho195be942017-12-04 23:40:14 +02001153 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 {
1155 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 return false;
1157 }
1158
Olli Etuahob60d30f2018-01-16 12:31:06 +02001159 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001160 return false;
1161
1162 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163}
1164
Martin Radev70866b82016-07-22 15:27:42 +03001165void TParseContext::checkIsParameterQualifierValid(
1166 const TSourceLoc &line,
1167 const TTypeQualifierBuilder &typeQualifierBuilder,
1168 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301169{
Olli Etuahocce89652017-06-19 16:04:09 +03001170 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001171 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001172
1173 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301174 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001175 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1176 }
1177
1178 if (!IsImage(type->getBasicType()))
1179 {
Olli Etuaho43364892017-02-13 16:00:12 +00001180 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001181 }
1182 else
1183 {
1184 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
Martin Radev70866b82016-07-22 15:27:42 +03001187 type->setQualifier(typeQualifier.qualifier);
1188
1189 if (typeQualifier.precision != EbpUndefined)
1190 {
1191 type->setPrecision(typeQualifier.precision);
1192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
Olli Etuaho703671e2017-11-08 17:47:18 +02001195template <size_t size>
1196bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1197 const std::array<TExtension, size> &extensions)
1198{
1199 ASSERT(!extensions.empty());
1200 const TExtensionBehavior &extBehavior = extensionBehavior();
1201
1202 bool canUseWithWarning = false;
1203 bool canUseWithoutWarning = false;
1204
1205 const char *errorMsgString = "";
1206 TExtension errorMsgExtension = TExtension::UNDEFINED;
1207
1208 for (TExtension extension : extensions)
1209 {
1210 auto extIter = extBehavior.find(extension);
1211 if (canUseWithWarning)
1212 {
1213 // We already have an extension that we can use, but with a warning.
1214 // See if we can use the alternative extension without a warning.
1215 if (extIter == extBehavior.end())
1216 {
1217 continue;
1218 }
1219 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1220 {
1221 canUseWithoutWarning = true;
1222 break;
1223 }
1224 continue;
1225 }
1226 if (extIter == extBehavior.end())
1227 {
1228 errorMsgString = "extension is not supported";
1229 errorMsgExtension = extension;
1230 }
1231 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1232 {
1233 errorMsgString = "extension is disabled";
1234 errorMsgExtension = extension;
1235 }
1236 else if (extIter->second == EBhWarn)
1237 {
1238 errorMsgExtension = extension;
1239 canUseWithWarning = true;
1240 }
1241 else
1242 {
1243 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1244 canUseWithoutWarning = true;
1245 break;
1246 }
1247 }
1248
1249 if (canUseWithoutWarning)
1250 {
1251 return true;
1252 }
1253 if (canUseWithWarning)
1254 {
1255 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1256 return true;
1257 }
1258 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1259 return false;
1260}
1261
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 1> &extensions);
1265template bool TParseContext::checkCanUseOneOfExtensions(
1266 const TSourceLoc &line,
1267 const std::array<TExtension, 2> &extensions);
1268template bool TParseContext::checkCanUseOneOfExtensions(
1269 const TSourceLoc &line,
1270 const std::array<TExtension, 3> &extensions);
1271
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001272bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001273{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001274 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001275 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276}
1277
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001278// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1279// compile-time or link-time errors are the same whether or not the declaration is empty".
1280// This function implements all the checks that are done on qualifiers regardless of if the
1281// declaration is empty.
1282void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1283 const sh::TLayoutQualifier &layoutQualifier,
1284 const TSourceLoc &location)
1285{
1286 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1287 {
1288 error(location, "Shared memory declarations cannot have layout specified", "layout");
1289 }
1290
1291 if (layoutQualifier.matrixPacking != EmpUnspecified)
1292 {
1293 error(location, "layout qualifier only valid for interface blocks",
1294 getMatrixPackingString(layoutQualifier.matrixPacking));
1295 return;
1296 }
1297
1298 if (layoutQualifier.blockStorage != EbsUnspecified)
1299 {
1300 error(location, "layout qualifier only valid for interface blocks",
1301 getBlockStorageString(layoutQualifier.blockStorage));
1302 return;
1303 }
1304
1305 if (qualifier == EvqFragmentOut)
1306 {
1307 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1308 {
1309 error(location, "invalid layout qualifier combination", "yuv");
1310 return;
1311 }
1312 }
1313 else
1314 {
1315 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1316 }
1317
Olli Etuaho95468d12017-05-04 11:14:34 +03001318 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1319 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001320 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1321 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001322 {
1323 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1324 }
1325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001329 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001330 // We're not checking whether the uniform location is in range here since that depends on
1331 // the type of the variable.
1332 // The type can only be fully determined for non-empty declarations.
1333 }
1334 if (!canHaveLocation)
1335 {
1336 checkLocationIsNotSpecified(location, layoutQualifier);
1337 }
1338}
1339
jchen104cdac9e2017-05-08 11:01:20 +08001340void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1341 const TSourceLoc &location)
1342{
1343 if (publicType.precision != EbpHigh)
1344 {
1345 error(location, "Can only be highp", "atomic counter");
1346 }
1347 // dEQP enforces compile error if location is specified. See uniform_location.test.
1348 if (publicType.layoutQualifier.location != -1)
1349 {
1350 error(location, "location must not be set for atomic_uint", "layout");
1351 }
1352 if (publicType.layoutQualifier.binding == -1)
1353 {
1354 error(location, "no binding specified", "atomic counter");
1355 }
1356}
1357
Olli Etuaho55bde912017-10-25 13:41:13 +03001358void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001359{
Olli Etuaho55bde912017-10-25 13:41:13 +03001360 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001361 {
1362 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1363 // error. It is assumed that this applies to empty declarations as well.
1364 error(location, "empty array declaration needs to specify a size", "");
1365 }
Martin Radevb8b01222016-11-20 23:25:53 +02001366}
1367
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001368// These checks are done for all declarations that are non-empty. They're done for non-empty
1369// declarations starting a declarator list, and declarators that follow an empty declaration.
1370void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1371 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001372{
Olli Etuahofa33d582015-04-09 14:33:12 +03001373 switch (publicType.qualifier)
1374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375 case EvqVaryingIn:
1376 case EvqVaryingOut:
1377 case EvqAttribute:
1378 case EvqVertexIn:
1379 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001380 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001381 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001382 {
1383 error(identifierLocation, "cannot be used with a structure",
1384 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001385 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001386 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001387 break;
1388 case EvqBuffer:
1389 if (publicType.getBasicType() != EbtInterfaceBlock)
1390 {
1391 error(identifierLocation,
1392 "cannot declare buffer variables at global scope(outside a block)",
1393 getQualifierString(publicType.qualifier));
1394 return;
1395 }
1396 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 default:
1398 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001399 }
jchen10cc2a10e2017-05-03 14:05:12 +08001400 std::string reason(getBasicString(publicType.getBasicType()));
1401 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001402 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001403 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001404 {
1405 return;
1406 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001407
Andrei Volykhina5527072017-03-22 16:46:30 +03001408 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1409 publicType.qualifier != EvqConst) &&
1410 publicType.getBasicType() == EbtYuvCscStandardEXT)
1411 {
1412 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1413 getQualifierString(publicType.qualifier));
1414 return;
1415 }
1416
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1418 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001419 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1420 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001421 TType type(publicType);
1422 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001424 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1425 publicType.layoutQualifier);
1426 }
1427 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001428
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001429 // check for layout qualifier issues
1430 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001431
Martin Radev2cc85b32016-08-05 16:22:53 +03001432 if (IsImage(publicType.getBasicType()))
1433 {
1434
1435 switch (layoutQualifier.imageInternalFormat)
1436 {
1437 case EiifRGBA32F:
1438 case EiifRGBA16F:
1439 case EiifR32F:
1440 case EiifRGBA8:
1441 case EiifRGBA8_SNORM:
1442 if (!IsFloatImage(publicType.getBasicType()))
1443 {
1444 error(identifierLocation,
1445 "internal image format requires a floating image type",
1446 getBasicString(publicType.getBasicType()));
1447 return;
1448 }
1449 break;
1450 case EiifRGBA32I:
1451 case EiifRGBA16I:
1452 case EiifRGBA8I:
1453 case EiifR32I:
1454 if (!IsIntegerImage(publicType.getBasicType()))
1455 {
1456 error(identifierLocation,
1457 "internal image format requires an integer image type",
1458 getBasicString(publicType.getBasicType()));
1459 return;
1460 }
1461 break;
1462 case EiifRGBA32UI:
1463 case EiifRGBA16UI:
1464 case EiifRGBA8UI:
1465 case EiifR32UI:
1466 if (!IsUnsignedImage(publicType.getBasicType()))
1467 {
1468 error(identifierLocation,
1469 "internal image format requires an unsigned image type",
1470 getBasicString(publicType.getBasicType()));
1471 return;
1472 }
1473 break;
1474 case EiifUnspecified:
1475 error(identifierLocation, "layout qualifier", "No image internal format specified");
1476 return;
1477 default:
1478 error(identifierLocation, "layout qualifier", "unrecognized token");
1479 return;
1480 }
1481
1482 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1483 switch (layoutQualifier.imageInternalFormat)
1484 {
1485 case EiifR32F:
1486 case EiifR32I:
1487 case EiifR32UI:
1488 break;
1489 default:
1490 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1491 {
1492 error(identifierLocation, "layout qualifier",
1493 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1494 "image variables must be qualified readonly and/or writeonly");
1495 return;
1496 }
1497 break;
1498 }
1499 }
1500 else
1501 {
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001503 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1504 }
jchen104cdac9e2017-05-08 11:01:20 +08001505
1506 if (IsAtomicCounter(publicType.getBasicType()))
1507 {
1508 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1509 }
1510 else
1511 {
1512 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1513 }
Olli Etuaho43364892017-02-13 16:00:12 +00001514}
Martin Radev2cc85b32016-08-05 16:22:53 +03001515
Olli Etuaho43364892017-02-13 16:00:12 +00001516void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1517{
1518 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1520 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1521 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1522 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1523 // when it comes to which shaders are accepted by the compiler.
1524 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001525 if (IsImage(type.getBasicType()))
1526 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001527 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1528 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001529 }
1530 else if (IsSampler(type.getBasicType()))
1531 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1533 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001534 }
jchen104cdac9e2017-05-08 11:01:20 +08001535 else if (IsAtomicCounter(type.getBasicType()))
1536 {
1537 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1538 }
Olli Etuaho43364892017-02-13 16:00:12 +00001539 else
1540 {
1541 ASSERT(!IsOpaqueType(type.getBasicType()));
1542 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001543 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001544}
1545
Olli Etuaho856c4972016-08-08 11:38:39 +03001546void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1547 const TString &layoutQualifierName,
1548 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001549{
1550
1551 if (mShaderVersion < versionRequired)
1552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001553 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001554 }
1555}
1556
Olli Etuaho856c4972016-08-08 11:38:39 +03001557bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1558 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001559{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001560 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001561 for (size_t i = 0u; i < localSize.size(); ++i)
1562 {
1563 if (localSize[i] != -1)
1564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001565 error(location,
1566 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1567 "global layout declaration",
1568 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001569 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001570 }
1571 }
1572
Olli Etuaho8a176262016-08-16 14:23:01 +03001573 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001574}
1575
Olli Etuaho43364892017-02-13 16:00:12 +00001576void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001577 TLayoutImageInternalFormat internalFormat)
1578{
1579 if (internalFormat != EiifUnspecified)
1580 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001581 error(location, "invalid layout qualifier: only valid when used with images",
1582 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001583 }
Olli Etuaho43364892017-02-13 16:00:12 +00001584}
1585
1586void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1587{
1588 if (binding != -1)
1589 {
1590 error(location,
1591 "invalid layout qualifier: only valid when used with opaque types or blocks",
1592 "binding");
1593 }
1594}
1595
jchen104cdac9e2017-05-08 11:01:20 +08001596void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1597{
1598 if (offset != -1)
1599 {
1600 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1601 "offset");
1602 }
1603}
1604
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001605void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1606 int binding,
1607 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001608{
1609 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001611 {
1612 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1613 }
1614}
1615
1616void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1617 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001619{
1620 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001621 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001622 {
1623 error(location, "sampler binding greater than maximum texture units", "binding");
1624 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001625}
1626
Jiajia Qinbc585152017-06-23 15:42:17 +08001627void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1628 const TQualifier &qualifier,
1629 int binding,
1630 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001631{
1632 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001635 if (binding + size > mMaxUniformBufferBindings)
1636 {
1637 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1638 "binding");
1639 }
1640 }
1641 else if (qualifier == EvqBuffer)
1642 {
1643 if (binding + size > mMaxShaderStorageBufferBindings)
1644 {
1645 error(location,
1646 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1647 "binding");
1648 }
jchen10af713a22017-04-19 09:10:56 +08001649 }
1650}
jchen104cdac9e2017-05-08 11:01:20 +08001651void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1652{
1653 if (binding >= mMaxAtomicCounterBindings)
1654 {
1655 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1656 "binding");
1657 }
1658}
jchen10af713a22017-04-19 09:10:56 +08001659
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001660void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1661 int objectLocationCount,
1662 const TLayoutQualifier &layoutQualifier)
1663{
1664 int loc = layoutQualifier.location;
1665 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1666 {
1667 error(location, "Uniform location out of range", "location");
1668 }
1669}
1670
Andrei Volykhina5527072017-03-22 16:46:30 +03001671void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1672{
1673 if (yuv != false)
1674 {
1675 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1676 }
1677}
1678
Jiajia Qinbc585152017-06-23 15:42:17 +08001679void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1680 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001681{
1682 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1683 {
1684 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001685 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1686 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1687 qual == EvqInOut || qual == EvqConstReadOnly))
1688 {
1689 if (argument->getMemoryQualifier().writeonly)
1690 {
1691 error(argument->getLine(),
1692 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001693 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001694 return;
1695 }
1696 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 if (qual == EvqOut || qual == EvqInOut)
1698 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001699 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001701 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001702 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001703 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001704 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001705 }
1706 }
1707 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001708}
1709
Martin Radev70866b82016-07-22 15:27:42 +03001710void TParseContext::checkInvariantVariableQualifier(bool invariant,
1711 const TQualifier qualifier,
1712 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001713{
Martin Radev70866b82016-07-22 15:27:42 +03001714 if (!invariant)
1715 return;
1716
1717 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001718 {
Martin Radev70866b82016-07-22 15:27:42 +03001719 // input variables in the fragment shader can be also qualified as invariant
1720 if (!sh::CanBeInvariantESSL1(qualifier))
1721 {
1722 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1723 }
1724 }
1725 else
1726 {
1727 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1728 {
1729 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1730 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731 }
1732}
1733
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001735{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001736 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001737}
1738
Jamie Madillb98c3a82015-07-23 14:26:04 -04001739void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1740 const char *extName,
1741 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001742{
1743 pp::SourceLocation srcLoc;
1744 srcLoc.file = loc.first_file;
1745 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001746 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001747}
1748
Jamie Madillb98c3a82015-07-23 14:26:04 -04001749void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1750 const char *name,
1751 const char *value,
1752 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001753{
1754 pp::SourceLocation srcLoc;
1755 srcLoc.file = loc.first_file;
1756 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001757 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001758}
1759
Martin Radev4c4c8e72016-08-04 12:25:34 +03001760sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001761{
Jamie Madill2f294c92017-11-20 14:47:26 -05001762 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001763 for (size_t i = 0u; i < result.size(); ++i)
1764 {
1765 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1766 {
1767 result[i] = 1;
1768 }
1769 else
1770 {
1771 result[i] = mComputeShaderLocalSize[i];
1772 }
1773 }
1774 return result;
1775}
1776
Olli Etuaho56229f12017-07-10 14:16:33 +03001777TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1778 const TSourceLoc &line)
1779{
1780 TIntermConstantUnion *node = new TIntermConstantUnion(
1781 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1782 node->setLine(line);
1783 return node;
1784}
1785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786/////////////////////////////////////////////////////////////////////////////////
1787//
1788// Non-Errors.
1789//
1790/////////////////////////////////////////////////////////////////////////////////
1791
Jamie Madill5c097022014-08-20 16:38:32 -04001792const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1793 const TString *name,
1794 const TSymbol *symbol)
1795{
Jamie Madill5c097022014-08-20 16:38:32 -04001796 if (!symbol)
1797 {
1798 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001799 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001800 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001801
1802 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
1804 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001805 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001806 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001807
1808 const TVariable *variable = static_cast<const TVariable *>(symbol);
1809
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001810 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001811 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001812 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001813 }
1814
Olli Etuaho0f684632017-07-13 12:42:15 +03001815 // Reject shaders using both gl_FragData and gl_FragColor
1816 TQualifier qualifier = variable->getType().getQualifier();
1817 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001818 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 mUsesFragData = true;
1820 }
1821 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1822 {
1823 mUsesFragColor = true;
1824 }
1825 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1826 {
1827 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001828 }
1829
Olli Etuaho0f684632017-07-13 12:42:15 +03001830 // This validation is not quite correct - it's only an error to write to
1831 // both FragData and FragColor. For simplicity, and because users shouldn't
1832 // be rewarded for reading from undefined varaibles, return an error
1833 // if they are both referenced, rather than assigned.
1834 if (mUsesFragData && mUsesFragColor)
1835 {
1836 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1837 if (mUsesSecondaryOutputs)
1838 {
1839 errorMessage =
1840 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1841 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1842 }
1843 error(location, errorMessage, name->c_str());
1844 }
1845
1846 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1847 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1848 qualifier == EvqWorkGroupSize)
1849 {
1850 error(location,
1851 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1852 "gl_WorkGroupSize");
1853 }
Jamie Madill5c097022014-08-20 16:38:32 -04001854 return variable;
1855}
1856
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001857TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1858 const TString *name,
1859 const TSymbol *symbol)
1860{
1861 const TVariable *variable = getNamedVariable(location, name, symbol);
1862
Olli Etuaho0f684632017-07-13 12:42:15 +03001863 if (!variable)
1864 {
1865 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1866 node->setLine(location);
1867 return node;
1868 }
1869
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001871 TIntermTyped *node = nullptr;
1872
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001873 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001874 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001875 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001879 {
1880 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1881 // needs to be added to the AST as a constant and not as a symbol.
1882 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1883 TConstantUnion *constArray = new TConstantUnion[3];
1884 for (size_t i = 0; i < 3; ++i)
1885 {
1886 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1887 }
1888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 ASSERT(variableType.getBasicType() == EbtUInt);
1890 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001894 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001895 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1897 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001898 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001899 ASSERT(mGlInVariableWithArraySize != nullptr);
1900 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Olli Etuaho195be942017-12-04 23:40:14 +02001904 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001917 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuahob60d30f2018-01-16 12:31:06 +02001924 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001926 // In case initializer is not an array or type has more dimensions than initializer, this
1927 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1928 // actually is an array or not. Having a non-array initializer for an unsized array will
1929 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001930 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001931 type->sizeUnsizedArrays(arraySizes);
1932 }
1933
1934 const TQualifier qualifier = type->getQualifier();
1935
1936 bool constError = false;
1937 if (qualifier == EvqConst)
1938 {
1939 if (EvqConst != initializer->getType().getQualifier())
1940 {
1941 std::stringstream reasonStream;
1942 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1943 std::string reason = reasonStream.str();
1944 error(line, reason.c_str(), "=");
1945
1946 // We're still going to declare the variable to avoid extra error messages.
1947 type->setQualifier(EvqTemporary);
1948 constError = true;
1949 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001950 }
Olli Etuaho195be942017-12-04 23:40:14 +02001951
1952 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001953 if (!declareVariable(line, identifier, type, &variable))
1954 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001955 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
Olli Etuahob60d30f2018-01-16 12:31:06 +02001958 if (constError)
1959 {
1960 return false;
1961 }
1962
Olli Etuahob0c645e2015-05-12 14:25:36 +03001963 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001965 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001966 {
1967 // Error message does not completely match behavior with ESSL 1.00, but
1968 // we want to steer developers towards only using constant expressions.
1969 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001970 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001971 }
1972 if (globalInitWarning)
1973 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001974 warning(
1975 line,
1976 "global variable initializers should be constant expressions "
1977 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1978 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001979 }
1980
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301982 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1983 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001984 error(line, " cannot initialize this type of qualifier ",
1985 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001986 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001987 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001988
1989 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1990 intermSymbol->setLine(line);
1991
1992 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1993 {
1994 assignError(line, "=", variable->getType().getCompleteString(),
1995 initializer->getCompleteString());
1996 return false;
1997 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998
Arun Patole7e7e68d2015-05-22 12:02:25 +05301999 if (qualifier == EvqConst)
2000 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002001 // Save the constant folded value to the variable if possible.
2002 const TConstantUnion *constArray = initializer->getConstantValue();
2003 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302004 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002005 variable->shareConstPointer(constArray);
2006 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002007 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002008 ASSERT(*initNode == nullptr);
2009 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002010 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002011 }
2012 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002013
Olli Etuahob60d30f2018-01-16 12:31:06 +02002014 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
2015 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002016 return true;
2017}
2018
2019TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2020 const TString &identifier,
2021 TIntermTyped *initializer,
2022 const TSourceLoc &loc)
2023{
2024 checkIsScalarBool(loc, pType);
2025 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002026 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002027 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002028 {
2029 // The initializer is valid. The init condition needs to have a node - either the
2030 // initializer node, or a constant node in case the initialized variable is const and won't
2031 // be recorded in the AST.
2032 if (initNode == nullptr)
2033 {
2034 return initializer;
2035 }
2036 else
2037 {
2038 TIntermDeclaration *declaration = new TIntermDeclaration();
2039 declaration->appendDeclarator(initNode);
2040 return declaration;
2041 }
2042 }
2043 return nullptr;
2044}
2045
2046TIntermNode *TParseContext::addLoop(TLoopType type,
2047 TIntermNode *init,
2048 TIntermNode *cond,
2049 TIntermTyped *expr,
2050 TIntermNode *body,
2051 const TSourceLoc &line)
2052{
2053 TIntermNode *node = nullptr;
2054 TIntermTyped *typedCond = nullptr;
2055 if (cond)
2056 {
2057 typedCond = cond->getAsTyped();
2058 }
2059 if (cond == nullptr || typedCond)
2060 {
Olli Etuahocce89652017-06-19 16:04:09 +03002061 if (type == ELoopDoWhile)
2062 {
2063 checkIsScalarBool(line, typedCond);
2064 }
2065 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2066 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2067 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2068 !typedCond->isVector()));
2069
Olli Etuaho3ec75682017-07-05 17:02:55 +03002070 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002071 node->setLine(line);
2072 return node;
2073 }
2074
Olli Etuahocce89652017-06-19 16:04:09 +03002075 ASSERT(type != ELoopDoWhile);
2076
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2078 ASSERT(declaration);
2079 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2080 ASSERT(declarator->getLeft()->getAsSymbolNode());
2081
2082 // The condition is a declaration. In the AST representation we don't support declarations as
2083 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2084 // the loop.
2085 TIntermBlock *block = new TIntermBlock();
2086
2087 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2088 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2089 block->appendStatement(declareCondition);
2090
2091 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2092 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002093 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002094 block->appendStatement(loop);
2095 loop->setLine(line);
2096 block->setLine(line);
2097 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098}
2099
Olli Etuahocce89652017-06-19 16:04:09 +03002100TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2101 TIntermNodePair code,
2102 const TSourceLoc &loc)
2103{
Olli Etuaho56229f12017-07-10 14:16:33 +03002104 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002105
2106 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002107 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002108 {
2109 if (cond->getAsConstantUnion()->getBConst(0) == true)
2110 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002111 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002112 }
2113 else
2114 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002115 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002116 }
2117 }
2118
Olli Etuaho3ec75682017-07-05 17:02:55 +03002119 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002120 node->setLine(loc);
2121
2122 return node;
2123}
2124
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002125void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2126{
2127 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2128 typeSpecifier->getBasicType());
2129
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002130 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002131 {
2132 error(typeSpecifier->getLine(), "not supported", "first-class array");
2133 typeSpecifier->clearArrayness();
2134 }
2135}
2136
Martin Radev70866b82016-07-22 15:27:42 +03002137TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302138 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002139{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002140 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002141
Martin Radev70866b82016-07-22 15:27:42 +03002142 TPublicType returnType = typeSpecifier;
2143 returnType.qualifier = typeQualifier.qualifier;
2144 returnType.invariant = typeQualifier.invariant;
2145 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002146 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002147 returnType.precision = typeSpecifier.precision;
2148
2149 if (typeQualifier.precision != EbpUndefined)
2150 {
2151 returnType.precision = typeQualifier.precision;
2152 }
2153
Martin Radev4a9cd802016-09-01 16:51:51 +03002154 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2155 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002156
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2158 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002161
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002162 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002163 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002164 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002165 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002167 returnType.clearArrayness();
2168 }
2169
Martin Radev70866b82016-07-22 15:27:42 +03002170 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002172 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002174 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002175 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002176
Martin Radev70866b82016-07-22 15:27:42 +03002177 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002179 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002181 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002182 }
2183 }
2184 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002185 {
Martin Radev70866b82016-07-22 15:27:42 +03002186 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002187 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002188 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002189 }
Martin Radev70866b82016-07-22 15:27:42 +03002190 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2191 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002192 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2194 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 }
Martin Radev70866b82016-07-22 15:27:42 +03002196 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002197 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002199 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002200 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002201 }
2202
2203 return returnType;
2204}
2205
Olli Etuaho856c4972016-08-08 11:38:39 +03002206void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2207 const TPublicType &type,
2208 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002209{
2210 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002212 {
2213 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002214 }
2215
2216 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2217 switch (qualifier)
2218 {
2219 case EvqVertexIn:
2220 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002221 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002222 {
2223 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002225 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 return;
2227 case EvqFragmentOut:
2228 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002229 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 {
2231 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002233 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 return;
2235 default:
2236 break;
2237 }
2238
2239 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2240 // restrictions.
2241 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002242 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2243 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2245 {
2246 error(qualifierLocation, "must use 'flat' interpolation here",
2247 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002248 }
2249
Martin Radev4a9cd802016-09-01 16:51:51 +03002250 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 {
2252 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2253 // These restrictions are only implied by the ESSL 3.00 spec, but
2254 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002255 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 {
2257 error(qualifierLocation, "cannot be an array of structures",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260 if (type.isStructureContainingArrays())
2261 {
2262 error(qualifierLocation, "cannot be a structure containing an array",
2263 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 }
2265 if (type.isStructureContainingType(EbtStruct))
2266 {
2267 error(qualifierLocation, "cannot be a structure containing a structure",
2268 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 }
2270 if (type.isStructureContainingType(EbtBool))
2271 {
2272 error(qualifierLocation, "cannot be a structure containing a bool",
2273 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002274 }
2275 }
2276}
2277
Martin Radev2cc85b32016-08-05 16:22:53 +03002278void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2279{
2280 if (qualifier.getType() == QtStorage)
2281 {
2282 const TStorageQualifierWrapper &storageQualifier =
2283 static_cast<const TStorageQualifierWrapper &>(qualifier);
2284 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2285 !symbolTable.atGlobalLevel())
2286 {
2287 error(storageQualifier.getLine(),
2288 "Local variables can only use the const storage qualifier.",
2289 storageQualifier.getQualifierString().c_str());
2290 }
2291 }
2292}
2293
Olli Etuaho43364892017-02-13 16:00:12 +00002294void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002295 const TSourceLoc &location)
2296{
Jiajia Qinbc585152017-06-23 15:42:17 +08002297 const std::string reason(
2298 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2299 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002300 if (memoryQualifier.readonly)
2301 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002302 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 }
2304 if (memoryQualifier.writeonly)
2305 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002306 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002307 }
Martin Radev049edfa2016-11-11 14:35:37 +02002308 if (memoryQualifier.coherent)
2309 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002311 }
2312 if (memoryQualifier.restrictQualifier)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002315 }
2316 if (memoryQualifier.volatileQualifier)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002319 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002320}
2321
jchen104cdac9e2017-05-08 11:01:20 +08002322// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2323// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002324void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2325 const TSourceLoc &loc,
2326 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002327{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002328 if (!IsAtomicCounter(type->getBasicType()))
2329 {
2330 return;
2331 }
2332
2333 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2334 : kAtomicCounterSize;
2335 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2336 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002337 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002338 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002339 {
2340 offset = bindingState.appendSpan(size);
2341 }
2342 else
2343 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002344 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002345 }
2346 if (offset == -1)
2347 {
2348 error(loc, "Offset overlapping", "atomic counter");
2349 return;
2350 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002351 layoutQualifier.offset = offset;
2352 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002353}
2354
Olli Etuaho454c34c2017-10-25 16:35:56 +03002355void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2356 const char *token,
2357 TType *type)
2358{
2359 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2360 {
2361 if (type->isArray() && type->getOutermostArraySize() == 0u)
2362 {
2363 // Set size for the unsized geometry shader inputs if they are declared after a valid
2364 // input primitive declaration.
2365 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2366 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002367 ASSERT(mGlInVariableWithArraySize != nullptr);
2368 type->sizeOutermostUnsizedArray(
2369 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002370 }
2371 else
2372 {
2373 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2374 // An input can be declared without an array size if there is a previous layout
2375 // which specifies the size.
2376 error(location,
2377 "Missing a valid input primitive declaration before declaring an unsized "
2378 "array input",
2379 token);
2380 }
2381 }
2382 else if (type->isArray())
2383 {
2384 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2385 }
2386 else
2387 {
2388 error(location, "Geometry shader input variable must be declared as an array", token);
2389 }
2390 }
2391}
2392
Olli Etuaho13389b62016-10-16 11:48:18 +01002393TIntermDeclaration *TParseContext::parseSingleDeclaration(
2394 TPublicType &publicType,
2395 const TSourceLoc &identifierOrTypeLocation,
2396 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002397{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002398 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002399 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2400 mDirectiveHandler.pragma().stdgl.invariantAll)
2401 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002402 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002403
2404 // The directive handler has already taken care of rejecting invalid uses of this pragma
2405 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2406 // affected variable declarations:
2407 //
2408 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2409 // elsewhere, in TranslatorGLSL.)
2410 //
2411 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2412 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2413 // the way this is currently implemented we have to enable this compiler option before
2414 // parsing the shader and determining the shading language version it uses. If this were
2415 // implemented as a post-pass, the workaround could be more targeted.
2416 //
2417 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2418 // the specification, but there are desktop OpenGL drivers that expect that this is the
2419 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2420 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2421 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002422 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002423 }
2424 }
2425
Olli Etuahob60d30f2018-01-16 12:31:06 +02002426 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002428 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2429 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002430
Olli Etuahobab4c082015-04-24 16:38:49 +03002431 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002432 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002433
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002434 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002435 if (emptyDeclaration)
2436 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002437 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 // In most cases we don't need to create a symbol node for an empty declaration.
2439 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002440 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002441 {
Olli Etuaho195be942017-12-04 23:40:14 +02002442 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002443 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002444 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 }
jchen104cdac9e2017-05-08 11:01:20 +08002446 else if (IsAtomicCounter(publicType.getBasicType()))
2447 {
2448 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2449 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002450 }
2451 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002452 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002454
Olli Etuahob60d30f2018-01-16 12:31:06 +02002455 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuahob60d30f2018-01-16 12:31:06 +02002457 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002458
Olli Etuaho2935c582015-04-08 14:32:06 +03002459 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002460 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002461 {
Olli Etuaho195be942017-12-04 23:40:14 +02002462 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002463 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002464 }
2465
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002466 TIntermDeclaration *declaration = new TIntermDeclaration();
2467 declaration->setLine(identifierOrTypeLocation);
2468 if (symbol)
2469 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002470 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 declaration->appendDeclarator(symbol);
2472 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002474}
2475
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002476TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2477 TPublicType &elementType,
2478 const TSourceLoc &identifierLocation,
2479 const TString &identifier,
2480 const TSourceLoc &indexLocation,
2481 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002482{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002483 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002484
Olli Etuaho55bde912017-10-25 13:41:13 +03002485 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002486 identifierLocation);
2487
Olli Etuaho55bde912017-10-25 13:41:13 +03002488 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002491
Olli Etuahob60d30f2018-01-16 12:31:06 +02002492 TType *arrayType = new TType(elementType);
2493 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002494
Olli Etuahob60d30f2018-01-16 12:31:06 +02002495 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002496
Olli Etuahob60d30f2018-01-16 12:31:06 +02002497 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002498
Olli Etuahob60d30f2018-01-16 12:31:06 +02002499 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002500
Olli Etuaho13389b62016-10-16 11:48:18 +01002501 TIntermDeclaration *declaration = new TIntermDeclaration();
2502 declaration->setLine(identifierLocation);
2503
Olli Etuaho195be942017-12-04 23:40:14 +02002504 TVariable *variable = nullptr;
2505 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 {
Olli Etuaho195be942017-12-04 23:40:14 +02002507 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002508 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 declaration->appendDeclarator(symbol);
2510 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002511
Olli Etuaho13389b62016-10-16 11:48:18 +01002512 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002513}
2514
Olli Etuaho13389b62016-10-16 11:48:18 +01002515TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2516 const TSourceLoc &identifierLocation,
2517 const TString &identifier,
2518 const TSourceLoc &initLocation,
2519 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002520{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002522
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2524 identifierLocation);
2525
2526 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002527
Olli Etuaho13389b62016-10-16 11:48:18 +01002528 TIntermDeclaration *declaration = new TIntermDeclaration();
2529 declaration->setLine(identifierLocation);
2530
2531 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002532 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002533 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002534 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002535 if (initNode)
2536 {
2537 declaration->appendDeclarator(initNode);
2538 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002539 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002541}
2542
Olli Etuaho13389b62016-10-16 11:48:18 +01002543TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002544 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002545 const TSourceLoc &identifierLocation,
2546 const TString &identifier,
2547 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002548 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002549 const TSourceLoc &initLocation,
2550 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002551{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002552 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553
Olli Etuaho55bde912017-10-25 13:41:13 +03002554 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002555 identifierLocation);
2556
Olli Etuaho55bde912017-10-25 13:41:13 +03002557 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuahob60d30f2018-01-16 12:31:06 +02002561 TType *arrayType = new TType(elementType);
2562 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563
Olli Etuaho13389b62016-10-16 11:48:18 +01002564 TIntermDeclaration *declaration = new TIntermDeclaration();
2565 declaration->setLine(identifierLocation);
2566
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002568 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002569 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 if (initNode)
2572 {
2573 declaration->appendDeclarator(initNode);
2574 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002576
2577 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002578}
2579
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002580TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002581 const TTypeQualifierBuilder &typeQualifierBuilder,
2582 const TSourceLoc &identifierLoc,
2583 const TString *identifier,
2584 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002585{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002586 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002587
Martin Radev70866b82016-07-22 15:27:42 +03002588 if (!typeQualifier.invariant)
2589 {
2590 error(identifierLoc, "Expected invariant", identifier->c_str());
2591 return nullptr;
2592 }
2593 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2594 {
2595 return nullptr;
2596 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597 if (!symbol)
2598 {
2599 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002600 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601 }
Martin Radev70866b82016-07-22 15:27:42 +03002602 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603 {
Martin Radev70866b82016-07-22 15:27:42 +03002604 error(identifierLoc, "invariant declaration specifies qualifier",
2605 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606 }
Martin Radev70866b82016-07-22 15:27:42 +03002607 if (typeQualifier.precision != EbpUndefined)
2608 {
2609 error(identifierLoc, "invariant declaration specifies precision",
2610 getPrecisionString(typeQualifier.precision));
2611 }
2612 if (!typeQualifier.layoutQualifier.isEmpty())
2613 {
2614 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2615 }
2616
2617 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002618 if (!variable)
2619 {
2620 return nullptr;
2621 }
Martin Radev70866b82016-07-22 15:27:42 +03002622 const TType &type = variable->getType();
2623
2624 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2625 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002626 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002627
2628 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2629
Olli Etuaho195be942017-12-04 23:40:14 +02002630 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002631 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002632
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002633 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002634}
2635
Olli Etuaho13389b62016-10-16 11:48:18 +01002636void TParseContext::parseDeclarator(TPublicType &publicType,
2637 const TSourceLoc &identifierLocation,
2638 const TString &identifier,
2639 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002640{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002641 // If the declaration starting this declarator list was empty (example: int,), some checks were
2642 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002643 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002644 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002645 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2646 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002647 }
2648
Olli Etuaho856c4972016-08-08 11:38:39 +03002649 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002650
Olli Etuahob60d30f2018-01-16 12:31:06 +02002651 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002652
Olli Etuahob60d30f2018-01-16 12:31:06 +02002653 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002654
Olli Etuahob60d30f2018-01-16 12:31:06 +02002655 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002656
Olli Etuahob60d30f2018-01-16 12:31:06 +02002657 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002658
Olli Etuaho195be942017-12-04 23:40:14 +02002659 TVariable *variable = nullptr;
2660 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002661 {
Olli Etuaho195be942017-12-04 23:40:14 +02002662 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002663 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002664 declarationOut->appendDeclarator(symbol);
2665 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002666}
2667
Olli Etuaho55bde912017-10-25 13:41:13 +03002668void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 const TSourceLoc &identifierLocation,
2670 const TString &identifier,
2671 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002672 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002673 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002674{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002675 // If the declaration starting this declarator list was empty (example: int,), some checks were
2676 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002677 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002678 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002679 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002680 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002681 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002682
Olli Etuaho55bde912017-10-25 13:41:13 +03002683 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002684
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002686 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002687 TType *arrayType = new TType(elementType);
2688 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002689
Olli Etuahob60d30f2018-01-16 12:31:06 +02002690 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002691
Olli Etuahob60d30f2018-01-16 12:31:06 +02002692 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002693
Olli Etuahob60d30f2018-01-16 12:31:06 +02002694 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002695
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002696 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002697 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002698 {
Olli Etuaho195be942017-12-04 23:40:14 +02002699 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002700 symbol->setLine(identifierLocation);
2701 declarationOut->appendDeclarator(symbol);
2702 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002703 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002704}
2705
Olli Etuaho13389b62016-10-16 11:48:18 +01002706void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2707 const TSourceLoc &identifierLocation,
2708 const TString &identifier,
2709 const TSourceLoc &initLocation,
2710 TIntermTyped *initializer,
2711 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002712{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002713 // If the declaration starting this declarator list was empty (example: int,), some checks were
2714 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002715 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002716 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002717 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2718 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002719 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002720
Olli Etuaho856c4972016-08-08 11:38:39 +03002721 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002722
Olli Etuaho13389b62016-10-16 11:48:18 +01002723 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002724 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002725 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002726 {
2727 //
2728 // build the intermediate representation
2729 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002730 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002731 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002734 }
2735}
2736
Olli Etuaho55bde912017-10-25 13:41:13 +03002737void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002738 const TSourceLoc &identifierLocation,
2739 const TString &identifier,
2740 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002741 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002742 const TSourceLoc &initLocation,
2743 TIntermTyped *initializer,
2744 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002745{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002746 // If the declaration starting this declarator list was empty (example: int,), some checks were
2747 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002748 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002749 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002750 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002751 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002752 }
2753
Olli Etuaho55bde912017-10-25 13:41:13 +03002754 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757
Olli Etuahob60d30f2018-01-16 12:31:06 +02002758 TType *arrayType = new TType(elementType);
2759 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002760
2761 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002762 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002763 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764 {
2765 if (initNode)
2766 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002767 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002768 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 }
2770}
2771
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002772TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2773{
2774 // It's simpler to parse an empty statement as a constant expression rather than having a
2775 // different type of node just for empty statements, that will be pruned from the AST anyway.
2776 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2777 node->setLine(location);
2778 return node;
2779}
2780
jchen104cdac9e2017-05-08 11:01:20 +08002781void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2782 const TSourceLoc &location)
2783{
2784 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2785 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2786 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2787 {
2788 error(location, "Requires both binding and offset", "layout");
2789 return;
2790 }
2791 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2792}
2793
Olli Etuahocce89652017-06-19 16:04:09 +03002794void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2795 const TPublicType &type,
2796 const TSourceLoc &loc)
2797{
2798 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2799 !getFragmentPrecisionHigh())
2800 {
2801 error(loc, "precision is not supported in fragment shader", "highp");
2802 }
2803
2804 if (!CanSetDefaultPrecisionOnType(type))
2805 {
2806 error(loc, "illegal type argument for default precision qualifier",
2807 getBasicString(type.getBasicType()));
2808 return;
2809 }
2810 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2811}
2812
Shaob5cc1192017-07-06 10:47:20 +08002813bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2814{
2815 switch (typeQualifier.layoutQualifier.primitiveType)
2816 {
2817 case EptLines:
2818 case EptLinesAdjacency:
2819 case EptTriangles:
2820 case EptTrianglesAdjacency:
2821 return typeQualifier.qualifier == EvqGeometryIn;
2822
2823 case EptLineStrip:
2824 case EptTriangleStrip:
2825 return typeQualifier.qualifier == EvqGeometryOut;
2826
2827 case EptPoints:
2828 return true;
2829
2830 default:
2831 UNREACHABLE();
2832 return false;
2833 }
2834}
2835
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002836void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2837 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002838{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002839 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002840 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002841 const TSymbol *glPerVertex = symbolTable.findBuiltIn("gl_PerVertex", 310);
2842 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002843 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2844 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002845 mGlInVariableWithArraySize =
2846 new TVariable(&symbolTable, NewPoolTString("gl_in"), glInType, SymbolType::BuiltIn,
2847 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002848 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002849 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002850 {
2851 error(line,
2852 "Array size or input primitive declaration doesn't match the size of earlier sized "
2853 "array inputs.",
2854 "layout");
2855 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002856}
2857
Shaob5cc1192017-07-06 10:47:20 +08002858bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2859{
2860 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2861
2862 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2863
2864 if (layoutQualifier.maxVertices != -1)
2865 {
2866 error(typeQualifier.line,
2867 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2868 return false;
2869 }
2870
2871 // Set mGeometryInputPrimitiveType if exists
2872 if (layoutQualifier.primitiveType != EptUndefined)
2873 {
2874 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2875 {
2876 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2877 return false;
2878 }
2879
2880 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2881 {
2882 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002883 setGeometryShaderInputArraySize(
2884 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2885 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002886 }
2887 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2888 {
2889 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2890 "layout");
2891 return false;
2892 }
2893 }
2894
2895 // Set mGeometryInvocations if exists
2896 if (layoutQualifier.invocations > 0)
2897 {
2898 if (mGeometryShaderInvocations == 0)
2899 {
2900 mGeometryShaderInvocations = layoutQualifier.invocations;
2901 }
2902 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2903 {
2904 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2905 "layout");
2906 return false;
2907 }
2908 }
2909
2910 return true;
2911}
2912
2913bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2914{
2915 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2916
2917 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2918
2919 if (layoutQualifier.invocations > 0)
2920 {
2921 error(typeQualifier.line,
2922 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2923 return false;
2924 }
2925
2926 // Set mGeometryOutputPrimitiveType if exists
2927 if (layoutQualifier.primitiveType != EptUndefined)
2928 {
2929 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2930 {
2931 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2932 return false;
2933 }
2934
2935 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2936 {
2937 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2938 }
2939 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2940 {
2941 error(typeQualifier.line,
2942 "primitive doesn't match earlier output primitive declaration", "layout");
2943 return false;
2944 }
2945 }
2946
2947 // Set mGeometryMaxVertices if exists
2948 if (layoutQualifier.maxVertices > -1)
2949 {
2950 if (mGeometryShaderMaxVertices == -1)
2951 {
2952 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2953 }
2954 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2955 {
2956 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2957 "layout");
2958 return false;
2959 }
2960 }
2961
2962 return true;
2963}
2964
Martin Radev70866b82016-07-22 15:27:42 +03002965void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002966{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002967 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002968 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002969
Martin Radev70866b82016-07-22 15:27:42 +03002970 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2971 typeQualifier.line);
2972
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973 // It should never be the case, but some strange parser errors can send us here.
2974 if (layoutQualifier.isEmpty())
2975 {
2976 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002977 return;
2978 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002979
Martin Radev802abe02016-08-04 17:48:32 +03002980 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002981 {
Olli Etuaho43364892017-02-13 16:00:12 +00002982 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002983 return;
2984 }
2985
Olli Etuaho43364892017-02-13 16:00:12 +00002986 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2987
2988 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002989
2990 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2991
Andrei Volykhina5527072017-03-22 16:46:30 +03002992 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2993
jchen104cdac9e2017-05-08 11:01:20 +08002994 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2995
Qin Jiajiaca68d982017-09-18 16:41:56 +08002996 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2997 typeQualifier.qualifier);
2998
Martin Radev802abe02016-08-04 17:48:32 +03002999 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003000 {
Martin Radev802abe02016-08-04 17:48:32 +03003001 if (mComputeShaderLocalSizeDeclared &&
3002 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3003 {
3004 error(typeQualifier.line, "Work group size does not match the previous declaration",
3005 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003006 return;
3007 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003008
Martin Radev802abe02016-08-04 17:48:32 +03003009 if (mShaderVersion < 310)
3010 {
3011 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003012 return;
3013 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003014
Martin Radev4c4c8e72016-08-04 12:25:34 +03003015 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003016 {
3017 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003018 return;
3019 }
3020
3021 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3022 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3023
3024 const TConstantUnion *maxComputeWorkGroupSizeData =
3025 maxComputeWorkGroupSize->getConstPointer();
3026
3027 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3028 {
3029 if (layoutQualifier.localSize[i] != -1)
3030 {
3031 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3032 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3033 if (mComputeShaderLocalSize[i] < 1 ||
3034 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3035 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003036 std::stringstream reasonStream;
3037 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3038 << maxComputeWorkGroupSizeValue;
3039 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003040
Olli Etuaho4de340a2016-12-16 09:32:03 +00003041 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003042 return;
3043 }
3044 }
3045 }
3046
3047 mComputeShaderLocalSizeDeclared = true;
3048 }
Shaob5cc1192017-07-06 10:47:20 +08003049 else if (typeQualifier.qualifier == EvqGeometryIn)
3050 {
3051 if (mShaderVersion < 310)
3052 {
3053 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3054 return;
3055 }
3056
3057 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3058 {
3059 return;
3060 }
3061 }
3062 else if (typeQualifier.qualifier == EvqGeometryOut)
3063 {
3064 if (mShaderVersion < 310)
3065 {
3066 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3067 "layout");
3068 return;
3069 }
3070
3071 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3072 {
3073 return;
3074 }
3075 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003076 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3077 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003078 {
3079 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3080 // specification.
3081 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3082 {
3083 error(typeQualifier.line, "Number of views does not match the previous declaration",
3084 "layout");
3085 return;
3086 }
3087
3088 if (layoutQualifier.numViews == -1)
3089 {
3090 error(typeQualifier.line, "No num_views specified", "layout");
3091 return;
3092 }
3093
3094 if (layoutQualifier.numViews > mMaxNumViews)
3095 {
3096 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3097 "layout");
3098 return;
3099 }
3100
3101 mNumViews = layoutQualifier.numViews;
3102 }
Martin Radev802abe02016-08-04 17:48:32 +03003103 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003104 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003105 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003106 {
Martin Radev802abe02016-08-04 17:48:32 +03003107 return;
3108 }
3109
Jiajia Qinbc585152017-06-23 15:42:17 +08003110 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003111 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003112 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003113 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003114 return;
3115 }
3116
3117 if (mShaderVersion < 300)
3118 {
3119 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3120 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003121 return;
3122 }
3123
Olli Etuaho09b04a22016-12-15 13:30:26 +00003124 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003125
3126 if (layoutQualifier.matrixPacking != EmpUnspecified)
3127 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003128 if (typeQualifier.qualifier == EvqUniform)
3129 {
3130 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3131 }
3132 else if (typeQualifier.qualifier == EvqBuffer)
3133 {
3134 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3135 }
Martin Radev802abe02016-08-04 17:48:32 +03003136 }
3137
3138 if (layoutQualifier.blockStorage != EbsUnspecified)
3139 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003140 if (typeQualifier.qualifier == EvqUniform)
3141 {
3142 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3143 }
3144 else if (typeQualifier.qualifier == EvqBuffer)
3145 {
3146 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3147 }
Martin Radev802abe02016-08-04 17:48:32 +03003148 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003149 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003150}
3151
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003152TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3153 const TFunction &function,
3154 const TSourceLoc &location,
3155 bool insertParametersToSymbolTable)
3156{
Olli Etuahobed35d72017-12-20 16:36:26 +02003157 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003158
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003159 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 prototype->setLine(location);
3161
3162 for (size_t i = 0; i < function.getParamCount(); i++)
3163 {
3164 const TConstParameter &param = function.getParam(i);
3165
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003166 TIntermSymbol *symbol = nullptr;
3167
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003168 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3169 // be used for unused args).
3170 if (param.name != nullptr)
3171 {
Olli Etuaho195be942017-12-04 23:40:14 +02003172 TVariable *variable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003173 new TVariable(&symbolTable, param.name, param.type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003174 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003175 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003176 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003177 {
Olli Etuaho195be942017-12-04 23:40:14 +02003178 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003179 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003180 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003183 // Unsized type of a named parameter should have already been checked and sanitized.
3184 ASSERT(!param.type->isUnsizedArray());
3185 }
3186 else
3187 {
3188 if (param.type->isUnsizedArray())
3189 {
3190 error(location, "function parameter array must be sized at compile time", "[]");
3191 // We don't need to size the arrays since the parameter is unnamed and hence
3192 // inaccessible.
3193 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003197 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3198 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003199 TVariable *emptyVariable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003200 new TVariable(&symbolTable, nullptr, param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003201 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003203 symbol->setLine(location);
3204 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 }
3206 return prototype;
3207}
3208
Olli Etuaho16c745a2017-01-16 17:02:27 +00003209TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3210 const TFunction &parsedFunction,
3211 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003212{
Olli Etuaho476197f2016-10-11 13:59:08 +01003213 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3214 // first declaration. Either way the instance in the symbol table is used to track whether the
3215 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003216 bool hadPrototypeDeclaration = false;
3217 const TFunction *function = symbolTable.markUserDefinedFunctionHasPrototypeDeclaration(
3218 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3219
3220 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003221 {
3222 // ESSL 1.00.17 section 4.2.7.
3223 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3224 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003225 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003226
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003227 TIntermFunctionPrototype *prototype =
3228 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003230 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003231
3232 if (!symbolTable.atGlobalLevel())
3233 {
3234 // ESSL 3.00.4 section 4.2.4.
3235 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003236 }
3237
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003238 return prototype;
3239}
3240
Olli Etuaho336b1472016-10-05 16:37:55 +01003241TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003243 TIntermBlock *functionBody,
3244 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003246 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3248 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003249 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003250 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003251 }
3252
Olli Etuahof51fdd22016-10-03 10:03:40 +01003253 if (functionBody == nullptr)
3254 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003255 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256 functionBody->setLine(location);
3257 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003259 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003260 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003263 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264}
3265
Olli Etuaho476197f2016-10-11 13:59:08 +01003266void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003267 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003268 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003269{
Olli Etuaho476197f2016-10-11 13:59:08 +01003270 ASSERT(function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003271 const TSymbol *builtIn =
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003272 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003273
3274 if (builtIn)
3275 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003276 error(location, "built-in functions cannot be redefined", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003277 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 else
Jamie Madill185fb402015-06-12 15:48:48 -04003279 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003280 bool wasDefined = false;
3281 function =
3282 symbolTable.setUserDefinedFunctionParameterNamesFromDefinition(function, &wasDefined);
3283 if (wasDefined)
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003285 error(location, "function already has a body", function->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003286 }
Jamie Madill185fb402015-06-12 15:48:48 -04003287 }
Jamie Madill185fb402015-06-12 15:48:48 -04003288
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003289 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003290 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003291 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003292
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003293 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003294 setLoopNestingLevel(0);
3295}
3296
Jamie Madillb98c3a82015-07-23 14:26:04 -04003297TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003298{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003299 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003300 // We don't know at this point whether this is a function definition or a prototype.
3301 // The definition production code will check for redefinitions.
3302 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003303 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003304 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3305 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003306 //
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003307 const TFunction *prevDec = static_cast<const TFunction *>(
3308 symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303309
Olli Etuahod80f2942017-11-06 12:44:45 +02003310 for (size_t i = 0u; i < function->getParamCount(); ++i)
3311 {
3312 auto &param = function->getParam(i);
3313 if (param.type->isStructSpecifier())
3314 {
3315 // ESSL 3.00.6 section 12.10.
3316 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003317 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003318 }
3319 }
3320
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003321 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003322 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303323 {
Martin Radevda6254b2016-12-14 17:00:36 +02003324 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303325 // Therefore overloading or redefining builtin functions is an error.
3326 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003327 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303328 }
3329 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003330 {
3331 if (prevDec->getReturnType() != function->getReturnType())
3332 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003333 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003334 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003335 }
3336 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003338 if (prevDec->getParam(i).type->getQualifier() !=
3339 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003340 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003341 error(location,
3342 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003343 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003344 }
3345 }
3346 }
3347
Jamie Madill185fb402015-06-12 15:48:48 -04003348 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003349 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3350 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003351 if (prevSym)
3352 {
3353 if (!prevSym->isFunction())
3354 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003355 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003356 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003357 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003359 // Parsing is at the inner scope level of the function's arguments and body statement at this
3360 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3361 // scope.
3362 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003363
Olli Etuaho78d13742017-01-18 13:06:10 +00003364 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003365 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003366 {
3367 if (function->getParamCount() > 0)
3368 {
3369 error(location, "function cannot take any parameter(s)", "main");
3370 }
3371 if (function->getReturnType().getBasicType() != EbtVoid)
3372 {
3373 error(location, "main function cannot return a value",
3374 function->getReturnType().getBasicString());
3375 }
3376 }
3377
Jamie Madill185fb402015-06-12 15:48:48 -04003378 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003379 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3380 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003381 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3382 //
3383 return function;
3384}
3385
Olli Etuaho9de84a52016-06-14 17:36:01 +03003386TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3387 const TString *name,
3388 const TSourceLoc &location)
3389{
3390 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3391 {
3392 error(location, "no qualifiers allowed for function return",
3393 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003394 }
3395 if (!type.layoutQualifier.isEmpty())
3396 {
3397 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003398 }
jchen10cc2a10e2017-05-03 14:05:12 +08003399 // make sure an opaque type is not involved as well...
3400 std::string reason(getBasicString(type.getBasicType()));
3401 reason += "s can't be function return values";
3402 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003403 if (mShaderVersion < 300)
3404 {
3405 // Array return values are forbidden, but there's also no valid syntax for declaring array
3406 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003407 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003408
3409 if (type.isStructureContainingArrays())
3410 {
3411 // ESSL 1.00.17 section 6.1 Function Definitions
3412 error(location, "structures containing arrays can't be function return values",
3413 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003414 }
3415 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003416
3417 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003418 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419}
3420
Olli Etuaho95ed1942018-02-01 14:01:19 +02003421TFunctionLookup *TParseContext::addNonConstructorFunc(const TString *name)
Olli Etuahocce89652017-06-19 16:04:09 +03003422{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003423 return TFunctionLookup::CreateFunctionCall(name);
Olli Etuahocce89652017-06-19 16:04:09 +03003424}
3425
Olli Etuaho95ed1942018-02-01 14:01:19 +02003426TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003427{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003428 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003429 {
3430 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3431 "[]");
3432 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003433 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003434 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003435 error(publicType.getLine(), "constructor can't be a structure definition",
3436 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003437 }
3438
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003439 TType *type = new TType(publicType);
3440 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003441 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003442 error(publicType.getLine(), "cannot construct this type",
3443 getBasicString(publicType.getBasicType()));
3444 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003445 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003446 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003447}
3448
Olli Etuaho55bde912017-10-25 13:41:13 +03003449void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3450 const char *errorMessage,
3451 const char *token,
3452 TType *arrayType)
3453{
3454 if (arrayType->isUnsizedArray())
3455 {
3456 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003457 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003458 }
3459}
3460
3461TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003462 const TString *name,
3463 const TSourceLoc &nameLoc)
3464{
Olli Etuaho55bde912017-10-25 13:41:13 +03003465 ASSERT(type);
3466 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3467 type);
3468 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003469 {
3470 error(nameLoc, "illegal use of type 'void'", name->c_str());
3471 }
3472 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003473 TParameter param = {name, type};
3474 return param;
3475}
3476
Olli Etuaho55bde912017-10-25 13:41:13 +03003477TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3478 const TString *name,
3479 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003480{
Olli Etuaho55bde912017-10-25 13:41:13 +03003481 TType *type = new TType(publicType);
3482 return parseParameterDeclarator(type, name, nameLoc);
3483}
3484
3485TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3486 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003487 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003488 const TSourceLoc &arrayLoc,
3489 TPublicType *elementType)
3490{
3491 checkArrayElementIsNotArray(arrayLoc, *elementType);
3492 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003493 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003494 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003495}
3496
Olli Etuaho95ed1942018-02-01 14:01:19 +02003497bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3498 const TIntermSequence &arguments,
3499 TType type,
3500 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003501{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003502 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003503 {
3504 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3505 return false;
3506 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003507 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003508 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003509 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003510 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003511 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3512 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003513 {
3514 error(line, "constructing from a non-dereferenced array", "constructor");
3515 return false;
3516 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003517 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003518 {
3519 if (dimensionalityFromElement == 1u)
3520 {
3521 error(line, "implicitly sized array of arrays constructor argument is not an array",
3522 "constructor");
3523 }
3524 else
3525 {
3526 error(line,
3527 "implicitly sized array of arrays constructor argument dimensionality is too "
3528 "low",
3529 "constructor");
3530 }
3531 return false;
3532 }
3533 }
3534 return true;
3535}
3536
Jamie Madillb98c3a82015-07-23 14:26:04 -04003537// This function is used to test for the correctness of the parameters passed to various constructor
3538// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003539//
Olli Etuaho856c4972016-08-08 11:38:39 +03003540// 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 +00003541//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003542TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003543{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003544 TType type = fnCall->constructorType();
3545 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003546 if (type.isUnsizedArray())
3547 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003549 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003550 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003551 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003552 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003553 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003555 if (type.getOutermostArraySize() == 0u)
3556 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003557 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003558 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003559 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003560 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003561 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003562 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003563 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003564 }
3565 }
3566 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003567 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003568
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003569 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003570 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003571 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003572 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003573
Olli Etuaho95ed1942018-02-01 14:01:19 +02003574 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003575 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003576
Olli Etuaho765924f2018-01-04 12:48:36 +02003577 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003578}
3579
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003580//
3581// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003582// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583//
Olli Etuaho13389b62016-10-16 11:48:18 +01003584TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003585 const TTypeQualifierBuilder &typeQualifierBuilder,
3586 const TSourceLoc &nameLine,
3587 const TString &blockName,
3588 TFieldList *fieldList,
3589 const TString *instanceName,
3590 const TSourceLoc &instanceLine,
3591 TIntermTyped *arrayIndex,
3592 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593{
Olli Etuaho856c4972016-08-08 11:38:39 +03003594 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003595
Olli Etuaho77ba4082016-12-16 12:01:18 +00003596 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003597
Jiajia Qinbc585152017-06-23 15:42:17 +08003598 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003600 error(typeQualifier.line,
3601 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3602 "3.10",
3603 getQualifierString(typeQualifier.qualifier));
3604 }
3605 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3606 {
3607 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003608 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609 }
3610
Martin Radev70866b82016-07-22 15:27:42 +03003611 if (typeQualifier.invariant)
3612 {
3613 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3614 }
3615
Jiajia Qinbc585152017-06-23 15:42:17 +08003616 if (typeQualifier.qualifier != EvqBuffer)
3617 {
3618 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3619 }
Olli Etuaho43364892017-02-13 16:00:12 +00003620
jchen10af713a22017-04-19 09:10:56 +08003621 // add array index
3622 unsigned int arraySize = 0;
3623 if (arrayIndex != nullptr)
3624 {
3625 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3626 }
3627
3628 if (mShaderVersion < 310)
3629 {
3630 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3631 }
3632 else
3633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3635 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003636 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003637
Andrei Volykhina5527072017-03-22 16:46:30 +03003638 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3639
Jamie Madill099c0f32013-06-20 11:55:52 -04003640 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003641 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003642 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3643 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003644
Jamie Madill099c0f32013-06-20 11:55:52 -04003645 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3646 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003647 if (typeQualifier.qualifier == EvqUniform)
3648 {
3649 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3650 }
3651 else if (typeQualifier.qualifier == EvqBuffer)
3652 {
3653 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3654 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003655 }
3656
Jamie Madill1566ef72013-06-20 11:55:54 -04003657 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3658 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier == EvqUniform)
3660 {
3661 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3662 }
3663 else if (typeQualifier.qualifier == EvqBuffer)
3664 {
3665 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3666 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003667 }
3668
Olli Etuaho856c4972016-08-08 11:38:39 +03003669 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003670
Martin Radev2cc85b32016-08-05 16:22:53 +03003671 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3672
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303674 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3675 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003676 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303677 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003678 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303679 {
jchen10cc2a10e2017-05-03 14:05:12 +08003680 std::string reason("unsupported type - ");
3681 reason += fieldType->getBasicString();
3682 reason += " types are not allowed in interface blocks";
3683 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003684 }
3685
Jamie Madill98493dd2013-07-08 14:39:03 -04003686 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003687 switch (qualifier)
3688 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003689 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003690 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003691 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003692 if (typeQualifier.qualifier == EvqBuffer)
3693 {
3694 error(field->line(), "invalid qualifier on shader storage block member",
3695 getQualifierString(qualifier));
3696 }
3697 break;
3698 case EvqBuffer:
3699 if (typeQualifier.qualifier == EvqUniform)
3700 {
3701 error(field->line(), "invalid qualifier on uniform block member",
3702 getQualifierString(qualifier));
3703 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003704 break;
3705 default:
3706 error(field->line(), "invalid qualifier on interface block member",
3707 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003708 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003709 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003710
Martin Radev70866b82016-07-22 15:27:42 +03003711 if (fieldType->isInvariant())
3712 {
3713 error(field->line(), "invalid qualifier on interface block member", "invariant");
3714 }
3715
Jamie Madilla5efff92013-06-06 11:56:47 -04003716 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003717 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003718 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003719 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003722 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003723 error(field->line(), "invalid layout qualifier: cannot be used here",
3724 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003725 }
3726
Jamie Madill98493dd2013-07-08 14:39:03 -04003727 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003728 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003729 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003730 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003731 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003732 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003733 warning(field->line(),
3734 "extraneous layout qualifier: only has an effect on matrix types",
3735 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003736 }
3737
Jamie Madill98493dd2013-07-08 14:39:03 -04003738 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003739
Olli Etuahoebee5b32017-11-23 12:56:32 +02003740 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3741 typeQualifier.qualifier != EvqBuffer)
3742 {
3743 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3744 checkIsNotUnsizedArray(field->line(),
3745 "array members of interface blocks must specify a size",
3746 field->name().c_str(), field->type());
3747 }
3748
Jiajia Qinbc585152017-06-23 15:42:17 +08003749 if (typeQualifier.qualifier == EvqBuffer)
3750 {
3751 // set memory qualifiers
3752 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3753 // qualified with a memory qualifier, it is as if all of its members were declared with
3754 // the same memory qualifier.
3755 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3756 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3757 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3758 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3759 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3760 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3761 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3762 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3763 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3764 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3765 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003766 }
3767
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003768 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3769 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003770 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3771 {
3772 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3773 }
3774
Olli Etuahob60d30f2018-01-16 12:31:06 +02003775 TType *interfaceBlockType =
3776 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003777 if (arrayIndex != nullptr)
3778 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003779 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003780 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003781
Olli Etuaho195be942017-12-04 23:40:14 +02003782 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003783 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3784 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003785 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003786 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3787 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003788
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003789 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003790 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003791 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003792 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003794 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003795 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003796
3797 // set parent pointer of the field variable
3798 fieldType->setInterfaceBlock(interfaceBlock);
3799
Olli Etuahob60d30f2018-01-16 12:31:06 +02003800 fieldType->setQualifier(typeQualifier.qualifier);
3801
Olli Etuaho195be942017-12-04 23:40:14 +02003802 TVariable *fieldVariable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003803 new TVariable(&symbolTable, &field->name(), fieldType, SymbolType::UserDefined);
3804 if (!symbolTable.declareVariable(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303805 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003806 error(field->line(), "redefinition of an interface block member name",
3807 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003808 }
3809 }
3810 }
3811 else
3812 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003813 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003814
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003816 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303817 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003818 error(instanceLine, "redefinition of an interface block instance name",
3819 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003821 }
3822
Olli Etuaho195be942017-12-04 23:40:14 +02003823 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3824 blockSymbol->setLine(typeQualifier.line);
3825 TIntermDeclaration *declaration = new TIntermDeclaration();
3826 declaration->appendDeclarator(blockSymbol);
3827 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003828
3829 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003830 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003831}
3832
Olli Etuaho383b7912016-08-05 11:22:59 +03003833void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003834{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003835 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003836
3837 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003838 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303839 if (mStructNestingLevel > 1)
3840 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003841 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003842 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003843}
3844
3845void TParseContext::exitStructDeclaration()
3846{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003847 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003848}
3849
Olli Etuaho8a176262016-08-16 14:23:01 +03003850void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003851{
Jamie Madillacb4b812016-11-07 13:50:29 -05003852 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303853 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003854 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003855 }
3856
Arun Patole7e7e68d2015-05-22 12:02:25 +05303857 if (field.type()->getBasicType() != EbtStruct)
3858 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003859 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003860 }
3861
3862 // We're already inside a structure definition at this point, so add
3863 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3865 {
Jamie Madill41a49272014-03-18 16:10:13 -04003866 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003867 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3868 {
3869 // This may happen in case there are nested struct definitions. While they are also
3870 // invalid GLSL, they don't cause a syntax error.
3871 reasonStream << "Struct nesting";
3872 }
3873 else
3874 {
3875 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003876 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003877 }
3878 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003879 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003880 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003881 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003883}
3884
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003885//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003886// Parse an array index expression
3887//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3889 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003891{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003892 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3893 {
3894 if (baseExpression->getAsSymbolNode())
3895 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303896 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02003897 baseExpression->getAsSymbolNode()->getName().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003898 }
3899 else
3900 {
3901 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3902 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003903
Olli Etuaho3ec75682017-07-05 17:02:55 +03003904 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003906
Jiawei Shaod8105a02017-08-08 09:54:36 +08003907 if (baseExpression->getQualifier() == EvqPerVertexIn)
3908 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003909 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003910 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3911 {
3912 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3913 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3914 }
3915 }
3916
Jamie Madill21c1e452014-12-29 11:33:41 -05003917 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3918
Olli Etuaho36b05142015-11-12 13:10:42 +02003919 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3920 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3921 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3922 // index is a constant expression.
3923 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3924 {
3925 if (baseExpression->isInterfaceBlock())
3926 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003927 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003928 switch (baseExpression->getQualifier())
3929 {
3930 case EvqPerVertexIn:
3931 break;
3932 case EvqUniform:
3933 case EvqBuffer:
3934 error(location,
3935 "array indexes for uniform block arrays and shader storage block arrays "
3936 "must be constant integral expressions",
3937 "[");
3938 break;
3939 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003940 // We can reach here only in error cases.
3941 ASSERT(mDiagnostics->numErrors() > 0);
3942 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003943 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003944 }
3945 else if (baseExpression->getQualifier() == EvqFragmentOut)
3946 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003947 error(location,
3948 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003949 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003950 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3951 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003952 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003953 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003954 }
3955
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003956 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003957 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003958 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3959 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3960 // constant fold expressions that are not constant expressions). The most compatible way to
3961 // handle this case is to report a warning instead of an error and force the index to be in
3962 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003963 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003964 int index = 0;
3965 if (indexConstantUnion->getBasicType() == EbtInt)
3966 {
3967 index = indexConstantUnion->getIConst(0);
3968 }
3969 else if (indexConstantUnion->getBasicType() == EbtUInt)
3970 {
3971 index = static_cast<int>(indexConstantUnion->getUConst(0));
3972 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003973
3974 int safeIndex = -1;
3975
Olli Etuahoebee5b32017-11-23 12:56:32 +02003976 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003977 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003978 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3979 safeIndex = 0;
3980 }
3981
3982 if (!baseExpression->getType().isUnsizedArray())
3983 {
3984 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003985 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003986 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003987 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003988 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3989 {
3990 outOfRangeError(outOfRangeIndexIsError, location,
3991 "array index for gl_FragData must be zero when "
3992 "GL_EXT_draw_buffers is disabled",
3993 "[]");
3994 safeIndex = 0;
3995 }
3996 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003997 }
3998 // Only do generic out-of-range check if similar error hasn't already been reported.
3999 if (safeIndex < 0)
4000 {
4001 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004002 {
4003 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4004 baseExpression->getOutermostArraySize(),
4005 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004007 else if (baseExpression->isMatrix())
4008 {
4009 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4010 baseExpression->getType().getCols(),
4011 "matrix field selection out of range");
4012 }
4013 else
4014 {
4015 ASSERT(baseExpression->isVector());
4016 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4017 baseExpression->getType().getNominalSize(),
4018 "vector field selection out of range");
4019 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004020 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004021
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 ASSERT(safeIndex >= 0);
4023 // Data of constant unions can't be changed, because it may be shared with other
4024 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4025 // sanitized object.
4026 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4027 {
4028 TConstantUnion *safeConstantUnion = new TConstantUnion();
4029 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004030 indexExpression = new TIntermConstantUnion(
4031 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4032 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004033 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004034
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035 TIntermBinary *node =
4036 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4037 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004038 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004039 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004040 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004041
4042 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4043 node->setLine(location);
4044 // Indirect indexing can never be constant folded.
4045 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004046}
4047
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4049 const TSourceLoc &location,
4050 int index,
4051 int arraySize,
4052 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004053{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054 // Should not reach here with an unsized / runtime-sized array.
4055 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004056 // A negative index should already have been checked.
4057 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004058 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004059 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004060 std::stringstream reasonStream;
4061 reasonStream << reason << " '" << index << "'";
4062 std::string token = reasonStream.str();
4063 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004064 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004065 }
4066 return index;
4067}
4068
Jamie Madillb98c3a82015-07-23 14:26:04 -04004069TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4070 const TSourceLoc &dotLocation,
4071 const TString &fieldString,
4072 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004073{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074 if (baseExpression->isArray())
4075 {
4076 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004077 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 }
4079
4080 if (baseExpression->isVector())
4081 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004082 TVector<int> fieldOffsets;
4083 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4084 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004086 fieldOffsets.resize(1);
4087 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004089 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4090 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004091
Olli Etuaho765924f2018-01-04 12:48:36 +02004092 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004093 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004094 else if (baseExpression->getBasicType() == EbtStruct)
4095 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304096 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004097 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098 {
4099 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004100 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 }
4102 else
4103 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004104 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004106 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004107 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004108 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 {
4110 fieldFound = true;
4111 break;
4112 }
4113 }
4114 if (fieldFound)
4115 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004116 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004117 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 TIntermBinary *node =
4119 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4120 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004121 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 }
4123 else
4124 {
4125 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004126 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 }
4128 }
4129 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004130 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004131 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304132 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004133 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004134 {
4135 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004137 }
4138 else
4139 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004141 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004142 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004143 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004144 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +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(EOpIndexDirectInterfaceBlock, baseExpression, index);
4156 node->setLine(dotLocation);
4157 // Indexing interface blocks can never be constant folded.
4158 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004159 }
4160 else
4161 {
4162 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004163 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 }
4165 }
4166 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004167 else
4168 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004169 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004171 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304172 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 }
4174 else
4175 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304176 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004177 " field selection requires structure, vector, or interface block on left hand "
4178 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304179 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004180 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004181 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004182 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004183}
4184
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4186 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004187{
Jamie Madill2f294c92017-11-20 14:47:26 -05004188 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004189
4190 if (qualifierType == "shared")
4191 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004192 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004193 {
4194 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4195 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004196 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004197 }
4198 else if (qualifierType == "packed")
4199 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004200 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004201 {
4202 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4203 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004204 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004205 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004206 else if (qualifierType == "std430")
4207 {
4208 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4209 qualifier.blockStorage = EbsStd430;
4210 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004211 else if (qualifierType == "std140")
4212 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004213 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004214 }
4215 else if (qualifierType == "row_major")
4216 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004217 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004218 }
4219 else if (qualifierType == "column_major")
4220 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004221 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004222 }
4223 else if (qualifierType == "location")
4224 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004225 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4226 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004227 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004228 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004229 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004230 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4231 {
4232 qualifier.yuv = true;
4233 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004234 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004235 else if (qualifierType == "rgba32f")
4236 {
4237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4238 qualifier.imageInternalFormat = EiifRGBA32F;
4239 }
4240 else if (qualifierType == "rgba16f")
4241 {
4242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4243 qualifier.imageInternalFormat = EiifRGBA16F;
4244 }
4245 else if (qualifierType == "r32f")
4246 {
4247 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4248 qualifier.imageInternalFormat = EiifR32F;
4249 }
4250 else if (qualifierType == "rgba8")
4251 {
4252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4253 qualifier.imageInternalFormat = EiifRGBA8;
4254 }
4255 else if (qualifierType == "rgba8_snorm")
4256 {
4257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4258 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4259 }
4260 else if (qualifierType == "rgba32i")
4261 {
4262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4263 qualifier.imageInternalFormat = EiifRGBA32I;
4264 }
4265 else if (qualifierType == "rgba16i")
4266 {
4267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4268 qualifier.imageInternalFormat = EiifRGBA16I;
4269 }
4270 else if (qualifierType == "rgba8i")
4271 {
4272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4273 qualifier.imageInternalFormat = EiifRGBA8I;
4274 }
4275 else if (qualifierType == "r32i")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifR32I;
4279 }
4280 else if (qualifierType == "rgba32ui")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA32UI;
4284 }
4285 else if (qualifierType == "rgba16ui")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifRGBA16UI;
4289 }
4290 else if (qualifierType == "rgba8ui")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.imageInternalFormat = EiifRGBA8UI;
4294 }
4295 else if (qualifierType == "r32ui")
4296 {
4297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4298 qualifier.imageInternalFormat = EiifR32UI;
4299 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004300 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4301 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.primitiveType = EptPoints;
4305 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004306 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4307 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004308 {
4309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4310 qualifier.primitiveType = EptLines;
4311 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004312 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4313 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004314 {
4315 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4316 qualifier.primitiveType = EptLinesAdjacency;
4317 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004318 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4319 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004320 {
4321 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4322 qualifier.primitiveType = EptTriangles;
4323 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004324 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4325 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.primitiveType = EptTrianglesAdjacency;
4329 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004330 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptLineStrip;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "triangle_strip" && 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 = EptTriangleStrip;
4341 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004342
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004343 else
4344 {
4345 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004346 }
4347
Jamie Madilla5efff92013-06-06 11:56:47 -04004348 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004349}
4350
Martin Radev802abe02016-08-04 17:48:32 +03004351void TParseContext::parseLocalSize(const TString &qualifierType,
4352 const TSourceLoc &qualifierTypeLine,
4353 int intValue,
4354 const TSourceLoc &intValueLine,
4355 const std::string &intValueString,
4356 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004357 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004358{
Olli Etuaho856c4972016-08-08 11:38:39 +03004359 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004360 if (intValue < 1)
4361 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004362 std::stringstream reasonStream;
4363 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4364 std::string reason = reasonStream.str();
4365 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004366 }
4367 (*localSize)[index] = intValue;
4368}
4369
Olli Etuaho09b04a22016-12-15 13:30:26 +00004370void TParseContext::parseNumViews(int intValue,
4371 const TSourceLoc &intValueLine,
4372 const std::string &intValueString,
4373 int *numViews)
4374{
4375 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4376 // specification.
4377 if (intValue < 1)
4378 {
4379 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4380 }
4381 *numViews = intValue;
4382}
4383
Shaob5cc1192017-07-06 10:47:20 +08004384void TParseContext::parseInvocations(int intValue,
4385 const TSourceLoc &intValueLine,
4386 const std::string &intValueString,
4387 int *numInvocations)
4388{
4389 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4390 // it doesn't make sense to accept invocations <= 0.
4391 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4392 {
4393 error(intValueLine,
4394 "out of range: invocations must be in the range of [1, "
4395 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4396 intValueString.c_str());
4397 }
4398 else
4399 {
4400 *numInvocations = intValue;
4401 }
4402}
4403
4404void TParseContext::parseMaxVertices(int intValue,
4405 const TSourceLoc &intValueLine,
4406 const std::string &intValueString,
4407 int *maxVertices)
4408{
4409 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4410 // it doesn't make sense to accept max_vertices < 0.
4411 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4412 {
4413 error(
4414 intValueLine,
4415 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4416 intValueString.c_str());
4417 }
4418 else
4419 {
4420 *maxVertices = intValue;
4421 }
4422}
4423
Jamie Madillb98c3a82015-07-23 14:26:04 -04004424TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4425 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004426 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304427 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004428{
Jamie Madill2f294c92017-11-20 14:47:26 -05004429 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004430
Martin Radev802abe02016-08-04 17:48:32 +03004431 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004432
Martin Radev802abe02016-08-04 17:48:32 +03004433 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004434 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004435 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004436 if (intValue < 0)
4437 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004438 error(intValueLine, "out of range: location must be non-negative",
4439 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004440 }
4441 else
4442 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004443 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004444 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004445 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004446 }
Olli Etuaho43364892017-02-13 16:00:12 +00004447 else if (qualifierType == "binding")
4448 {
4449 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4450 if (intValue < 0)
4451 {
4452 error(intValueLine, "out of range: binding must be non-negative",
4453 intValueString.c_str());
4454 }
4455 else
4456 {
4457 qualifier.binding = intValue;
4458 }
4459 }
jchen104cdac9e2017-05-08 11:01:20 +08004460 else if (qualifierType == "offset")
4461 {
4462 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4463 if (intValue < 0)
4464 {
4465 error(intValueLine, "out of range: offset must be non-negative",
4466 intValueString.c_str());
4467 }
4468 else
4469 {
4470 qualifier.offset = intValue;
4471 }
4472 }
Martin Radev802abe02016-08-04 17:48:32 +03004473 else if (qualifierType == "local_size_x")
4474 {
4475 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4476 &qualifier.localSize);
4477 }
4478 else if (qualifierType == "local_size_y")
4479 {
4480 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4481 &qualifier.localSize);
4482 }
4483 else if (qualifierType == "local_size_z")
4484 {
4485 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4486 &qualifier.localSize);
4487 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004488 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004489 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004490 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4491 {
4492 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4493 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004494 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004495 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4496 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004497 {
4498 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4499 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004500 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4501 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004502 {
4503 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4504 }
4505
Martin Radev802abe02016-08-04 17:48:32 +03004506 else
4507 {
4508 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004509 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004510
Jamie Madilla5efff92013-06-06 11:56:47 -04004511 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004512}
4513
Olli Etuaho613b9592016-09-05 12:05:53 +03004514TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4515{
4516 return new TTypeQualifierBuilder(
4517 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4518 mShaderVersion);
4519}
4520
Olli Etuahocce89652017-06-19 16:04:09 +03004521TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4522 const TSourceLoc &loc)
4523{
4524 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4525 return new TStorageQualifierWrapper(qualifier, loc);
4526}
4527
4528TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4529{
4530 if (getShaderType() == GL_VERTEX_SHADER)
4531 {
4532 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4533 }
4534 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4535}
4536
4537TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4538{
4539 if (declaringFunction())
4540 {
4541 return new TStorageQualifierWrapper(EvqIn, loc);
4542 }
Shaob5cc1192017-07-06 10:47:20 +08004543
4544 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004545 {
Shaob5cc1192017-07-06 10:47:20 +08004546 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004547 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004548 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004549 {
4550 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4551 }
4552 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004553 }
Shaob5cc1192017-07-06 10:47:20 +08004554 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004555 {
Shaob5cc1192017-07-06 10:47:20 +08004556 if (mShaderVersion < 300)
4557 {
4558 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4559 }
4560 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004561 }
Shaob5cc1192017-07-06 10:47:20 +08004562 case GL_COMPUTE_SHADER:
4563 {
4564 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4565 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004566 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004567 {
4568 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4569 }
4570 default:
4571 {
4572 UNREACHABLE();
4573 return new TStorageQualifierWrapper(EvqLast, loc);
4574 }
Olli Etuahocce89652017-06-19 16:04:09 +03004575 }
Olli Etuahocce89652017-06-19 16:04:09 +03004576}
4577
4578TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4579{
4580 if (declaringFunction())
4581 {
4582 return new TStorageQualifierWrapper(EvqOut, loc);
4583 }
Shaob5cc1192017-07-06 10:47:20 +08004584 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004585 {
Shaob5cc1192017-07-06 10:47:20 +08004586 case GL_VERTEX_SHADER:
4587 {
4588 if (mShaderVersion < 300)
4589 {
4590 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4591 }
4592 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4593 }
4594 case GL_FRAGMENT_SHADER:
4595 {
4596 if (mShaderVersion < 300)
4597 {
4598 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4599 }
4600 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4601 }
4602 case GL_COMPUTE_SHADER:
4603 {
4604 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4605 return new TStorageQualifierWrapper(EvqLast, loc);
4606 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004607 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004608 {
4609 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4610 }
4611 default:
4612 {
4613 UNREACHABLE();
4614 return new TStorageQualifierWrapper(EvqLast, loc);
4615 }
Olli Etuahocce89652017-06-19 16:04:09 +03004616 }
Olli Etuahocce89652017-06-19 16:04:09 +03004617}
4618
4619TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4620{
4621 if (!declaringFunction())
4622 {
4623 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4624 }
4625 return new TStorageQualifierWrapper(EvqInOut, loc);
4626}
4627
Jamie Madillb98c3a82015-07-23 14:26:04 -04004628TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004629 TLayoutQualifier rightQualifier,
4630 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004631{
Martin Radevc28888b2016-07-22 15:27:42 +03004632 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004633 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004634}
4635
Olli Etuahod5f44c92017-11-29 17:15:40 +02004636TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004637{
4638 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004639 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004640}
4641
Olli Etuahod5f44c92017-11-29 17:15:40 +02004642TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4643 const TSourceLoc &loc,
4644 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004645{
4646 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004647 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004648}
4649
Olli Etuaho722bfb52017-10-26 17:00:11 +03004650void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4651 const TFieldList::const_iterator end,
4652 const TString &name,
4653 const TSourceLoc &location)
4654{
4655 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4656 {
4657 if ((*fieldIter)->name() == name)
4658 {
4659 error(location, "duplicate field name in structure", name.c_str());
4660 }
4661 }
4662}
4663
4664TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4665{
4666 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4667 ++fieldIter)
4668 {
4669 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4670 location);
4671 }
4672 return fields;
4673}
4674
Olli Etuaho4de340a2016-12-16 09:32:03 +00004675TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4676 const TFieldList *newlyAddedFields,
4677 const TSourceLoc &location)
4678{
4679 for (TField *field : *newlyAddedFields)
4680 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004681 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4682 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004683 processedFields->push_back(field);
4684 }
4685 return processedFields;
4686}
4687
Martin Radev70866b82016-07-22 15:27:42 +03004688TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4689 const TTypeQualifierBuilder &typeQualifierBuilder,
4690 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004691 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004692{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004693 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004694
Martin Radev70866b82016-07-22 15:27:42 +03004695 typeSpecifier->qualifier = typeQualifier.qualifier;
4696 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004697 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004698 typeSpecifier->invariant = typeQualifier.invariant;
4699 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304700 {
Martin Radev70866b82016-07-22 15:27:42 +03004701 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004702 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004703 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004704}
4705
Jamie Madillb98c3a82015-07-23 14:26:04 -04004706TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004707 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004708{
Martin Radev4a9cd802016-09-01 16:51:51 +03004709 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4710 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004711
Olli Etuahod5f44c92017-11-29 17:15:40 +02004712 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004713 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004714
Martin Radev4a9cd802016-09-01 16:51:51 +03004715 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004716
Olli Etuahod5f44c92017-11-29 17:15:40 +02004717 TFieldList *fieldList = new TFieldList();
4718
4719 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304720 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004721 TType *type = new TType(typeSpecifier);
4722 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304723 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004724 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004725 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004726 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004728
Olli Etuahod5f44c92017-11-29 17:15:40 +02004729 TField *field = new TField(type, declarator->name(), declarator->line());
4730 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4731 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004732 }
4733
Olli Etuahod5f44c92017-11-29 17:15:40 +02004734 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004735}
4736
Martin Radev4a9cd802016-09-01 16:51:51 +03004737TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4738 const TSourceLoc &nameLine,
4739 const TString *structName,
4740 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004741{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004742 SymbolType structSymbolType = SymbolType::UserDefined;
4743 if (structName == nullptr)
4744 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004745 structSymbolType = SymbolType::Empty;
4746 }
4747 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748
Jamie Madill9b820842015-02-12 10:40:10 -05004749 // Store a bool in the struct if we're at global scope, to allow us to
4750 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004751 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004752
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004753 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004755 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004756 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304757 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004758 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004759 }
4760 }
4761
4762 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004763 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004764 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004765 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004766 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 switch (qualifier)
4768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004769 case EvqGlobal:
4770 case EvqTemporary:
4771 break;
4772 default:
4773 error(field.line(), "invalid qualifier on struct member",
4774 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004775 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776 }
Martin Radev70866b82016-07-22 15:27:42 +03004777 if (field.type()->isInvariant())
4778 {
4779 error(field.line(), "invalid qualifier on struct member", "invariant");
4780 }
jchen104cdac9e2017-05-08 11:01:20 +08004781 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4782 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004783 {
4784 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4785 }
4786
Olli Etuahoebee5b32017-11-23 12:56:32 +02004787 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4788 field.name().c_str(), field.type());
4789
Olli Etuaho43364892017-02-13 16:00:12 +00004790 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4791
4792 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004793
4794 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 }
4796
Martin Radev4a9cd802016-09-01 16:51:51 +03004797 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004798 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004799 exitStructDeclaration();
4800
Martin Radev4a9cd802016-09-01 16:51:51 +03004801 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802}
4803
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004805 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004806 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004807{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004808 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004809 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004810 init->isVector())
4811 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004812 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4813 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004814 return nullptr;
4815 }
4816
Olli Etuaho923ecef2017-10-11 12:01:38 +03004817 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004818 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004819 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004820 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004821 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004822 }
4823
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004824 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4825 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004826 return node;
4827}
4828
4829TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4830{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004831 if (mSwitchNestingLevel == 0)
4832 {
4833 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 return nullptr;
4835 }
4836 if (condition == nullptr)
4837 {
4838 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 return nullptr;
4840 }
4841 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 {
4844 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 }
4846 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004847 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4848 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4849 // fold in case labels.
4850 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 {
4852 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004853 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004854 TIntermCase *node = new TIntermCase(condition);
4855 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004856 return node;
4857}
4858
4859TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4860{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004861 if (mSwitchNestingLevel == 0)
4862 {
4863 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004866 TIntermCase *node = new TIntermCase(nullptr);
4867 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004868 return node;
4869}
4870
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4872 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004873 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004874{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004875 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004876
4877 switch (op)
4878 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004879 case EOpLogicalNot:
4880 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4881 child->isVector())
4882 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004883 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004884 return nullptr;
4885 }
4886 break;
4887 case EOpBitwiseNot:
4888 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4889 child->isMatrix() || child->isArray())
4890 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004891 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 return nullptr;
4893 }
4894 break;
4895 case EOpPostIncrement:
4896 case EOpPreIncrement:
4897 case EOpPostDecrement:
4898 case EOpPreDecrement:
4899 case EOpNegative:
4900 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004901 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4902 child->getBasicType() == EbtBool || child->isArray() ||
4903 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004906 return nullptr;
4907 }
4908 // Operators for built-ins are already type checked against their prototype.
4909 default:
4910 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004911 }
4912
Jiajia Qinbc585152017-06-23 15:42:17 +08004913 if (child->getMemoryQualifier().writeonly)
4914 {
4915 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4916 return nullptr;
4917 }
4918
Olli Etuahof119a262016-08-19 15:54:22 +03004919 TIntermUnary *node = new TIntermUnary(op, child);
4920 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004921
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004922 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004923}
4924
Olli Etuaho09b22472015-02-11 11:47:26 +02004925TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4926{
Olli Etuahocce89652017-06-19 16:04:09 +03004927 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004928 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004929 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004930 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004931 return child;
4932 }
4933 return node;
4934}
4935
Jamie Madillb98c3a82015-07-23 14:26:04 -04004936TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4937 TIntermTyped *child,
4938 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004939{
Olli Etuaho856c4972016-08-08 11:38:39 +03004940 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004941 return addUnaryMath(op, child, loc);
4942}
4943
Olli Etuaho765924f2018-01-04 12:48:36 +02004944TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4945{
4946 // If we can, we should return the folded version of the expression for subsequent parsing. This
4947 // enables folding the containing expression during parsing as well, instead of the separate
4948 // FoldExpressions() step where folding nested expressions requires multiple full AST
4949 // traversals.
4950
4951 // Even if folding fails the fold() functions return some node representing the expression,
4952 // typically the original node. So "folded" can be assumed to be non-null.
4953 TIntermTyped *folded = expression->fold(mDiagnostics);
4954 ASSERT(folded != nullptr);
4955 if (folded->getQualifier() == expression->getQualifier())
4956 {
4957 // We need this expression to have the correct qualifier when validating the consuming
4958 // expression. So we can only return the folded node from here in case it has the same
4959 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4960 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4961 // 1. (true ? 1.0 : non_constant)
4962 // 2. (non_constant, 1.0)
4963 return folded;
4964 }
4965 return expression;
4966}
4967
Jamie Madillb98c3a82015-07-23 14:26:04 -04004968bool TParseContext::binaryOpCommonCheck(TOperator op,
4969 TIntermTyped *left,
4970 TIntermTyped *right,
4971 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004972{
jchen10b4cf5652017-05-05 18:51:17 +08004973 // Check opaque types are not allowed to be operands in expressions other than array indexing
4974 // and structure member selection.
4975 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4976 {
4977 switch (op)
4978 {
4979 case EOpIndexDirect:
4980 case EOpIndexIndirect:
4981 break;
4982 case EOpIndexDirectStruct:
4983 UNREACHABLE();
4984
4985 default:
4986 error(loc, "Invalid operation for variables with an opaque type",
4987 GetOperatorString(op));
4988 return false;
4989 }
4990 }
jchen10cc2a10e2017-05-03 14:05:12 +08004991
Jiajia Qinbc585152017-06-23 15:42:17 +08004992 if (right->getMemoryQualifier().writeonly)
4993 {
4994 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4995 return false;
4996 }
4997
4998 if (left->getMemoryQualifier().writeonly)
4999 {
5000 switch (op)
5001 {
5002 case EOpAssign:
5003 case EOpInitialize:
5004 case EOpIndexDirect:
5005 case EOpIndexIndirect:
5006 case EOpIndexDirectStruct:
5007 case EOpIndexDirectInterfaceBlock:
5008 break;
5009 default:
5010 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5011 return false;
5012 }
5013 }
5014
Olli Etuaho244be012016-08-18 15:26:02 +03005015 if (left->getType().getStruct() || right->getType().getStruct())
5016 {
5017 switch (op)
5018 {
5019 case EOpIndexDirectStruct:
5020 ASSERT(left->getType().getStruct());
5021 break;
5022 case EOpEqual:
5023 case EOpNotEqual:
5024 case EOpAssign:
5025 case EOpInitialize:
5026 if (left->getType() != right->getType())
5027 {
5028 return false;
5029 }
5030 break;
5031 default:
5032 error(loc, "Invalid operation for structs", GetOperatorString(op));
5033 return false;
5034 }
5035 }
5036
Olli Etuaho94050052017-05-08 14:17:44 +03005037 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5038 {
5039 switch (op)
5040 {
5041 case EOpIndexDirectInterfaceBlock:
5042 ASSERT(left->getType().getInterfaceBlock());
5043 break;
5044 default:
5045 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5046 return false;
5047 }
5048 }
5049
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005050 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005051 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005052 error(loc, "array / non-array mismatch", GetOperatorString(op));
5053 return false;
5054 }
5055
5056 if (left->isArray())
5057 {
5058 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005059 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005060 {
5061 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5062 return false;
5063 }
5064
Olli Etuahoe79904c2015-03-18 16:56:42 +02005065 switch (op)
5066 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005067 case EOpEqual:
5068 case EOpNotEqual:
5069 case EOpAssign:
5070 case EOpInitialize:
5071 break;
5072 default:
5073 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5074 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005075 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005076 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005077 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005078 {
5079 error(loc, "array size mismatch", GetOperatorString(op));
5080 return false;
5081 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005082 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005083
5084 // Check ops which require integer / ivec parameters
5085 bool isBitShift = false;
5086 switch (op)
5087 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005088 case EOpBitShiftLeft:
5089 case EOpBitShiftRight:
5090 case EOpBitShiftLeftAssign:
5091 case EOpBitShiftRightAssign:
5092 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5093 // check that the basic type is an integer type.
5094 isBitShift = true;
5095 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5096 {
5097 return false;
5098 }
5099 break;
5100 case EOpBitwiseAnd:
5101 case EOpBitwiseXor:
5102 case EOpBitwiseOr:
5103 case EOpBitwiseAndAssign:
5104 case EOpBitwiseXorAssign:
5105 case EOpBitwiseOrAssign:
5106 // It is enough to check the type of only one operand, since later it
5107 // is checked that the operand types match.
5108 if (!IsInteger(left->getBasicType()))
5109 {
5110 return false;
5111 }
5112 break;
5113 default:
5114 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005115 }
5116
5117 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5118 // So the basic type should usually match.
5119 if (!isBitShift && left->getBasicType() != right->getBasicType())
5120 {
5121 return false;
5122 }
5123
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005124 // Check that:
5125 // 1. Type sizes match exactly on ops that require that.
5126 // 2. Restrictions for structs that contain arrays or samplers are respected.
5127 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005128 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005129 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 case EOpAssign:
5131 case EOpInitialize:
5132 case EOpEqual:
5133 case EOpNotEqual:
5134 // ESSL 1.00 sections 5.7, 5.8, 5.9
5135 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5136 {
5137 error(loc, "undefined operation for structs containing arrays",
5138 GetOperatorString(op));
5139 return false;
5140 }
5141 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5142 // we interpret the spec so that this extends to structs containing samplers,
5143 // similarly to ESSL 1.00 spec.
5144 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5145 left->getType().isStructureContainingSamplers())
5146 {
5147 error(loc, "undefined operation for structs containing samplers",
5148 GetOperatorString(op));
5149 return false;
5150 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005151
Olli Etuahoe1805592017-01-02 16:41:20 +00005152 if ((left->getNominalSize() != right->getNominalSize()) ||
5153 (left->getSecondarySize() != right->getSecondarySize()))
5154 {
5155 error(loc, "dimension mismatch", GetOperatorString(op));
5156 return false;
5157 }
5158 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005159 case EOpLessThan:
5160 case EOpGreaterThan:
5161 case EOpLessThanEqual:
5162 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005163 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005164 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 return false;
5167 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005168 break;
5169 case EOpAdd:
5170 case EOpSub:
5171 case EOpDiv:
5172 case EOpIMod:
5173 case EOpBitShiftLeft:
5174 case EOpBitShiftRight:
5175 case EOpBitwiseAnd:
5176 case EOpBitwiseXor:
5177 case EOpBitwiseOr:
5178 case EOpAddAssign:
5179 case EOpSubAssign:
5180 case EOpDivAssign:
5181 case EOpIModAssign:
5182 case EOpBitShiftLeftAssign:
5183 case EOpBitShiftRightAssign:
5184 case EOpBitwiseAndAssign:
5185 case EOpBitwiseXorAssign:
5186 case EOpBitwiseOrAssign:
5187 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5188 {
5189 return false;
5190 }
5191
5192 // Are the sizes compatible?
5193 if (left->getNominalSize() != right->getNominalSize() ||
5194 left->getSecondarySize() != right->getSecondarySize())
5195 {
5196 // If the nominal sizes of operands do not match:
5197 // One of them must be a scalar.
5198 if (!left->isScalar() && !right->isScalar())
5199 return false;
5200
5201 // In the case of compound assignment other than multiply-assign,
5202 // the right side needs to be a scalar. Otherwise a vector/matrix
5203 // would be assigned to a scalar. A scalar can't be shifted by a
5204 // vector either.
5205 if (!right->isScalar() &&
5206 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5207 return false;
5208 }
5209 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005210 default:
5211 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005212 }
5213
Olli Etuahod6b14282015-03-17 14:31:35 +02005214 return true;
5215}
5216
Olli Etuaho1dded802016-08-18 18:13:13 +03005217bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5218 const TType &left,
5219 const TType &right)
5220{
5221 switch (op)
5222 {
5223 case EOpMul:
5224 case EOpMulAssign:
5225 return left.getNominalSize() == right.getNominalSize() &&
5226 left.getSecondarySize() == right.getSecondarySize();
5227 case EOpVectorTimesScalar:
5228 return true;
5229 case EOpVectorTimesScalarAssign:
5230 ASSERT(!left.isMatrix() && !right.isMatrix());
5231 return left.isVector() && !right.isVector();
5232 case EOpVectorTimesMatrix:
5233 return left.getNominalSize() == right.getRows();
5234 case EOpVectorTimesMatrixAssign:
5235 ASSERT(!left.isMatrix() && right.isMatrix());
5236 return left.isVector() && left.getNominalSize() == right.getRows() &&
5237 left.getNominalSize() == right.getCols();
5238 case EOpMatrixTimesVector:
5239 return left.getCols() == right.getNominalSize();
5240 case EOpMatrixTimesScalar:
5241 return true;
5242 case EOpMatrixTimesScalarAssign:
5243 ASSERT(left.isMatrix() && !right.isMatrix());
5244 return !right.isVector();
5245 case EOpMatrixTimesMatrix:
5246 return left.getCols() == right.getRows();
5247 case EOpMatrixTimesMatrixAssign:
5248 ASSERT(left.isMatrix() && right.isMatrix());
5249 // We need to check two things:
5250 // 1. The matrix multiplication step is valid.
5251 // 2. The result will have the same number of columns as the lvalue.
5252 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5253
5254 default:
5255 UNREACHABLE();
5256 return false;
5257 }
5258}
5259
Jamie Madillb98c3a82015-07-23 14:26:04 -04005260TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5261 TIntermTyped *left,
5262 TIntermTyped *right,
5263 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005264{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005265 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005266 return nullptr;
5267
Olli Etuahofc1806e2015-03-17 13:03:11 +02005268 switch (op)
5269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005270 case EOpEqual:
5271 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272 case EOpLessThan:
5273 case EOpGreaterThan:
5274 case EOpLessThanEqual:
5275 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005276 break;
5277 case EOpLogicalOr:
5278 case EOpLogicalXor:
5279 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005280 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5281 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005282 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 {
5284 return nullptr;
5285 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005286 // Basic types matching should have been already checked.
5287 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 break;
5289 case EOpAdd:
5290 case EOpSub:
5291 case EOpDiv:
5292 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005293 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5294 !right->getType().getStruct());
5295 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 {
5297 return nullptr;
5298 }
5299 break;
5300 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005301 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5302 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005303 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005304 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 {
5306 return nullptr;
5307 }
5308 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 default:
5310 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005311 }
5312
Olli Etuaho1dded802016-08-18 18:13:13 +03005313 if (op == EOpMul)
5314 {
5315 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5316 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5317 {
5318 return nullptr;
5319 }
5320 }
5321
Olli Etuaho3fdec912016-08-18 15:08:06 +03005322 TIntermBinary *node = new TIntermBinary(op, left, right);
5323 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005324 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005325}
5326
Jamie Madillb98c3a82015-07-23 14:26:04 -04005327TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5328 TIntermTyped *left,
5329 TIntermTyped *right,
5330 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005331{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005332 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005333 if (node == 0)
5334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5336 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005337 return left;
5338 }
5339 return node;
5340}
5341
Jamie Madillb98c3a82015-07-23 14:26:04 -04005342TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5343 TIntermTyped *left,
5344 TIntermTyped *right,
5345 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005346{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005347 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005348 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005349 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005350 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5351 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005352 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005353 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005354 }
5355 return node;
5356}
5357
Olli Etuaho13389b62016-10-16 11:48:18 +01005358TIntermBinary *TParseContext::createAssign(TOperator op,
5359 TIntermTyped *left,
5360 TIntermTyped *right,
5361 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005362{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005363 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005364 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005365 if (op == EOpMulAssign)
5366 {
5367 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5368 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5369 {
5370 return nullptr;
5371 }
5372 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005373 TIntermBinary *node = new TIntermBinary(op, left, right);
5374 node->setLine(loc);
5375
Olli Etuaho3fdec912016-08-18 15:08:06 +03005376 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005377 }
5378 return nullptr;
5379}
5380
Jamie Madillb98c3a82015-07-23 14:26:04 -04005381TIntermTyped *TParseContext::addAssign(TOperator op,
5382 TIntermTyped *left,
5383 TIntermTyped *right,
5384 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005385{
Olli Etuahocce89652017-06-19 16:04:09 +03005386 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005387 TIntermTyped *node = createAssign(op, left, right, loc);
5388 if (node == nullptr)
5389 {
5390 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 return left;
5392 }
5393 return node;
5394}
5395
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005396TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5397 TIntermTyped *right,
5398 const TSourceLoc &loc)
5399{
Corentin Wallez0d959252016-07-12 17:26:32 -04005400 // WebGL2 section 5.26, the following results in an error:
5401 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005402 if (mShaderSpec == SH_WEBGL2_SPEC &&
5403 (left->isArray() || left->getBasicType() == EbtVoid ||
5404 left->getType().isStructureContainingArrays() || right->isArray() ||
5405 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005406 {
5407 error(loc,
5408 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5409 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005410 }
5411
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005412 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005413
5414 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005415}
5416
Olli Etuaho49300862015-02-20 14:54:49 +02005417TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5418{
5419 switch (op)
5420 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005421 case EOpContinue:
5422 if (mLoopNestingLevel <= 0)
5423 {
5424 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005425 }
5426 break;
5427 case EOpBreak:
5428 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5429 {
5430 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005431 }
5432 break;
5433 case EOpReturn:
5434 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5435 {
5436 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005437 }
5438 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005439 case EOpKill:
5440 if (mShaderType != GL_FRAGMENT_SHADER)
5441 {
5442 error(loc, "discard supported in fragment shaders only", "discard");
5443 }
5444 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005446 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005447 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005448 }
Olli Etuahocce89652017-06-19 16:04:09 +03005449 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005450}
5451
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005453 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005455{
Olli Etuahocce89652017-06-19 16:04:09 +03005456 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005457 {
Olli Etuahocce89652017-06-19 16:04:09 +03005458 ASSERT(op == EOpReturn);
5459 mFunctionReturnsValue = true;
5460 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5461 {
5462 error(loc, "void function cannot return a value", "return");
5463 }
5464 else if (*mCurrentFunctionType != expression->getType())
5465 {
5466 error(loc, "function return is not matching type:", "return");
5467 }
Olli Etuaho49300862015-02-20 14:54:49 +02005468 }
Olli Etuahocce89652017-06-19 16:04:09 +03005469 TIntermBranch *node = new TIntermBranch(op, expression);
5470 node->setLine(loc);
5471 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005472}
5473
Martin Radev84aa2dc2017-09-11 15:51:02 +03005474void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5475{
5476 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005477 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005478 bool isTextureGather = (name == "textureGather");
5479 bool isTextureGatherOffset = (name == "textureGatherOffset");
5480 if (isTextureGather || isTextureGatherOffset)
5481 {
5482 TIntermNode *componentNode = nullptr;
5483 TIntermSequence *arguments = functionCall->getSequence();
5484 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5485 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5486 ASSERT(sampler != nullptr);
5487 switch (sampler->getBasicType())
5488 {
5489 case EbtSampler2D:
5490 case EbtISampler2D:
5491 case EbtUSampler2D:
5492 case EbtSampler2DArray:
5493 case EbtISampler2DArray:
5494 case EbtUSampler2DArray:
5495 if ((isTextureGather && arguments->size() == 3u) ||
5496 (isTextureGatherOffset && arguments->size() == 4u))
5497 {
5498 componentNode = arguments->back();
5499 }
5500 break;
5501 case EbtSamplerCube:
5502 case EbtISamplerCube:
5503 case EbtUSamplerCube:
5504 ASSERT(!isTextureGatherOffset);
5505 if (arguments->size() == 3u)
5506 {
5507 componentNode = arguments->back();
5508 }
5509 break;
5510 case EbtSampler2DShadow:
5511 case EbtSampler2DArrayShadow:
5512 case EbtSamplerCubeShadow:
5513 break;
5514 default:
5515 UNREACHABLE();
5516 break;
5517 }
5518 if (componentNode)
5519 {
5520 const TIntermConstantUnion *componentConstantUnion =
5521 componentNode->getAsConstantUnion();
5522 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5523 {
5524 error(functionCall->getLine(), "Texture component must be a constant expression",
5525 name.c_str());
5526 }
5527 else
5528 {
5529 int component = componentConstantUnion->getIConst(0);
5530 if (component < 0 || component > 3)
5531 {
5532 error(functionCall->getLine(), "Component must be in the range [0;3]",
5533 name.c_str());
5534 }
5535 }
5536 }
5537 }
5538}
5539
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005540void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5541{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005542 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005543 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005544 TIntermNode *offset = nullptr;
5545 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005546 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005547 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5548 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5549 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005550 {
5551 offset = arguments->back();
5552 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005553 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005554 {
5555 // A bias parameter might follow the offset parameter.
5556 ASSERT(arguments->size() >= 3);
5557 offset = (*arguments)[2];
5558 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005559 else if (name == "textureGatherOffset")
5560 {
5561 ASSERT(arguments->size() >= 3u);
5562 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5563 ASSERT(sampler != nullptr);
5564 switch (sampler->getBasicType())
5565 {
5566 case EbtSampler2D:
5567 case EbtISampler2D:
5568 case EbtUSampler2D:
5569 case EbtSampler2DArray:
5570 case EbtISampler2DArray:
5571 case EbtUSampler2DArray:
5572 offset = (*arguments)[2];
5573 break;
5574 case EbtSampler2DShadow:
5575 case EbtSampler2DArrayShadow:
5576 offset = (*arguments)[3];
5577 break;
5578 default:
5579 UNREACHABLE();
5580 break;
5581 }
5582 useTextureGatherOffsetConstraints = true;
5583 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005584 if (offset != nullptr)
5585 {
5586 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5587 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5588 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005589 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005590 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005591 }
5592 else
5593 {
5594 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5595 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005596 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005597 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5598 : mMinProgramTexelOffset;
5599 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5600 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005601 for (size_t i = 0u; i < size; ++i)
5602 {
5603 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005604 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005605 {
5606 std::stringstream tokenStream;
5607 tokenStream << offsetValue;
5608 std::string token = tokenStream.str();
5609 error(offset->getLine(), "Texture offset value out of valid range",
5610 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005611 }
5612 }
5613 }
5614 }
5615}
5616
Jiajia Qina3106c52017-11-03 09:39:39 +08005617void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5618{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005619 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005620 const TString &functionName = functionCall->getFunction()->name();
5621 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005622 {
5623 TIntermSequence *arguments = functionCall->getSequence();
5624 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5625
5626 if (IsBufferOrSharedVariable(memNode))
5627 {
5628 return;
5629 }
5630
5631 while (memNode->getAsBinaryNode())
5632 {
5633 memNode = memNode->getAsBinaryNode()->getLeft();
5634 if (IsBufferOrSharedVariable(memNode))
5635 {
5636 return;
5637 }
5638 }
5639
5640 error(memNode->getLine(),
5641 "The value passed to the mem argument of an atomic memory function does not "
5642 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005643 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005644 }
5645}
5646
Martin Radev2cc85b32016-08-05 16:22:53 +03005647// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5648void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5649{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005650 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005651 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005652
5653 if (name.compare(0, 5, "image") == 0)
5654 {
5655 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005656 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005657
Olli Etuaho485eefd2017-02-14 17:40:06 +00005658 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005659
5660 if (name.compare(5, 5, "Store") == 0)
5661 {
5662 if (memoryQualifier.readonly)
5663 {
5664 error(imageNode->getLine(),
5665 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005666 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005667 }
5668 }
5669 else if (name.compare(5, 4, "Load") == 0)
5670 {
5671 if (memoryQualifier.writeonly)
5672 {
5673 error(imageNode->getLine(),
5674 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005675 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005676 }
5677 }
5678 }
5679}
5680
5681// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5682void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5683 const TFunction *functionDefinition,
5684 const TIntermAggregate *functionCall)
5685{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005686 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005687
5688 const TIntermSequence &arguments = *functionCall->getSequence();
5689
5690 ASSERT(functionDefinition->getParamCount() == arguments.size());
5691
5692 for (size_t i = 0; i < arguments.size(); ++i)
5693 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005694 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5695 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005696 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5697 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5698
5699 if (IsImage(functionArgumentType.getBasicType()))
5700 {
5701 const TMemoryQualifier &functionArgumentMemoryQualifier =
5702 functionArgumentType.getMemoryQualifier();
5703 const TMemoryQualifier &functionParameterMemoryQualifier =
5704 functionParameterType.getMemoryQualifier();
5705 if (functionArgumentMemoryQualifier.readonly &&
5706 !functionParameterMemoryQualifier.readonly)
5707 {
5708 error(functionCall->getLine(),
5709 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005710 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005711 }
5712
5713 if (functionArgumentMemoryQualifier.writeonly &&
5714 !functionParameterMemoryQualifier.writeonly)
5715 {
5716 error(functionCall->getLine(),
5717 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005718 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005719 }
Martin Radev049edfa2016-11-11 14:35:37 +02005720
5721 if (functionArgumentMemoryQualifier.coherent &&
5722 !functionParameterMemoryQualifier.coherent)
5723 {
5724 error(functionCall->getLine(),
5725 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005726 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005727 }
5728
5729 if (functionArgumentMemoryQualifier.volatileQualifier &&
5730 !functionParameterMemoryQualifier.volatileQualifier)
5731 {
5732 error(functionCall->getLine(),
5733 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005734 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005735 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005736 }
5737 }
5738}
5739
Olli Etuaho95ed1942018-02-01 14:01:19 +02005740TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005741{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005742 if (fnCall->thisNode() != nullptr)
5743 {
5744 return addMethod(fnCall, loc);
5745 }
5746 if (fnCall->isConstructor())
5747 {
5748 return addConstructor(fnCall, loc);
5749 }
5750 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005751}
5752
Olli Etuaho95ed1942018-02-01 14:01:19 +02005753TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005754{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005755 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005756 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5757 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5758 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005759 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005760 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005762 error(loc, "invalid method", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005763 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005764 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005765 {
5766 error(loc, "method takes no parameters", "length");
5767 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005768 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769 {
5770 error(loc, "length can only be called on arrays", "length");
5771 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005772 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005773 mGeometryShaderInputPrimitiveType == EptUndefined)
5774 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005775 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005776 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5777 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005778 else
5779 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005780 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005781 node->setLine(loc);
5782 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005784 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005785}
5786
Olli Etuaho95ed1942018-02-01 14:01:19 +02005787TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005788 const TSourceLoc &loc)
5789{
5790 // First find by unmangled name to check whether the function name has been
5791 // hidden by a variable name or struct typename.
5792 // If a function is found, check for one with a matching argument list.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005793 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005794 if (symbol != nullptr && !symbol->isFunction())
5795 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005796 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005797 }
5798 else
5799 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005800 symbol = symbolTable.find(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005802 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005803 error(loc, "no matching overloaded function found", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005804 }
5805 else
5806 {
5807 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005808 //
5809 // A declared function.
5810 //
Olli Etuaho37b697e2018-01-29 12:19:27 +02005811 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005812 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005813 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005814 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005815 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuaho95ed1942018-02-01 14:01:19 +02005816 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005817 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005818 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005819 if (fnCandidate->getParamCount() == 1)
5820 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005821 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005822 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005823 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005824 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005825 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005826 }
5827 else
5828 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829 TIntermAggregate *callNode =
Olli Etuaho95ed1942018-02-01 14:01:19 +02005830 TIntermAggregate::Create(*fnCandidate, op, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005831 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005832
5833 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005834 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305835
Olli Etuaho765924f2018-01-04 12:48:36 +02005836 // See if we can constant fold a built-in. Note that this may be possible
5837 // even if it is not const-qualified.
5838 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 }
5840 }
5841 else
5842 {
Olli Etuaho37b697e2018-01-29 12:19:27 +02005843 // This is a real function call.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005844 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845
Olli Etuaho37b697e2018-01-29 12:19:27 +02005846 // If the symbol type is not BuiltIn, the function is user defined - could be an
5847 // overloaded built-in as well. if the symbol type is BuiltIn, it's a built-in
5848 // function with no op associated with it.
5849 if (fnCandidate->symbolType() == SymbolType::BuiltIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005850 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005851 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate,
5852 &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005853 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005854 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005856 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005857 }
5858 else
5859 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005860 callNode =
5861 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005862 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005863 }
5864
Jiajia Qinbc585152017-06-23 15:42:17 +08005865 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005866
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005867 callNode->setLine(loc);
5868
5869 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 }
5871 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005872 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005873
5874 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005875 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005876}
5877
Jamie Madillb98c3a82015-07-23 14:26:04 -04005878TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005879 TIntermTyped *trueExpression,
5880 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005881 const TSourceLoc &loc)
5882{
Olli Etuaho56229f12017-07-10 14:16:33 +03005883 if (!checkIsScalarBool(loc, cond))
5884 {
5885 return falseExpression;
5886 }
Olli Etuaho52901742015-04-15 13:42:45 +03005887
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005888 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005889 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005890 std::stringstream reasonStream;
5891 reasonStream << "mismatching ternary operator operand types '"
5892 << trueExpression->getCompleteString() << " and '"
5893 << falseExpression->getCompleteString() << "'";
5894 std::string reason = reasonStream.str();
5895 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005896 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005897 }
Olli Etuahode318b22016-10-25 16:18:25 +01005898 if (IsOpaqueType(trueExpression->getBasicType()))
5899 {
5900 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005901 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005902 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5903 // Note that structs containing opaque types don't need to be checked as structs are
5904 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005905 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005906 return falseExpression;
5907 }
5908
Jiajia Qinbc585152017-06-23 15:42:17 +08005909 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5910 falseExpression->getMemoryQualifier().writeonly)
5911 {
5912 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5913 return falseExpression;
5914 }
5915
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005916 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005917 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005918 // ESSL 3.00.6 section 5.7:
5919 // Ternary operator support is optional for arrays. No certainty that it works across all
5920 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5921 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005922 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005923 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005924 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005925 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005926 }
Olli Etuaho94050052017-05-08 14:17:44 +03005927 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5928 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005929 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005930 return falseExpression;
5931 }
5932
Corentin Wallez0d959252016-07-12 17:26:32 -04005933 // WebGL2 section 5.26, the following results in an error:
5934 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005935 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005936 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005937 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005938 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005939 }
5940
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005941 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5942 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005943 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005944}
Olli Etuaho49300862015-02-20 14:54:49 +02005945
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005946//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005947// Parse an array of strings using yyparse.
5948//
5949// Returns 0 for success.
5950//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005951int PaParseStrings(size_t count,
5952 const char *const string[],
5953 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305954 TParseContext *context)
5955{
Yunchao He4f285442017-04-21 12:15:49 +08005956 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005957 return 1;
5958
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005959 if (glslang_initialize(context))
5960 return 1;
5961
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005962 int error = glslang_scan(count, string, length, context);
5963 if (!error)
5964 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005965
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005966 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005967
alokp@chromium.org6b495712012-06-29 00:06:58 +00005968 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005969}
Jamie Madill45bcc782016-11-07 13:58:48 -05005970
5971} // namespace sh