blob: 81c72d4f7c5c4ed58d03eeed9464ba85bf7a9810 [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 Etuaho2bfe9f62018-03-02 16:53:29 +020015#include "compiler/translator/ParseContext_autogen.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 Etuahoc26214d2018-03-16 10:43:11 +020020#include "compiler/translator/tree_util/IntermNode_util.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030021#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000022
Jamie Madill45bcc782016-11-07 13:58:48 -050023namespace sh
24{
25
alokp@chromium.org8b851c62012-06-15 16:25:11 +000026///////////////////////////////////////////////////////////////////////
27//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000028// Sub- vector and matrix fields
29//
30////////////////////////////////////////////////////////////////////////
31
Martin Radev2cc85b32016-08-05 16:22:53 +030032namespace
33{
34
35const int kWebGLMaxStructNesting = 4;
36
Olli Etuaho0f684632017-07-13 12:42:15 +030037bool ContainsSampler(const TStructure *structType);
38
Martin Radev2cc85b32016-08-05 16:22:53 +030039bool ContainsSampler(const TType &type)
40{
41 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030042 {
Martin Radev2cc85b32016-08-05 16:22:53 +030043 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030044 }
jchen10cc2a10e2017-05-03 14:05:12 +080045 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030046 {
Olli Etuaho0f684632017-07-13 12:42:15 +030047 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030048 }
49
50 return false;
51}
52
Olli Etuaho0f684632017-07-13 12:42:15 +030053bool ContainsSampler(const TStructure *structType)
54{
55 for (const auto &field : structType->fields())
56 {
57 if (ContainsSampler(*field->type()))
58 return true;
59 }
60 return false;
61}
62
Olli Etuaho485eefd2017-02-14 17:40:06 +000063// Get a token from an image argument to use as an error message token.
64const char *GetImageArgumentToken(TIntermTyped *imageNode)
65{
66 ASSERT(IsImage(imageNode->getBasicType()));
67 while (imageNode->getAsBinaryNode() &&
68 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
69 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
70 {
71 imageNode = imageNode->getAsBinaryNode()->getLeft();
72 }
73 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
74 if (imageSymbol)
75 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020076 return imageSymbol->getName().data();
Olli Etuaho485eefd2017-02-14 17:40:06 +000077 }
78 return "image";
79}
80
Olli Etuahocce89652017-06-19 16:04:09 +030081bool CanSetDefaultPrecisionOnType(const TPublicType &type)
82{
83 if (!SupportsPrecision(type.getBasicType()))
84 {
85 return false;
86 }
87 if (type.getBasicType() == EbtUInt)
88 {
89 // ESSL 3.00.4 section 4.5.4
90 return false;
91 }
92 if (type.isAggregate())
93 {
94 // Not allowed to set for aggregate types
95 return false;
96 }
97 return true;
98}
99
Jiawei Shaod8105a02017-08-08 09:54:36 +0800100// Map input primitive types to input array sizes in a geometry shader.
101GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
102{
103 switch (primitiveType)
104 {
105 case EptPoints:
106 return 1u;
107 case EptLines:
108 return 2u;
109 case EptTriangles:
110 return 3u;
111 case EptLinesAdjacency:
112 return 4u;
113 case EptTrianglesAdjacency:
114 return 6u;
115 default:
116 UNREACHABLE();
117 return 0u;
118 }
119}
120
Jiajia Qina3106c52017-11-03 09:39:39 +0800121bool IsBufferOrSharedVariable(TIntermTyped *var)
122{
123 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
124 var->getQualifier() == EvqShared)
125 {
126 return true;
127 }
128 return false;
129}
130
Martin Radev2cc85b32016-08-05 16:22:53 +0300131} // namespace
132
jchen104cdac9e2017-05-08 11:01:20 +0800133// This tracks each binding point's current default offset for inheritance of subsequent
134// variables using the same binding, and keeps offsets unique and non overlapping.
135// See GLSL ES 3.1, section 4.4.6.
136class TParseContext::AtomicCounterBindingState
137{
138 public:
139 AtomicCounterBindingState() : mDefaultOffset(0) {}
140 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
141 // newly inserted span.
142 int insertSpan(int start, size_t length)
143 {
144 gl::RangeI newSpan(start, start + static_cast<int>(length));
145 for (const auto &span : mSpans)
146 {
147 if (newSpan.intersects(span))
148 {
149 return -1;
150 }
151 }
152 mSpans.push_back(newSpan);
153 mDefaultOffset = newSpan.high();
154 return start;
155 }
156 // Inserts a new span starting from the default offset.
157 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
158 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
159
160 private:
161 int mDefaultOffset;
162 std::vector<gl::RangeI> mSpans;
163};
164
Jamie Madillacb4b812016-11-07 13:50:29 -0500165TParseContext::TParseContext(TSymbolTable &symt,
166 TExtensionBehavior &ext,
167 sh::GLenum type,
168 ShShaderSpec spec,
169 ShCompileOptions options,
170 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000171 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500172 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300173 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300174 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500175 mShaderType(type),
176 mShaderSpec(spec),
177 mCompileOptions(options),
178 mShaderVersion(100),
179 mTreeRoot(nullptr),
180 mLoopNestingLevel(0),
181 mStructNestingLevel(0),
182 mSwitchNestingLevel(0),
183 mCurrentFunctionType(nullptr),
184 mFunctionReturnsValue(false),
185 mChecksPrecisionErrors(checksPrecErrors),
186 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800187 mDefaultUniformMatrixPacking(EmpColumnMajor),
188 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
189 mDefaultBufferMatrixPacking(EmpColumnMajor),
190 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000191 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500192 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000193 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500194 mShaderVersion,
195 mShaderType,
196 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000197 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 mScanner(nullptr),
199 mUsesFragData(false),
200 mUsesFragColor(false),
201 mUsesSecondaryOutputs(false),
202 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
203 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300204 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
205 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500206 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500207 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000208 mNumViews(-1),
209 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000210 mMaxImageUnits(resources.MaxImageUnits),
211 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000212 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800213 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800214 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800215 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800216 mDeclaringFunction(false),
217 mGeometryShaderInputPrimitiveType(EptUndefined),
218 mGeometryShaderOutputPrimitiveType(EptUndefined),
219 mGeometryShaderInvocations(0),
220 mGeometryShaderMaxVertices(-1),
221 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Olli Etuaho94bbed12018-03-20 14:44:53 +0200222 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices)
Jamie Madillacb4b812016-11-07 13:50:29 -0500223{
Jamie Madillacb4b812016-11-07 13:50:29 -0500224}
225
jchen104cdac9e2017-05-08 11:01:20 +0800226TParseContext::~TParseContext()
227{
228}
229
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300230bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200231 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400232 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300233 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300235 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200236 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300237 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530238 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200239 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000240 return false;
241 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300242 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 enum
245 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000246 exyzw,
247 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000248 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000249 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530252 {
253 switch (compString[i])
254 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400255 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300256 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400257 fieldSet[i] = exyzw;
258 break;
259 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300260 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400261 fieldSet[i] = ergba;
262 break;
263 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300264 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400265 fieldSet[i] = estpq;
266 break;
267 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300268 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400269 fieldSet[i] = exyzw;
270 break;
271 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = ergba;
274 break;
275 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = estpq;
278 break;
279 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = exyzw;
282 break;
283 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = ergba;
286 break;
287 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = estpq;
290 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530291
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300293 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400294 fieldSet[i] = exyzw;
295 break;
296 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300297 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400298 fieldSet[i] = ergba;
299 break;
300 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300301 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400302 fieldSet[i] = estpq;
303 break;
304 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200305 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400306 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000307 }
308 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300310 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530311 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300312 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200314 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000315 return false;
316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317
Arun Patole7e7e68d2015-05-22 12:02:25 +0530318 if (i > 0)
319 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400320 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200322 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 return false;
324 }
325 }
326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329}
330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331///////////////////////////////////////////////////////////////////////
332//
333// Errors
334//
335////////////////////////////////////////////////////////////////////////
336
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337//
338// Used by flex/bison to output all syntax and parsing errors.
339//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000340void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000342 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343}
344
Olli Etuahofbb1c792018-01-19 16:26:59 +0200345void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
346{
347 mDiagnostics->error(loc, reason, token.data());
348}
349
Olli Etuaho4de340a2016-12-16 09:32:03 +0000350void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530351{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000352 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000353}
354
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200355void TParseContext::outOfRangeError(bool isError,
356 const TSourceLoc &loc,
357 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000358 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200359{
360 if (isError)
361 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200363 }
364 else
365 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000366 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367 }
368}
369
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370//
371// Same error message for all places assignments don't work.
372//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530373void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000375 std::stringstream reasonStream;
376 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
377 std::string reason = reasonStream.str();
378 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000379}
380
381//
382// Same error message for all places unary operations don't work.
383//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530384void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000386 std::stringstream reasonStream;
387 reasonStream << "wrong operand type - no operation '" << op
388 << "' exists that takes an operand of type " << operand
389 << " (or there is no acceptable conversion)";
390 std::string reason = reasonStream.str();
391 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
394//
395// Same error message for all binary operations don't work.
396//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400397void TParseContext::binaryOpError(const TSourceLoc &line,
398 const char *op,
399 TString left,
400 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000402 std::stringstream reasonStream;
403 reasonStream << "wrong operand types - no operation '" << op
404 << "' exists that takes a left-hand operand of type '" << left
405 << "' and a right operand of type '" << right
406 << "' (or there is no acceptable conversion)";
407 std::string reason = reasonStream.str();
408 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409}
410
Olli Etuaho856c4972016-08-08 11:38:39 +0300411void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
412 TPrecision precision,
413 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530414{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400415 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300416 return;
Martin Radev70866b82016-07-22 15:27:42 +0300417
418 if (precision != EbpUndefined && !SupportsPrecision(type))
419 {
420 error(line, "illegal type for precision qualifier", getBasicString(type));
421 }
422
Olli Etuaho183d7e22015-11-20 15:59:09 +0200423 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530424 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200425 switch (type)
426 {
427 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400428 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300429 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200430 case EbtInt:
431 case EbtUInt:
432 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400433 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300434 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800436 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 {
jchen10cc2a10e2017-05-03 14:05:12 +0800438 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300439 return;
440 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200441 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000442 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000443}
444
Olli Etuaho94bbed12018-03-20 14:44:53 +0200445void TParseContext::markStaticReadIfSymbol(TIntermNode *node)
446{
447 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
448 if (swizzleNode)
449 {
450 markStaticReadIfSymbol(swizzleNode->getOperand());
451 return;
452 }
453 TIntermBinary *binaryNode = node->getAsBinaryNode();
454 if (binaryNode)
455 {
456 switch (binaryNode->getOp())
457 {
458 case EOpIndexDirect:
459 case EOpIndexIndirect:
460 case EOpIndexDirectStruct:
461 case EOpIndexDirectInterfaceBlock:
462 markStaticReadIfSymbol(binaryNode->getLeft());
463 return;
464 default:
465 return;
466 }
467 }
468 TIntermSymbol *symbolNode = node->getAsSymbolNode();
469 if (symbolNode)
470 {
471 symbolTable.markStaticRead(symbolNode->variable());
472 }
473}
474
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475// Both test and if necessary, spit out an error, to see if the node is really
476// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300477bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478{
Olli Etuahob6fa0432016-09-28 16:28:05 +0100479 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100480 if (swizzleNode)
481 {
482 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
483 if (ok && swizzleNode->hasDuplicateOffsets())
484 {
485 error(line, " l-value of swizzle cannot have duplicate components", op);
486 return false;
487 }
488 return ok;
489 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
Olli Etuahodaf120b2018-03-20 14:21:10 +0200491 TIntermBinary *binaryNode = node->getAsBinaryNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 if (binaryNode)
493 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400494 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530495 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400496 case EOpIndexDirect:
497 case EOpIndexIndirect:
498 case EOpIndexDirectStruct:
499 case EOpIndexDirectInterfaceBlock:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800500 if (node->getMemoryQualifier().readonly)
501 {
502 error(line, "can't modify a readonly variable", op);
503 return false;
504 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300505 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 default:
507 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000509 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300510 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512
jchen10cc2a10e2017-05-03 14:05:12 +0800513 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530514 switch (node->getQualifier())
515 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400516 case EvqConst:
517 message = "can't modify a const";
518 break;
519 case EvqConstReadOnly:
520 message = "can't modify a const";
521 break;
522 case EvqAttribute:
523 message = "can't modify an attribute";
524 break;
525 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400526 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800527 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800528 case EvqFlatIn:
529 case EvqSmoothIn:
530 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400531 message = "can't modify an input";
532 break;
533 case EvqUniform:
534 message = "can't modify a uniform";
535 break;
536 case EvqVaryingIn:
537 message = "can't modify a varying";
538 break;
539 case EvqFragCoord:
540 message = "can't modify gl_FragCoord";
541 break;
542 case EvqFrontFacing:
543 message = "can't modify gl_FrontFacing";
544 break;
545 case EvqPointCoord:
546 message = "can't modify gl_PointCoord";
547 break;
Martin Radevb0883602016-08-04 17:48:58 +0300548 case EvqNumWorkGroups:
549 message = "can't modify gl_NumWorkGroups";
550 break;
551 case EvqWorkGroupSize:
552 message = "can't modify gl_WorkGroupSize";
553 break;
554 case EvqWorkGroupID:
555 message = "can't modify gl_WorkGroupID";
556 break;
557 case EvqLocalInvocationID:
558 message = "can't modify gl_LocalInvocationID";
559 break;
560 case EvqGlobalInvocationID:
561 message = "can't modify gl_GlobalInvocationID";
562 break;
563 case EvqLocalInvocationIndex:
564 message = "can't modify gl_LocalInvocationIndex";
565 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300566 case EvqViewIDOVR:
567 message = "can't modify gl_ViewID_OVR";
568 break;
Martin Radev802abe02016-08-04 17:48:32 +0300569 case EvqComputeIn:
570 message = "can't modify work group size variable";
571 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800572 case EvqPerVertexIn:
573 message = "can't modify any member in gl_in";
574 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800575 case EvqPrimitiveIDIn:
576 message = "can't modify gl_PrimitiveIDIn";
577 break;
578 case EvqInvocationID:
579 message = "can't modify gl_InvocationID";
580 break;
581 case EvqPrimitiveID:
582 if (mShaderType == GL_FRAGMENT_SHADER)
583 {
584 message = "can't modify gl_PrimitiveID in a fragment shader";
585 }
586 break;
587 case EvqLayer:
588 if (mShaderType == GL_FRAGMENT_SHADER)
589 {
590 message = "can't modify gl_Layer in a fragment shader";
591 }
592 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400593 default:
594 //
595 // Type that can't be written to?
596 //
597 if (node->getBasicType() == EbtVoid)
598 {
599 message = "can't modify void";
600 }
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400602 {
jchen10cc2a10e2017-05-03 14:05:12 +0800603 message = "can't modify a variable with type ";
604 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300605 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800606 else if (node->getMemoryQualifier().readonly)
607 {
608 message = "can't modify a readonly variable";
609 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611
Olli Etuahodaf120b2018-03-20 14:21:10 +0200612 ASSERT(binaryNode == nullptr && swizzleNode == nullptr);
613 TIntermSymbol *symNode = node->getAsSymbolNode();
614 if (message.empty() && symNode != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200616 symbolTable.markStaticWrite(symNode->variable());
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return true;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000618 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200619
620 std::stringstream reasonStream;
621 reasonStream << "l-value required";
622 if (!message.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530623 {
Olli Etuahodaf120b2018-03-20 14:21:10 +0200624 if (symNode)
625 {
626 // Symbol inside an expression can't be nameless.
627 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
628 const ImmutableString &symbol = symNode->getName();
629 reasonStream << " (" << message << " \"" << symbol << "\")";
630 }
631 else
632 {
633 reasonStream << " (" << message << ")";
634 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000635 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200636 std::string reason = reasonStream.str();
637 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638
Olli Etuaho8a176262016-08-16 14:23:01 +0300639 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640}
641
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642// Both test, and if necessary spit out an error, to see if the node is really
643// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300644void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645{
Olli Etuaho383b7912016-08-05 11:22:59 +0300646 if (node->getQualifier() != EvqConst)
647 {
648 error(node->getLine(), "constant expression required", "");
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652// Both test, and if necessary spit out an error, to see if the node is really
653// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300654void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655{
Olli Etuaho383b7912016-08-05 11:22:59 +0300656 if (!node->isScalarInt())
657 {
658 error(node->getLine(), "integer expression required", token);
659 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660}
661
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662// Both test, and if necessary spit out an error, to see if we are currently
663// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800664bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665{
Olli Etuaho856c4972016-08-08 11:38:39 +0300666 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300667 {
668 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800669 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300670 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800671 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672}
673
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300674// ESSL 3.00.5 sections 3.8 and 3.9.
675// If it starts "gl_" or contains two consecutive underscores, it's reserved.
676// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200677bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530679 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200680 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530681 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300682 error(line, reservedErrMsg, "gl_");
683 return false;
684 }
685 if (sh::IsWebGLBasedSpec(mShaderSpec))
686 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200687 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530688 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300689 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200692 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530693 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300694 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300695 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 }
697 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200698 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300699 {
700 error(line,
701 "identifiers containing two consecutive underscores (__) are reserved as "
702 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200703 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300704 return false;
705 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300706 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707}
708
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300709// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300710bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200711 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300712 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200714 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530715 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200716 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300717 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000718 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200719
Olli Etuaho95ed1942018-02-01 14:01:19 +0200720 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530721 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200722 markStaticReadIfSymbol(arg);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300723 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200724 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300725 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200726 {
jchen10cc2a10e2017-05-03 14:05:12 +0800727 std::string reason("cannot convert a variable with type ");
728 reason += getBasicString(argTyped->getBasicType());
729 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300730 return false;
731 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800732 else if (argTyped->getMemoryQualifier().writeonly)
733 {
734 error(line, "cannot convert a variable with writeonly", "constructor");
735 return false;
736 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200737 if (argTyped->getBasicType() == EbtVoid)
738 {
739 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300740 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200741 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
Olli Etuaho856c4972016-08-08 11:38:39 +0300744 if (type.isArray())
745 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 // The size of an unsized constructor should already have been determined.
747 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200748 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300749 {
750 error(line, "array constructor needs one argument per array element", "constructor");
751 return false;
752 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
754 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200755 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300756 {
757 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300758 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500759 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300760 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500761 return false;
762 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300763 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300764 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000765 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300766 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300767 }
768 }
769 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300770 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 {
772 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200773 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300774 {
775 error(line,
776 "Number of constructor parameters does not match the number of structure fields",
777 "constructor");
778 return false;
779 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300780
781 for (size_t i = 0; i < fields.size(); i++)
782 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200783 if (i >= arguments.size() ||
784 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300785 {
786 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000787 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300788 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300789 }
790 }
791 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300792 else
793 {
794 // We're constructing a scalar, vector, or matrix.
795
796 // Note: It's okay to have too many components available, but not okay to have unused
797 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
798 // there is an extra argument, so 'overFull' will become true.
799
800 size_t size = 0;
801 bool full = false;
802 bool overFull = false;
803 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200804 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300805 {
806 const TIntermTyped *argTyped = arg->getAsTyped();
807 ASSERT(argTyped != nullptr);
808
Olli Etuaho487b63a2017-05-23 15:55:09 +0300809 if (argTyped->getBasicType() == EbtStruct)
810 {
811 error(line, "a struct cannot be used as a constructor argument for this type",
812 "constructor");
813 return false;
814 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300815 if (argTyped->getType().isArray())
816 {
817 error(line, "constructing from a non-dereferenced array", "constructor");
818 return false;
819 }
820 if (argTyped->getType().isMatrix())
821 {
822 matrixArg = true;
823 }
824
825 size += argTyped->getType().getObjectSize();
826 if (full)
827 {
828 overFull = true;
829 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300830 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300831 {
832 full = true;
833 }
834 }
835
836 if (type.isMatrix() && matrixArg)
837 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200838 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300839 {
840 error(line, "constructing matrix from matrix can only take one argument",
841 "constructor");
842 return false;
843 }
844 }
845 else
846 {
847 if (size != 1 && size < type.getObjectSize())
848 {
849 error(line, "not enough data provided for construction", "constructor");
850 return false;
851 }
852 if (overFull)
853 {
854 error(line, "too many arguments", "constructor");
855 return false;
856 }
857 }
858 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300859
Olli Etuaho8a176262016-08-16 14:23:01 +0300860 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
Jamie Madillb98c3a82015-07-23 14:26:04 -0400863// This function checks to see if a void variable has been declared and raise an error message for
864// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865//
866// returns true in case of an error
867//
Olli Etuaho856c4972016-08-08 11:38:39 +0300868bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200869 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400870 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300872 if (type == EbtVoid)
873 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200874 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300875 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300876 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
Olli Etuaho8a176262016-08-16 14:23:01 +0300878 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
Jamie Madillb98c3a82015-07-23 14:26:04 -0400881// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300882// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300883bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300885 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000887 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300888 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530889 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300890 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
Jamie Madillb98c3a82015-07-23 14:26:04 -0400893// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300894// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300895void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896{
Martin Radev4a9cd802016-09-01 16:51:51 +0300897 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530898 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000899 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530900 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
jchen10cc2a10e2017-05-03 14:05:12 +0800903bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
904 const TTypeSpecifierNonArray &pType,
905 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530907 if (pType.type == EbtStruct)
908 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300909 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530910 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000911 std::stringstream reasonStream;
912 reasonStream << reason << " (structure contains a sampler)";
913 std::string reasonStr = reasonStream.str();
914 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 }
jchen10cc2a10e2017-05-03 14:05:12 +0800917 // only samplers need to be checked from structs, since other opaque types can't be struct
918 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300919 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530920 }
jchen10cc2a10e2017-05-03 14:05:12 +0800921 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530922 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000923 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
Olli Etuaho856c4972016-08-08 11:38:39 +0300930void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
931 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400932{
933 if (pType.layoutQualifier.location != -1)
934 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400935 error(line, "location must only be specified for a single input or output variable",
936 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400937 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400938}
939
Olli Etuaho856c4972016-08-08 11:38:39 +0300940void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
941 const TLayoutQualifier &layoutQualifier)
942{
943 if (layoutQualifier.location != -1)
944 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000945 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
946 if (mShaderVersion >= 310)
947 {
948 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800949 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000950 }
951 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300952 }
953}
954
Qin Jiajiaca68d982017-09-18 16:41:56 +0800955void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
956 const TLayoutBlockStorage &blockStorage,
957 const TQualifier &qualifier)
958{
959 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
960 {
961 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
962 }
963}
964
Martin Radev2cc85b32016-08-05 16:22:53 +0300965void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
966 TQualifier qualifier,
967 const TType &type)
968{
Martin Radev2cc85b32016-08-05 16:22:53 +0300969 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800970 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530971 {
jchen10cc2a10e2017-05-03 14:05:12 +0800972 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300977unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000980
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200981 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
982 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
983 // fold as array size.
984 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000986 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300987 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000988 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
Olli Etuaho856c4972016-08-08 11:38:39 +0300990 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400991
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000992 if (constant->getBasicType() == EbtUInt)
993 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300994 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000995 }
996 else
997 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000999
Olli Etuaho856c4972016-08-08 11:38:39 +03001000 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001001 {
Nicolas Capens906744a2014-06-06 15:18:07 -04001002 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001004 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 }
1008
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001010 {
1011 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001012 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001013 }
1014
1015 // The size of arrays is restricted here to prevent issues further down the
1016 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1017 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1018 const unsigned int sizeLimit = 65536;
1019
Olli Etuaho856c4972016-08-08 11:38:39 +03001020 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001021 {
1022 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001023 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001024 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001025
1026 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001030bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1031 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032{
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 if ((elementQualifier.qualifier == EvqAttribute) ||
1034 (elementQualifier.qualifier == EvqVertexIn) ||
1035 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001036 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001037 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001038 TType(elementQualifier).getQualifierString());
1039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041
Olli Etuaho8a176262016-08-16 14:23:01 +03001042 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043}
1044
Olli Etuaho8a176262016-08-16 14:23:01 +03001045// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001046bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1047 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001049 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001050 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001051 error(line, "cannot declare arrays of arrays",
1052 TType(elementType).getCompleteString().c_str());
1053 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001054 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001055 return true;
1056}
1057
1058// Check if this qualified element type can be formed into an array. This is only called when array
1059// brackets are associated with an identifier in a declaration, like this:
1060// float a[2];
1061// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1062// are associated with the type, like this:
1063// float[2] a;
1064bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1065 const TPublicType &elementType)
1066{
1067 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1068 {
1069 return false;
1070 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001071 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1072 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1073 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001074 // Geometry shader requires each user-defined input be declared as arrays or inside input
1075 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1076 // interface matching, such variables and blocks are treated as though they were not declared
1077 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001078 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001079 sh::IsVarying(elementType.qualifier) &&
1080 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001081 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001082 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001083 TType(elementType).getCompleteString().c_str());
1084 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001085 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001086 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001090void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001091 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093{
Olli Etuaho3739d232015-04-08 12:23:44 +03001094 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001095 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001096 {
1097 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001098 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001099
1100 // Generate informative error messages for ESSL1.
1101 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001102 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001103 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001105 "structures containing arrays may not be declared constant since they cannot be "
1106 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001107 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001108 }
1109 else
1110 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001111 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001112 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001114 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001115 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1116 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117}
1118
Olli Etuaho2935c582015-04-08 14:32:06 +03001119// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120// and update the symbol table.
1121//
Olli Etuaho2935c582015-04-08 14:32:06 +03001122// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001125 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001126 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128{
Olli Etuaho2935c582015-04-08 14:32:06 +03001129 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130
Olli Etuahofbb1c792018-01-19 16:26:59 +02001131 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001132
Olli Etuahob60d30f2018-01-16 12:31:06 +02001133 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001134
Olli Etuaho856c4972016-08-08 11:38:39 +03001135 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136
Olli Etuaho2935c582015-04-08 14:32:06 +03001137 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001138 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001139 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001140 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001141 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001142 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001143 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001144 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001145 return false;
1146 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001147 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001148 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001149 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001150 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001152 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001153 }
1154 }
1155 else
1156 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001158 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001159 return false;
1160 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001161 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
Olli Etuaho8a176262016-08-16 14:23:01 +03001163 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001164 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165
Olli Etuaho437664b2018-02-28 15:38:14 +02001166 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001167 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001168 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001169 return false;
1170 }
1171
Olli Etuahob60d30f2018-01-16 12:31:06 +02001172 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001173 return false;
1174
1175 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176}
1177
Martin Radev70866b82016-07-22 15:27:42 +03001178void TParseContext::checkIsParameterQualifierValid(
1179 const TSourceLoc &line,
1180 const TTypeQualifierBuilder &typeQualifierBuilder,
1181 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301182{
Olli Etuahocce89652017-06-19 16:04:09 +03001183 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001184 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001185
1186 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301187 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001188 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1189 }
1190
1191 if (!IsImage(type->getBasicType()))
1192 {
Olli Etuaho43364892017-02-13 16:00:12 +00001193 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001194 }
1195 else
1196 {
1197 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001199
Martin Radev70866b82016-07-22 15:27:42 +03001200 type->setQualifier(typeQualifier.qualifier);
1201
1202 if (typeQualifier.precision != EbpUndefined)
1203 {
1204 type->setPrecision(typeQualifier.precision);
1205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206}
1207
Olli Etuaho703671e2017-11-08 17:47:18 +02001208template <size_t size>
1209bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1210 const std::array<TExtension, size> &extensions)
1211{
1212 ASSERT(!extensions.empty());
1213 const TExtensionBehavior &extBehavior = extensionBehavior();
1214
1215 bool canUseWithWarning = false;
1216 bool canUseWithoutWarning = false;
1217
1218 const char *errorMsgString = "";
1219 TExtension errorMsgExtension = TExtension::UNDEFINED;
1220
1221 for (TExtension extension : extensions)
1222 {
1223 auto extIter = extBehavior.find(extension);
1224 if (canUseWithWarning)
1225 {
1226 // We already have an extension that we can use, but with a warning.
1227 // See if we can use the alternative extension without a warning.
1228 if (extIter == extBehavior.end())
1229 {
1230 continue;
1231 }
1232 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1233 {
1234 canUseWithoutWarning = true;
1235 break;
1236 }
1237 continue;
1238 }
1239 if (extIter == extBehavior.end())
1240 {
1241 errorMsgString = "extension is not supported";
1242 errorMsgExtension = extension;
1243 }
1244 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1245 {
1246 errorMsgString = "extension is disabled";
1247 errorMsgExtension = extension;
1248 }
1249 else if (extIter->second == EBhWarn)
1250 {
1251 errorMsgExtension = extension;
1252 canUseWithWarning = true;
1253 }
1254 else
1255 {
1256 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1257 canUseWithoutWarning = true;
1258 break;
1259 }
1260 }
1261
1262 if (canUseWithoutWarning)
1263 {
1264 return true;
1265 }
1266 if (canUseWithWarning)
1267 {
1268 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1269 return true;
1270 }
1271 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1272 return false;
1273}
1274
1275template bool TParseContext::checkCanUseOneOfExtensions(
1276 const TSourceLoc &line,
1277 const std::array<TExtension, 1> &extensions);
1278template bool TParseContext::checkCanUseOneOfExtensions(
1279 const TSourceLoc &line,
1280 const std::array<TExtension, 2> &extensions);
1281template bool TParseContext::checkCanUseOneOfExtensions(
1282 const TSourceLoc &line,
1283 const std::array<TExtension, 3> &extensions);
1284
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001285bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001286{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001287 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001288 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289}
1290
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001291// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1292// compile-time or link-time errors are the same whether or not the declaration is empty".
1293// This function implements all the checks that are done on qualifiers regardless of if the
1294// declaration is empty.
1295void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1296 const sh::TLayoutQualifier &layoutQualifier,
1297 const TSourceLoc &location)
1298{
1299 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1300 {
1301 error(location, "Shared memory declarations cannot have layout specified", "layout");
1302 }
1303
1304 if (layoutQualifier.matrixPacking != EmpUnspecified)
1305 {
1306 error(location, "layout qualifier only valid for interface blocks",
1307 getMatrixPackingString(layoutQualifier.matrixPacking));
1308 return;
1309 }
1310
1311 if (layoutQualifier.blockStorage != EbsUnspecified)
1312 {
1313 error(location, "layout qualifier only valid for interface blocks",
1314 getBlockStorageString(layoutQualifier.blockStorage));
1315 return;
1316 }
1317
1318 if (qualifier == EvqFragmentOut)
1319 {
1320 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1321 {
1322 error(location, "invalid layout qualifier combination", "yuv");
1323 return;
1324 }
1325 }
1326 else
1327 {
1328 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1329 }
1330
Olli Etuaho95468d12017-05-04 11:14:34 +03001331 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1332 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001333 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1334 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001335 {
1336 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1337 }
1338
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001339 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001340 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001341 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001342 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001343 // We're not checking whether the uniform location is in range here since that depends on
1344 // the type of the variable.
1345 // The type can only be fully determined for non-empty declarations.
1346 }
1347 if (!canHaveLocation)
1348 {
1349 checkLocationIsNotSpecified(location, layoutQualifier);
1350 }
1351}
1352
jchen104cdac9e2017-05-08 11:01:20 +08001353void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1354 const TSourceLoc &location)
1355{
1356 if (publicType.precision != EbpHigh)
1357 {
1358 error(location, "Can only be highp", "atomic counter");
1359 }
1360 // dEQP enforces compile error if location is specified. See uniform_location.test.
1361 if (publicType.layoutQualifier.location != -1)
1362 {
1363 error(location, "location must not be set for atomic_uint", "layout");
1364 }
1365 if (publicType.layoutQualifier.binding == -1)
1366 {
1367 error(location, "no binding specified", "atomic counter");
1368 }
1369}
1370
Olli Etuaho55bde912017-10-25 13:41:13 +03001371void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001372{
Olli Etuaho55bde912017-10-25 13:41:13 +03001373 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001374 {
1375 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1376 // error. It is assumed that this applies to empty declarations as well.
1377 error(location, "empty array declaration needs to specify a size", "");
1378 }
Martin Radevb8b01222016-11-20 23:25:53 +02001379}
1380
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001381// These checks are done for all declarations that are non-empty. They're done for non-empty
1382// declarations starting a declarator list, and declarators that follow an empty declaration.
1383void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1384 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001385{
Olli Etuahofa33d582015-04-09 14:33:12 +03001386 switch (publicType.qualifier)
1387 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001388 case EvqVaryingIn:
1389 case EvqVaryingOut:
1390 case EvqAttribute:
1391 case EvqVertexIn:
1392 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001393 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001394 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001395 {
1396 error(identifierLocation, "cannot be used with a structure",
1397 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001398 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001399 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001400 break;
1401 case EvqBuffer:
1402 if (publicType.getBasicType() != EbtInterfaceBlock)
1403 {
1404 error(identifierLocation,
1405 "cannot declare buffer variables at global scope(outside a block)",
1406 getQualifierString(publicType.qualifier));
1407 return;
1408 }
1409 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001410 default:
1411 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001412 }
jchen10cc2a10e2017-05-03 14:05:12 +08001413 std::string reason(getBasicString(publicType.getBasicType()));
1414 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001415 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001416 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001417 {
1418 return;
1419 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001420
Andrei Volykhina5527072017-03-22 16:46:30 +03001421 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1422 publicType.qualifier != EvqConst) &&
1423 publicType.getBasicType() == EbtYuvCscStandardEXT)
1424 {
1425 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1426 getQualifierString(publicType.qualifier));
1427 return;
1428 }
1429
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001430 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1431 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001432 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1433 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001434 TType type(publicType);
1435 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001436 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001437 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1438 publicType.layoutQualifier);
1439 }
1440 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001441
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001442 // check for layout qualifier issues
1443 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001444
Martin Radev2cc85b32016-08-05 16:22:53 +03001445 if (IsImage(publicType.getBasicType()))
1446 {
1447
1448 switch (layoutQualifier.imageInternalFormat)
1449 {
1450 case EiifRGBA32F:
1451 case EiifRGBA16F:
1452 case EiifR32F:
1453 case EiifRGBA8:
1454 case EiifRGBA8_SNORM:
1455 if (!IsFloatImage(publicType.getBasicType()))
1456 {
1457 error(identifierLocation,
1458 "internal image format requires a floating image type",
1459 getBasicString(publicType.getBasicType()));
1460 return;
1461 }
1462 break;
1463 case EiifRGBA32I:
1464 case EiifRGBA16I:
1465 case EiifRGBA8I:
1466 case EiifR32I:
1467 if (!IsIntegerImage(publicType.getBasicType()))
1468 {
1469 error(identifierLocation,
1470 "internal image format requires an integer image type",
1471 getBasicString(publicType.getBasicType()));
1472 return;
1473 }
1474 break;
1475 case EiifRGBA32UI:
1476 case EiifRGBA16UI:
1477 case EiifRGBA8UI:
1478 case EiifR32UI:
1479 if (!IsUnsignedImage(publicType.getBasicType()))
1480 {
1481 error(identifierLocation,
1482 "internal image format requires an unsigned image type",
1483 getBasicString(publicType.getBasicType()));
1484 return;
1485 }
1486 break;
1487 case EiifUnspecified:
1488 error(identifierLocation, "layout qualifier", "No image internal format specified");
1489 return;
1490 default:
1491 error(identifierLocation, "layout qualifier", "unrecognized token");
1492 return;
1493 }
1494
1495 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1496 switch (layoutQualifier.imageInternalFormat)
1497 {
1498 case EiifR32F:
1499 case EiifR32I:
1500 case EiifR32UI:
1501 break;
1502 default:
1503 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1504 {
1505 error(identifierLocation, "layout qualifier",
1506 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1507 "image variables must be qualified readonly and/or writeonly");
1508 return;
1509 }
1510 break;
1511 }
1512 }
1513 else
1514 {
Olli Etuaho43364892017-02-13 16:00:12 +00001515 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001516 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1517 }
jchen104cdac9e2017-05-08 11:01:20 +08001518
1519 if (IsAtomicCounter(publicType.getBasicType()))
1520 {
1521 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1522 }
1523 else
1524 {
1525 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1526 }
Olli Etuaho43364892017-02-13 16:00:12 +00001527}
Martin Radev2cc85b32016-08-05 16:22:53 +03001528
Olli Etuaho43364892017-02-13 16:00:12 +00001529void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1530{
1531 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1533 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1534 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1535 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1536 // when it comes to which shaders are accepted by the compiler.
1537 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001538 if (IsImage(type.getBasicType()))
1539 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001540 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1541 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001542 }
1543 else if (IsSampler(type.getBasicType()))
1544 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001545 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1546 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001547 }
jchen104cdac9e2017-05-08 11:01:20 +08001548 else if (IsAtomicCounter(type.getBasicType()))
1549 {
1550 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1551 }
Olli Etuaho43364892017-02-13 16:00:12 +00001552 else
1553 {
1554 ASSERT(!IsOpaqueType(type.getBasicType()));
1555 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001556 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001557}
1558
Olli Etuaho856c4972016-08-08 11:38:39 +03001559void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001560 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001561 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001562{
1563
1564 if (mShaderVersion < versionRequired)
1565 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001566 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001567 }
1568}
1569
Olli Etuaho856c4972016-08-08 11:38:39 +03001570bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1571 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001572{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001573 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001574 for (size_t i = 0u; i < localSize.size(); ++i)
1575 {
1576 if (localSize[i] != -1)
1577 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001578 error(location,
1579 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1580 "global layout declaration",
1581 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001582 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001583 }
1584 }
1585
Olli Etuaho8a176262016-08-16 14:23:01 +03001586 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001587}
1588
Olli Etuaho43364892017-02-13 16:00:12 +00001589void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001590 TLayoutImageInternalFormat internalFormat)
1591{
1592 if (internalFormat != EiifUnspecified)
1593 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001594 error(location, "invalid layout qualifier: only valid when used with images",
1595 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001596 }
Olli Etuaho43364892017-02-13 16:00:12 +00001597}
1598
1599void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1600{
1601 if (binding != -1)
1602 {
1603 error(location,
1604 "invalid layout qualifier: only valid when used with opaque types or blocks",
1605 "binding");
1606 }
1607}
1608
jchen104cdac9e2017-05-08 11:01:20 +08001609void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1610{
1611 if (offset != -1)
1612 {
1613 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1614 "offset");
1615 }
1616}
1617
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1619 int binding,
1620 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001621{
1622 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001623 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001624 {
1625 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1626 }
1627}
1628
1629void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1630 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001631 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001632{
1633 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001634 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001635 {
1636 error(location, "sampler binding greater than maximum texture units", "binding");
1637 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001638}
1639
Jiajia Qinbc585152017-06-23 15:42:17 +08001640void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1641 const TQualifier &qualifier,
1642 int binding,
1643 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001644{
1645 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001646 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001647 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001648 if (binding + size > mMaxUniformBufferBindings)
1649 {
1650 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1651 "binding");
1652 }
1653 }
1654 else if (qualifier == EvqBuffer)
1655 {
1656 if (binding + size > mMaxShaderStorageBufferBindings)
1657 {
1658 error(location,
1659 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1660 "binding");
1661 }
jchen10af713a22017-04-19 09:10:56 +08001662 }
1663}
jchen104cdac9e2017-05-08 11:01:20 +08001664void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1665{
1666 if (binding >= mMaxAtomicCounterBindings)
1667 {
1668 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1669 "binding");
1670 }
1671}
jchen10af713a22017-04-19 09:10:56 +08001672
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001673void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1674 int objectLocationCount,
1675 const TLayoutQualifier &layoutQualifier)
1676{
1677 int loc = layoutQualifier.location;
1678 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1679 {
1680 error(location, "Uniform location out of range", "location");
1681 }
1682}
1683
Andrei Volykhina5527072017-03-22 16:46:30 +03001684void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1685{
1686 if (yuv != false)
1687 {
1688 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1689 }
1690}
1691
Jiajia Qinbc585152017-06-23 15:42:17 +08001692void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1693 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001694{
1695 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1696 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001697 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001698 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001699 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1700 qual == EvqConstReadOnly);
1701 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001702 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001703 markStaticReadIfSymbol(argument);
1704 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001705 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001706 if (argument->getMemoryQualifier().writeonly)
1707 {
1708 error(argument->getLine(),
1709 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1710 fnCall->functionName());
1711 return;
1712 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001713 }
1714 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001715 if (qual == EvqOut || qual == EvqInOut)
1716 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001717 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001718 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001719 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001720 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001721 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001722 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001723 }
1724 }
1725 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001726}
1727
Martin Radev70866b82016-07-22 15:27:42 +03001728void TParseContext::checkInvariantVariableQualifier(bool invariant,
1729 const TQualifier qualifier,
1730 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731{
Martin Radev70866b82016-07-22 15:27:42 +03001732 if (!invariant)
1733 return;
1734
1735 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001736 {
Martin Radev70866b82016-07-22 15:27:42 +03001737 // input variables in the fragment shader can be also qualified as invariant
1738 if (!sh::CanBeInvariantESSL1(qualifier))
1739 {
1740 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1741 }
1742 }
1743 else
1744 {
1745 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1746 {
1747 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1748 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001749 }
1750}
1751
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001752bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001753{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001754 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001755}
1756
Jamie Madillb98c3a82015-07-23 14:26:04 -04001757void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1758 const char *extName,
1759 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001760{
1761 pp::SourceLocation srcLoc;
1762 srcLoc.file = loc.first_file;
1763 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001764 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001765}
1766
Jamie Madillb98c3a82015-07-23 14:26:04 -04001767void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1768 const char *name,
1769 const char *value,
1770 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001771{
1772 pp::SourceLocation srcLoc;
1773 srcLoc.file = loc.first_file;
1774 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001775 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001776}
1777
Martin Radev4c4c8e72016-08-04 12:25:34 +03001778sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001779{
Jamie Madill2f294c92017-11-20 14:47:26 -05001780 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001781 for (size_t i = 0u; i < result.size(); ++i)
1782 {
1783 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1784 {
1785 result[i] = 1;
1786 }
1787 else
1788 {
1789 result[i] = mComputeShaderLocalSize[i];
1790 }
1791 }
1792 return result;
1793}
1794
Olli Etuaho56229f12017-07-10 14:16:33 +03001795TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1796 const TSourceLoc &line)
1797{
1798 TIntermConstantUnion *node = new TIntermConstantUnion(
1799 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1800 node->setLine(line);
1801 return node;
1802}
1803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804/////////////////////////////////////////////////////////////////////////////////
1805//
1806// Non-Errors.
1807//
1808/////////////////////////////////////////////////////////////////////////////////
1809
Jamie Madill5c097022014-08-20 16:38:32 -04001810const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001811 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001812 const TSymbol *symbol)
1813{
Jamie Madill5c097022014-08-20 16:38:32 -04001814 if (!symbol)
1815 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001816 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001817 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001818 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001819
1820 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001821 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001822 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001823 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001824 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001825
1826 const TVariable *variable = static_cast<const TVariable *>(symbol);
1827
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001828 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001829 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001830 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001831 }
1832
Olli Etuaho0f684632017-07-13 12:42:15 +03001833 // Reject shaders using both gl_FragData and gl_FragColor
1834 TQualifier qualifier = variable->getType().getQualifier();
1835 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001836 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001837 mUsesFragData = true;
1838 }
1839 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1840 {
1841 mUsesFragColor = true;
1842 }
1843 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1844 {
1845 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001846 }
1847
Olli Etuaho0f684632017-07-13 12:42:15 +03001848 // This validation is not quite correct - it's only an error to write to
1849 // both FragData and FragColor. For simplicity, and because users shouldn't
1850 // be rewarded for reading from undefined varaibles, return an error
1851 // if they are both referenced, rather than assigned.
1852 if (mUsesFragData && mUsesFragColor)
1853 {
1854 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1855 if (mUsesSecondaryOutputs)
1856 {
1857 errorMessage =
1858 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1859 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1860 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02001861 error(location, errorMessage, name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001862 }
1863
1864 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1865 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1866 qualifier == EvqWorkGroupSize)
1867 {
1868 error(location,
1869 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1870 "gl_WorkGroupSize");
1871 }
Jamie Madill5c097022014-08-20 16:38:32 -04001872 return variable;
1873}
1874
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001875TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001876 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877 const TSymbol *symbol)
1878{
1879 const TVariable *variable = getNamedVariable(location, name, symbol);
1880
Olli Etuaho0f684632017-07-13 12:42:15 +03001881 if (!variable)
1882 {
1883 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1884 node->setLine(location);
1885 return node;
1886 }
1887
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001889 TIntermTyped *node = nullptr;
1890
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001891 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001892 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001893 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001894 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001895 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001897 {
1898 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1899 // needs to be added to the AST as a constant and not as a symbol.
1900 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1901 TConstantUnion *constArray = new TConstantUnion[3];
1902 for (size_t i = 0; i < 3; ++i)
1903 {
1904 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1905 }
1906
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001907 ASSERT(variableType.getBasicType() == EbtUInt);
1908 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001909
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001910 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001911 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001912 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001913 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001914 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1915 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001916 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001917 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1918 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001919 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001920 else
1921 {
Olli Etuaho195be942017-12-04 23:40:14 +02001922 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001923 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001924 ASSERT(node != nullptr);
1925 node->setLine(location);
1926 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001927}
1928
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929// Initializers show up in several places in the grammar. Have one set of
1930// code to handle them here.
1931//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001932// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001933bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001934 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001935 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001936 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001937 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938{
Olli Etuaho13389b62016-10-16 11:48:18 +01001939 ASSERT(initNode != nullptr);
1940 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941
Olli Etuahob60d30f2018-01-16 12:31:06 +02001942 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001943 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001944 // In case initializer is not an array or type has more dimensions than initializer, this
1945 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1946 // actually is an array or not. Having a non-array initializer for an unsized array will
1947 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001948 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001949 type->sizeUnsizedArrays(arraySizes);
1950 }
1951
1952 const TQualifier qualifier = type->getQualifier();
1953
1954 bool constError = false;
1955 if (qualifier == EvqConst)
1956 {
1957 if (EvqConst != initializer->getType().getQualifier())
1958 {
1959 std::stringstream reasonStream;
1960 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1961 std::string reason = reasonStream.str();
1962 error(line, reason.c_str(), "=");
1963
1964 // We're still going to declare the variable to avoid extra error messages.
1965 type->setQualifier(EvqTemporary);
1966 constError = true;
1967 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001968 }
Olli Etuaho195be942017-12-04 23:40:14 +02001969
1970 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001971 if (!declareVariable(line, identifier, type, &variable))
1972 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001973 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975
Olli Etuahob60d30f2018-01-16 12:31:06 +02001976 if (constError)
1977 {
1978 return false;
1979 }
1980
Olli Etuahob0c645e2015-05-12 14:25:36 +03001981 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001982 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001983 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001984 {
1985 // Error message does not completely match behavior with ESSL 1.00, but
1986 // we want to steer developers towards only using constant expressions.
1987 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001988 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001989 }
1990 if (globalInitWarning)
1991 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001992 warning(
1993 line,
1994 "global variable initializers should be constant expressions "
1995 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1996 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001997 }
1998
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001999 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05302000 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
2001 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002002 error(line, " cannot initialize this type of qualifier ",
2003 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002004 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002005 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02002006
2007 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
2008 intermSymbol->setLine(line);
2009
2010 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
2011 {
2012 assignError(line, "=", variable->getType().getCompleteString(),
2013 initializer->getCompleteString());
2014 return false;
2015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016
Arun Patole7e7e68d2015-05-22 12:02:25 +05302017 if (qualifier == EvqConst)
2018 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002019 // Save the constant folded value to the variable if possible.
2020 const TConstantUnion *constArray = initializer->getConstantValue();
2021 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302022 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002023 variable->shareConstPointer(constArray);
2024 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002025 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002026 ASSERT(*initNode == nullptr);
2027 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002028 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002029 }
2030 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002031
Olli Etuahob60d30f2018-01-16 12:31:06 +02002032 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002033 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002034 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002035 return true;
2036}
2037
2038TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002039 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002040 TIntermTyped *initializer,
2041 const TSourceLoc &loc)
2042{
2043 checkIsScalarBool(loc, pType);
2044 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002045 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002046 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002047 {
2048 // The initializer is valid. The init condition needs to have a node - either the
2049 // initializer node, or a constant node in case the initialized variable is const and won't
2050 // be recorded in the AST.
2051 if (initNode == nullptr)
2052 {
2053 return initializer;
2054 }
2055 else
2056 {
2057 TIntermDeclaration *declaration = new TIntermDeclaration();
2058 declaration->appendDeclarator(initNode);
2059 return declaration;
2060 }
2061 }
2062 return nullptr;
2063}
2064
2065TIntermNode *TParseContext::addLoop(TLoopType type,
2066 TIntermNode *init,
2067 TIntermNode *cond,
2068 TIntermTyped *expr,
2069 TIntermNode *body,
2070 const TSourceLoc &line)
2071{
2072 TIntermNode *node = nullptr;
2073 TIntermTyped *typedCond = nullptr;
2074 if (cond)
2075 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002076 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 typedCond = cond->getAsTyped();
2078 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002079 if (expr)
2080 {
2081 markStaticReadIfSymbol(expr);
2082 }
2083 // In case the loop body was not parsed as a block and contains a statement that simply refers
2084 // to a variable, we need to mark it as statically used.
2085 if (body)
2086 {
2087 markStaticReadIfSymbol(body);
2088 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002089 if (cond == nullptr || typedCond)
2090 {
Olli Etuahocce89652017-06-19 16:04:09 +03002091 if (type == ELoopDoWhile)
2092 {
2093 checkIsScalarBool(line, typedCond);
2094 }
2095 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2096 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2097 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2098 !typedCond->isVector()));
2099
Olli Etuaho3ec75682017-07-05 17:02:55 +03002100 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002101 node->setLine(line);
2102 return node;
2103 }
2104
Olli Etuahocce89652017-06-19 16:04:09 +03002105 ASSERT(type != ELoopDoWhile);
2106
Olli Etuaho914b79a2017-06-19 16:03:19 +03002107 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2108 ASSERT(declaration);
2109 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2110 ASSERT(declarator->getLeft()->getAsSymbolNode());
2111
2112 // The condition is a declaration. In the AST representation we don't support declarations as
2113 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2114 // the loop.
2115 TIntermBlock *block = new TIntermBlock();
2116
2117 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2118 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2119 block->appendStatement(declareCondition);
2120
2121 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2122 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002123 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002124 block->appendStatement(loop);
2125 loop->setLine(line);
2126 block->setLine(line);
2127 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128}
2129
Olli Etuahocce89652017-06-19 16:04:09 +03002130TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2131 TIntermNodePair code,
2132 const TSourceLoc &loc)
2133{
Olli Etuaho56229f12017-07-10 14:16:33 +03002134 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002135 // In case the conditional statements were not parsed as blocks and contain a statement that
2136 // simply refers to a variable, we need to mark them as statically used.
2137 if (code.node1)
2138 {
2139 markStaticReadIfSymbol(code.node1);
2140 }
2141 if (code.node2)
2142 {
2143 markStaticReadIfSymbol(code.node2);
2144 }
Olli Etuahocce89652017-06-19 16:04:09 +03002145
2146 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002147 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002148 {
2149 if (cond->getAsConstantUnion()->getBConst(0) == true)
2150 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002151 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002152 }
2153 else
2154 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002155 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002156 }
2157 }
2158
Olli Etuaho3ec75682017-07-05 17:02:55 +03002159 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002160 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002161 node->setLine(loc);
2162
2163 return node;
2164}
2165
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002166void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2167{
2168 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2169 typeSpecifier->getBasicType());
2170
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002171 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002172 {
2173 error(typeSpecifier->getLine(), "not supported", "first-class array");
2174 typeSpecifier->clearArrayness();
2175 }
2176}
2177
Martin Radev70866b82016-07-22 15:27:42 +03002178TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302179 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002180{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002181 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182
Martin Radev70866b82016-07-22 15:27:42 +03002183 TPublicType returnType = typeSpecifier;
2184 returnType.qualifier = typeQualifier.qualifier;
2185 returnType.invariant = typeQualifier.invariant;
2186 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002187 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002188 returnType.precision = typeSpecifier.precision;
2189
2190 if (typeQualifier.precision != EbpUndefined)
2191 {
2192 returnType.precision = typeQualifier.precision;
2193 }
2194
Martin Radev4a9cd802016-09-01 16:51:51 +03002195 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2196 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002197
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2199 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002200
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002202
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002203 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002204 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002205 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002206 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002207 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002208 returnType.clearArrayness();
2209 }
2210
Martin Radev70866b82016-07-22 15:27:42 +03002211 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002212 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002213 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002215 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002216 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002217
Martin Radev70866b82016-07-22 15:27:42 +03002218 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002219 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002220 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002221 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002222 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002223 }
2224 }
2225 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002226 {
Martin Radev70866b82016-07-22 15:27:42 +03002227 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002228 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002229 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002230 }
Martin Radev70866b82016-07-22 15:27:42 +03002231 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2232 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002233 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002234 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2235 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002236 }
Martin Radev70866b82016-07-22 15:27:42 +03002237 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002238 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002239 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002240 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002241 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002242 }
2243
2244 return returnType;
2245}
2246
Olli Etuaho856c4972016-08-08 11:38:39 +03002247void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2248 const TPublicType &type,
2249 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002250{
2251 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002252 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 {
2254 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 }
2256
2257 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2258 switch (qualifier)
2259 {
2260 case EvqVertexIn:
2261 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002262 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 {
2264 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002266 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 return;
2268 case EvqFragmentOut:
2269 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002270 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002271 {
2272 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002273 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002274 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 return;
2276 default:
2277 break;
2278 }
2279
2280 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2281 // restrictions.
2282 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002283 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2284 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002285 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2286 {
2287 error(qualifierLocation, "must use 'flat' interpolation here",
2288 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002289 }
2290
Martin Radev4a9cd802016-09-01 16:51:51 +03002291 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002292 {
2293 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2294 // These restrictions are only implied by the ESSL 3.00 spec, but
2295 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002296 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002297 {
2298 error(qualifierLocation, "cannot be an array of structures",
2299 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002300 }
2301 if (type.isStructureContainingArrays())
2302 {
2303 error(qualifierLocation, "cannot be a structure containing an array",
2304 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002305 }
2306 if (type.isStructureContainingType(EbtStruct))
2307 {
2308 error(qualifierLocation, "cannot be a structure containing a structure",
2309 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002310 }
2311 if (type.isStructureContainingType(EbtBool))
2312 {
2313 error(qualifierLocation, "cannot be a structure containing a bool",
2314 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002315 }
2316 }
2317}
2318
Martin Radev2cc85b32016-08-05 16:22:53 +03002319void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2320{
2321 if (qualifier.getType() == QtStorage)
2322 {
2323 const TStorageQualifierWrapper &storageQualifier =
2324 static_cast<const TStorageQualifierWrapper &>(qualifier);
2325 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2326 !symbolTable.atGlobalLevel())
2327 {
2328 error(storageQualifier.getLine(),
2329 "Local variables can only use the const storage qualifier.",
2330 storageQualifier.getQualifierString().c_str());
2331 }
2332 }
2333}
2334
Olli Etuaho43364892017-02-13 16:00:12 +00002335void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002336 const TSourceLoc &location)
2337{
Jiajia Qinbc585152017-06-23 15:42:17 +08002338 const std::string reason(
2339 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2340 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002341 if (memoryQualifier.readonly)
2342 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002343 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002344 }
2345 if (memoryQualifier.writeonly)
2346 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002347 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002348 }
Martin Radev049edfa2016-11-11 14:35:37 +02002349 if (memoryQualifier.coherent)
2350 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002351 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002352 }
2353 if (memoryQualifier.restrictQualifier)
2354 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002355 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002356 }
2357 if (memoryQualifier.volatileQualifier)
2358 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002359 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002360 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002361}
2362
jchen104cdac9e2017-05-08 11:01:20 +08002363// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2364// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002365void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2366 const TSourceLoc &loc,
2367 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002368{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002369 if (!IsAtomicCounter(type->getBasicType()))
2370 {
2371 return;
2372 }
2373
2374 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2375 : kAtomicCounterSize;
2376 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2377 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002378 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002379 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002380 {
2381 offset = bindingState.appendSpan(size);
2382 }
2383 else
2384 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002385 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002386 }
2387 if (offset == -1)
2388 {
2389 error(loc, "Offset overlapping", "atomic counter");
2390 return;
2391 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002392 layoutQualifier.offset = offset;
2393 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002394}
2395
Olli Etuaho454c34c2017-10-25 16:35:56 +03002396void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002397 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002398 TType *type)
2399{
2400 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2401 {
2402 if (type->isArray() && type->getOutermostArraySize() == 0u)
2403 {
2404 // Set size for the unsized geometry shader inputs if they are declared after a valid
2405 // input primitive declaration.
2406 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2407 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002408 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002409 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002410 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002411 }
2412 else
2413 {
2414 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2415 // An input can be declared without an array size if there is a previous layout
2416 // which specifies the size.
2417 error(location,
2418 "Missing a valid input primitive declaration before declaring an unsized "
2419 "array input",
2420 token);
2421 }
2422 }
2423 else if (type->isArray())
2424 {
2425 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2426 }
2427 else
2428 {
2429 error(location, "Geometry shader input variable must be declared as an array", token);
2430 }
2431 }
2432}
2433
Olli Etuaho13389b62016-10-16 11:48:18 +01002434TIntermDeclaration *TParseContext::parseSingleDeclaration(
2435 TPublicType &publicType,
2436 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002437 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002438{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002439 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002440 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2441 mDirectiveHandler.pragma().stdgl.invariantAll)
2442 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002443 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002444
2445 // The directive handler has already taken care of rejecting invalid uses of this pragma
2446 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2447 // affected variable declarations:
2448 //
2449 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2450 // elsewhere, in TranslatorGLSL.)
2451 //
2452 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2453 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2454 // the way this is currently implemented we have to enable this compiler option before
2455 // parsing the shader and determining the shading language version it uses. If this were
2456 // implemented as a post-pass, the workaround could be more targeted.
2457 //
2458 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2459 // the specification, but there are desktop OpenGL drivers that expect that this is the
2460 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2461 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2462 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002463 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002464 }
2465 }
2466
Olli Etuahofbb1c792018-01-19 16:26:59 +02002467 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002468
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002469 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2470 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002471
Olli Etuahobab4c082015-04-24 16:38:49 +03002472 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002474
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002476 if (emptyDeclaration)
2477 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002478 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 // In most cases we don't need to create a symbol node for an empty declaration.
2480 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002481 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 {
Olli Etuaho195be942017-12-04 23:40:14 +02002483 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002484 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002485 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002486 }
jchen104cdac9e2017-05-08 11:01:20 +08002487 else if (IsAtomicCounter(publicType.getBasicType()))
2488 {
2489 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2490 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002491 }
2492 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002493 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002494 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002495
Olli Etuahob60d30f2018-01-16 12:31:06 +02002496 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002497
Olli Etuahob60d30f2018-01-16 12:31:06 +02002498 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002499
Olli Etuaho2935c582015-04-08 14:32:06 +03002500 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002501 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002502 {
Olli Etuaho195be942017-12-04 23:40:14 +02002503 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002505 }
2506
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002507 TIntermDeclaration *declaration = new TIntermDeclaration();
2508 declaration->setLine(identifierOrTypeLocation);
2509 if (symbol)
2510 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002511 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002512 declaration->appendDeclarator(symbol);
2513 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002515}
2516
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002517TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2518 TPublicType &elementType,
2519 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002520 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002521 const TSourceLoc &indexLocation,
2522 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002523{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002524 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002525
Olli Etuaho55bde912017-10-25 13:41:13 +03002526 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002527 identifierLocation);
2528
Olli Etuaho55bde912017-10-25 13:41:13 +03002529 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002530
Olli Etuaho55bde912017-10-25 13:41:13 +03002531 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002532
Olli Etuahob60d30f2018-01-16 12:31:06 +02002533 TType *arrayType = new TType(elementType);
2534 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002535
Olli Etuahofbb1c792018-01-19 16:26:59 +02002536 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002537
Olli Etuahob60d30f2018-01-16 12:31:06 +02002538 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002539
Olli Etuahob60d30f2018-01-16 12:31:06 +02002540 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002541
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 TIntermDeclaration *declaration = new TIntermDeclaration();
2543 declaration->setLine(identifierLocation);
2544
Olli Etuaho195be942017-12-04 23:40:14 +02002545 TVariable *variable = nullptr;
2546 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 {
Olli Etuaho195be942017-12-04 23:40:14 +02002548 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002549 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 declaration->appendDeclarator(symbol);
2551 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002552
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2557 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002558 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002559 const TSourceLoc &initLocation,
2560 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002561{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002562 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002563
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2565 identifierLocation);
2566
2567 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002568
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 TIntermDeclaration *declaration = new TIntermDeclaration();
2570 declaration->setLine(identifierLocation);
2571
2572 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002573 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002574 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002575 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 if (initNode)
2577 {
2578 declaration->appendDeclarator(initNode);
2579 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002580 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002581 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002582}
2583
Olli Etuaho13389b62016-10-16 11:48:18 +01002584TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002585 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002586 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002587 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002588 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002589 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590 const TSourceLoc &initLocation,
2591 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002593 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002594
Olli Etuaho55bde912017-10-25 13:41:13 +03002595 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002596 identifierLocation);
2597
Olli Etuaho55bde912017-10-25 13:41:13 +03002598 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002599
Olli Etuaho55bde912017-10-25 13:41:13 +03002600 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002601
Olli Etuahob60d30f2018-01-16 12:31:06 +02002602 TType *arrayType = new TType(elementType);
2603 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605 TIntermDeclaration *declaration = new TIntermDeclaration();
2606 declaration->setLine(identifierLocation);
2607
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002608 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002609 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002610 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002611 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002612 if (initNode)
2613 {
2614 declaration->appendDeclarator(initNode);
2615 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002616 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002617
2618 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002619}
2620
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002621TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002622 const TTypeQualifierBuilder &typeQualifierBuilder,
2623 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002624 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002625 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002626{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002627 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002628
Martin Radev70866b82016-07-22 15:27:42 +03002629 if (!typeQualifier.invariant)
2630 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002631 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002632 return nullptr;
2633 }
2634 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2635 {
2636 return nullptr;
2637 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002638 if (!symbol)
2639 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002640 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002641 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002642 }
Martin Radev70866b82016-07-22 15:27:42 +03002643 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002644 {
Martin Radev70866b82016-07-22 15:27:42 +03002645 error(identifierLoc, "invariant declaration specifies qualifier",
2646 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002647 }
Martin Radev70866b82016-07-22 15:27:42 +03002648 if (typeQualifier.precision != EbpUndefined)
2649 {
2650 error(identifierLoc, "invariant declaration specifies precision",
2651 getPrecisionString(typeQualifier.precision));
2652 }
2653 if (!typeQualifier.layoutQualifier.isEmpty())
2654 {
2655 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2656 }
2657
2658 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002659 if (!variable)
2660 {
2661 return nullptr;
2662 }
Martin Radev70866b82016-07-22 15:27:42 +03002663 const TType &type = variable->getType();
2664
2665 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2666 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002667 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002668
Olli Etuahodefe3932018-02-13 11:56:09 +02002669 symbolTable.addInvariantVarying(identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002670
Olli Etuaho195be942017-12-04 23:40:14 +02002671 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002672 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002673
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002674 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002675}
2676
Olli Etuaho13389b62016-10-16 11:48:18 +01002677void TParseContext::parseDeclarator(TPublicType &publicType,
2678 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002679 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002680 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002681{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002682 // If the declaration starting this declarator list was empty (example: int,), some checks were
2683 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002684 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002685 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002686 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2687 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 }
2689
Olli Etuaho856c4972016-08-08 11:38:39 +03002690 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002691
Olli Etuahob60d30f2018-01-16 12:31:06 +02002692 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002693
Olli Etuahofbb1c792018-01-19 16:26:59 +02002694 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002695
Olli Etuahob60d30f2018-01-16 12:31:06 +02002696 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002697
Olli Etuahob60d30f2018-01-16 12:31:06 +02002698 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002699
Olli Etuaho195be942017-12-04 23:40:14 +02002700 TVariable *variable = nullptr;
2701 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002702 {
Olli Etuaho195be942017-12-04 23:40:14 +02002703 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002704 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002705 declarationOut->appendDeclarator(symbol);
2706 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002707}
2708
Olli Etuaho55bde912017-10-25 13:41:13 +03002709void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002710 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002711 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002712 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002713 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002714 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002715{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002716 // If the declaration starting this declarator list was empty (example: int,), some checks were
2717 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002718 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002719 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002720 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002721 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002722 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002723
Olli Etuaho55bde912017-10-25 13:41:13 +03002724 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002725
Olli Etuaho55bde912017-10-25 13:41:13 +03002726 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002727 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002728 TType *arrayType = new TType(elementType);
2729 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002730
Olli Etuahofbb1c792018-01-19 16:26:59 +02002731 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002732
Olli Etuahob60d30f2018-01-16 12:31:06 +02002733 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002734
Olli Etuahob60d30f2018-01-16 12:31:06 +02002735 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002736
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002737 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002738 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002739 {
Olli Etuaho195be942017-12-04 23:40:14 +02002740 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002741 symbol->setLine(identifierLocation);
2742 declarationOut->appendDeclarator(symbol);
2743 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002745}
2746
Olli Etuaho13389b62016-10-16 11:48:18 +01002747void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2748 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002749 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 const TSourceLoc &initLocation,
2751 TIntermTyped *initializer,
2752 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002753{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754 // If the declaration starting this declarator list was empty (example: int,), some checks were
2755 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002756 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002757 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2759 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002760 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002761
Olli Etuaho856c4972016-08-08 11:38:39 +03002762 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002763
Olli Etuaho13389b62016-10-16 11:48:18 +01002764 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002765 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002766 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002767 {
2768 //
2769 // build the intermediate representation
2770 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002771 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002772 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002773 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002774 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002775 }
2776}
2777
Olli Etuaho55bde912017-10-25 13:41:13 +03002778void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002779 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002780 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002781 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002782 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002783 const TSourceLoc &initLocation,
2784 TIntermTyped *initializer,
2785 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002786{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 // If the declaration starting this declarator list was empty (example: int,), some checks were
2788 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002789 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002790 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002791 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002792 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002793 }
2794
Olli Etuaho55bde912017-10-25 13:41:13 +03002795 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002796
Olli Etuaho55bde912017-10-25 13:41:13 +03002797 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002798
Olli Etuahob60d30f2018-01-16 12:31:06 +02002799 TType *arrayType = new TType(elementType);
2800 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002801
2802 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002803 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002804 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002805 {
2806 if (initNode)
2807 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002808 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002809 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002810 }
2811}
2812
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002813TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2814{
2815 // It's simpler to parse an empty statement as a constant expression rather than having a
2816 // different type of node just for empty statements, that will be pruned from the AST anyway.
2817 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2818 node->setLine(location);
2819 return node;
2820}
2821
jchen104cdac9e2017-05-08 11:01:20 +08002822void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2823 const TSourceLoc &location)
2824{
2825 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2826 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2827 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2828 {
2829 error(location, "Requires both binding and offset", "layout");
2830 return;
2831 }
2832 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2833}
2834
Olli Etuahocce89652017-06-19 16:04:09 +03002835void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2836 const TPublicType &type,
2837 const TSourceLoc &loc)
2838{
2839 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2840 !getFragmentPrecisionHigh())
2841 {
2842 error(loc, "precision is not supported in fragment shader", "highp");
2843 }
2844
2845 if (!CanSetDefaultPrecisionOnType(type))
2846 {
2847 error(loc, "illegal type argument for default precision qualifier",
2848 getBasicString(type.getBasicType()));
2849 return;
2850 }
2851 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2852}
2853
Shaob5cc1192017-07-06 10:47:20 +08002854bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2855{
2856 switch (typeQualifier.layoutQualifier.primitiveType)
2857 {
2858 case EptLines:
2859 case EptLinesAdjacency:
2860 case EptTriangles:
2861 case EptTrianglesAdjacency:
2862 return typeQualifier.qualifier == EvqGeometryIn;
2863
2864 case EptLineStrip:
2865 case EptTriangleStrip:
2866 return typeQualifier.qualifier == EvqGeometryOut;
2867
2868 case EptPoints:
2869 return true;
2870
2871 default:
2872 UNREACHABLE();
2873 return false;
2874 }
2875}
2876
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002877void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2878 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002879{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002880 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002881 {
2882 error(line,
2883 "Array size or input primitive declaration doesn't match the size of earlier sized "
2884 "array inputs.",
2885 "layout");
2886 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002887}
2888
Shaob5cc1192017-07-06 10:47:20 +08002889bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2890{
2891 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2892
2893 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2894
2895 if (layoutQualifier.maxVertices != -1)
2896 {
2897 error(typeQualifier.line,
2898 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2899 return false;
2900 }
2901
2902 // Set mGeometryInputPrimitiveType if exists
2903 if (layoutQualifier.primitiveType != EptUndefined)
2904 {
2905 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2906 {
2907 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2908 return false;
2909 }
2910
2911 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2912 {
2913 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002914 setGeometryShaderInputArraySize(
2915 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2916 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002917 }
2918 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2919 {
2920 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2921 "layout");
2922 return false;
2923 }
2924 }
2925
2926 // Set mGeometryInvocations if exists
2927 if (layoutQualifier.invocations > 0)
2928 {
2929 if (mGeometryShaderInvocations == 0)
2930 {
2931 mGeometryShaderInvocations = layoutQualifier.invocations;
2932 }
2933 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2934 {
2935 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2936 "layout");
2937 return false;
2938 }
2939 }
2940
2941 return true;
2942}
2943
2944bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2945{
2946 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2947
2948 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2949
2950 if (layoutQualifier.invocations > 0)
2951 {
2952 error(typeQualifier.line,
2953 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2954 return false;
2955 }
2956
2957 // Set mGeometryOutputPrimitiveType if exists
2958 if (layoutQualifier.primitiveType != EptUndefined)
2959 {
2960 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2961 {
2962 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2963 return false;
2964 }
2965
2966 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2967 {
2968 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2969 }
2970 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2971 {
2972 error(typeQualifier.line,
2973 "primitive doesn't match earlier output primitive declaration", "layout");
2974 return false;
2975 }
2976 }
2977
2978 // Set mGeometryMaxVertices if exists
2979 if (layoutQualifier.maxVertices > -1)
2980 {
2981 if (mGeometryShaderMaxVertices == -1)
2982 {
2983 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2984 }
2985 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2986 {
2987 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2988 "layout");
2989 return false;
2990 }
2991 }
2992
2993 return true;
2994}
2995
Martin Radev70866b82016-07-22 15:27:42 +03002996void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002997{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002998 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002999 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04003000
Martin Radev70866b82016-07-22 15:27:42 +03003001 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
3002 typeQualifier.line);
3003
Jamie Madillc2128ff2016-07-04 10:26:17 -04003004 // It should never be the case, but some strange parser errors can send us here.
3005 if (layoutQualifier.isEmpty())
3006 {
3007 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04003008 return;
3009 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003010
Martin Radev802abe02016-08-04 17:48:32 +03003011 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003012 {
Olli Etuaho43364892017-02-13 16:00:12 +00003013 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003014 return;
3015 }
3016
Olli Etuaho43364892017-02-13 16:00:12 +00003017 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3018
3019 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003020
3021 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3022
Andrei Volykhina5527072017-03-22 16:46:30 +03003023 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3024
jchen104cdac9e2017-05-08 11:01:20 +08003025 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3026
Qin Jiajiaca68d982017-09-18 16:41:56 +08003027 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3028 typeQualifier.qualifier);
3029
Martin Radev802abe02016-08-04 17:48:32 +03003030 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003031 {
Martin Radev802abe02016-08-04 17:48:32 +03003032 if (mComputeShaderLocalSizeDeclared &&
3033 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3034 {
3035 error(typeQualifier.line, "Work group size does not match the previous declaration",
3036 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003037 return;
3038 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003039
Martin Radev802abe02016-08-04 17:48:32 +03003040 if (mShaderVersion < 310)
3041 {
3042 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003043 return;
3044 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003045
Martin Radev4c4c8e72016-08-04 12:25:34 +03003046 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003047 {
3048 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003049 return;
3050 }
3051
3052 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003053 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003054
3055 const TConstantUnion *maxComputeWorkGroupSizeData =
3056 maxComputeWorkGroupSize->getConstPointer();
3057
3058 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3059 {
3060 if (layoutQualifier.localSize[i] != -1)
3061 {
3062 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3063 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3064 if (mComputeShaderLocalSize[i] < 1 ||
3065 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3066 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003067 std::stringstream reasonStream;
3068 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3069 << maxComputeWorkGroupSizeValue;
3070 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003071
Olli Etuaho4de340a2016-12-16 09:32:03 +00003072 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003073 return;
3074 }
3075 }
3076 }
3077
3078 mComputeShaderLocalSizeDeclared = true;
3079 }
Shaob5cc1192017-07-06 10:47:20 +08003080 else if (typeQualifier.qualifier == EvqGeometryIn)
3081 {
3082 if (mShaderVersion < 310)
3083 {
3084 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3085 return;
3086 }
3087
3088 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3089 {
3090 return;
3091 }
3092 }
3093 else if (typeQualifier.qualifier == EvqGeometryOut)
3094 {
3095 if (mShaderVersion < 310)
3096 {
3097 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3098 "layout");
3099 return;
3100 }
3101
3102 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3103 {
3104 return;
3105 }
3106 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003107 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3108 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003109 {
3110 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3111 // specification.
3112 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3113 {
3114 error(typeQualifier.line, "Number of views does not match the previous declaration",
3115 "layout");
3116 return;
3117 }
3118
3119 if (layoutQualifier.numViews == -1)
3120 {
3121 error(typeQualifier.line, "No num_views specified", "layout");
3122 return;
3123 }
3124
3125 if (layoutQualifier.numViews > mMaxNumViews)
3126 {
3127 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3128 "layout");
3129 return;
3130 }
3131
3132 mNumViews = layoutQualifier.numViews;
3133 }
Martin Radev802abe02016-08-04 17:48:32 +03003134 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003135 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003136 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003137 {
Martin Radev802abe02016-08-04 17:48:32 +03003138 return;
3139 }
3140
Jiajia Qinbc585152017-06-23 15:42:17 +08003141 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003142 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003143 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003144 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003145 return;
3146 }
3147
3148 if (mShaderVersion < 300)
3149 {
3150 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3151 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003152 return;
3153 }
3154
Olli Etuaho09b04a22016-12-15 13:30:26 +00003155 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003156
3157 if (layoutQualifier.matrixPacking != EmpUnspecified)
3158 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003159 if (typeQualifier.qualifier == EvqUniform)
3160 {
3161 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3162 }
3163 else if (typeQualifier.qualifier == EvqBuffer)
3164 {
3165 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3166 }
Martin Radev802abe02016-08-04 17:48:32 +03003167 }
3168
3169 if (layoutQualifier.blockStorage != EbsUnspecified)
3170 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003171 if (typeQualifier.qualifier == EvqUniform)
3172 {
3173 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3174 }
3175 else if (typeQualifier.qualifier == EvqBuffer)
3176 {
3177 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3178 }
Martin Radev802abe02016-08-04 17:48:32 +03003179 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003180 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003181}
3182
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3184 const TFunction &function,
3185 const TSourceLoc &location,
3186 bool insertParametersToSymbolTable)
3187{
Olli Etuahobed35d72017-12-20 16:36:26 +02003188 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003189
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003190 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 prototype->setLine(location);
3192
3193 for (size_t i = 0; i < function.getParamCount(); i++)
3194 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003195 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003196
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003197 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3198 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003199 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003201 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003203 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003204 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003205 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003207 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003208 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003209 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003210 }
3211 else
3212 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003213 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003214 {
3215 error(location, "function parameter array must be sized at compile time", "[]");
3216 // We don't need to size the arrays since the parameter is unnamed and hence
3217 // inaccessible.
3218 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003219 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003220 }
3221 return prototype;
3222}
3223
Olli Etuaho16c745a2017-01-16 17:02:27 +00003224TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3225 const TFunction &parsedFunction,
3226 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003227{
Olli Etuaho476197f2016-10-11 13:59:08 +01003228 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3229 // first declaration. Either way the instance in the symbol table is used to track whether the
3230 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003231 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003232 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003233 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3234
3235 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003236 {
3237 // ESSL 1.00.17 section 4.2.7.
3238 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3239 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003241
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *prototype =
3243 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003246
3247 if (!symbolTable.atGlobalLevel())
3248 {
3249 // ESSL 3.00.4 section 4.2.4.
3250 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003251 }
3252
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253 return prototype;
3254}
3255
Olli Etuaho336b1472016-10-05 16:37:55 +01003256TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermBlock *functionBody,
3259 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003260{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3263 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003264 error(location,
3265 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266 }
3267
Olli Etuahof51fdd22016-10-03 10:03:40 +01003268 if (functionBody == nullptr)
3269 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003270 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003271 functionBody->setLine(location);
3272 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003273 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003274 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003275 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003276
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003277 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003278 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003279}
3280
Olli Etuaho476197f2016-10-11 13:59:08 +01003281void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003282 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003283 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003284{
Olli Etuaho476197f2016-10-11 13:59:08 +01003285 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003286
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003287 bool wasDefined = false;
3288 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3289 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003290 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003291 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003292 }
Jamie Madill185fb402015-06-12 15:48:48 -04003293
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003294 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003295 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003296 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003297
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003298 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003299 setLoopNestingLevel(0);
3300}
3301
Jamie Madillb98c3a82015-07-23 14:26:04 -04003302TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003303{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003304 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003305 // We don't know at this point whether this is a function definition or a prototype.
3306 // The definition production code will check for redefinitions.
3307 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003308 //
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 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003312 const TVariable *param = function->getParam(i);
3313 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003314 {
3315 // ESSL 3.00.6 section 12.10.
3316 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003317 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003318 }
3319 }
3320
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003321 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303322 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003323 const UnmangledBuiltIn *builtIn =
3324 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3325 if (builtIn &&
3326 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3327 {
3328 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3329 // functions. Therefore overloading or redefining builtin functions is an error.
3330 error(location, "Name of a built-in function cannot be redeclared as function",
3331 function->name());
3332 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303333 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003334 else
3335 {
3336 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3337 // this applies to redeclarations as well.
3338 const TSymbol *builtIn =
3339 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3340 if (builtIn)
3341 {
3342 error(location, "built-in functions cannot be redefined", function->name());
3343 }
3344 }
3345
3346 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3347 // here.
3348 const TFunction *prevDec =
3349 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3350 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003351 {
3352 if (prevDec->getReturnType() != function->getReturnType())
3353 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003354 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003355 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003356 }
3357 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3358 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003359 if (prevDec->getParam(i)->getType().getQualifier() !=
3360 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003361 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003362 error(location,
3363 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003364 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003365 }
3366 }
3367 }
3368
Jamie Madill185fb402015-06-12 15:48:48 -04003369 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003370 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3371 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003372 if (prevSym)
3373 {
3374 if (!prevSym->isFunction())
3375 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003376 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003377 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003378 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003379 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003380 // Parsing is at the inner scope level of the function's arguments and body statement at this
3381 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3382 // scope.
3383 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003384
Olli Etuaho78d13742017-01-18 13:06:10 +00003385 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003386 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003387 {
3388 if (function->getParamCount() > 0)
3389 {
3390 error(location, "function cannot take any parameter(s)", "main");
3391 }
3392 if (function->getReturnType().getBasicType() != EbtVoid)
3393 {
3394 error(location, "main function cannot return a value",
3395 function->getReturnType().getBasicString());
3396 }
3397 }
3398
Jamie Madill185fb402015-06-12 15:48:48 -04003399 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3401 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003402 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3403 //
3404 return function;
3405}
3406
Olli Etuaho9de84a52016-06-14 17:36:01 +03003407TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003408 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003409 const TSourceLoc &location)
3410{
3411 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3412 {
3413 error(location, "no qualifiers allowed for function return",
3414 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003415 }
3416 if (!type.layoutQualifier.isEmpty())
3417 {
3418 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419 }
jchen10cc2a10e2017-05-03 14:05:12 +08003420 // make sure an opaque type is not involved as well...
3421 std::string reason(getBasicString(type.getBasicType()));
3422 reason += "s can't be function return values";
3423 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003424 if (mShaderVersion < 300)
3425 {
3426 // Array return values are forbidden, but there's also no valid syntax for declaring array
3427 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003428 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003429
3430 if (type.isStructureContainingArrays())
3431 {
3432 // ESSL 1.00.17 section 6.1 Function Definitions
3433 error(location, "structures containing arrays can't be function return values",
3434 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003435 }
3436 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003437
3438 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003439 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003440}
3441
Olli Etuaho697bf652018-02-16 11:50:54 +02003442TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3443 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003444{
Olli Etuaho697bf652018-02-16 11:50:54 +02003445 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003446}
3447
Olli Etuaho95ed1942018-02-01 14:01:19 +02003448TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003449{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003450 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003451 {
3452 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3453 "[]");
3454 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003455 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003456 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003457 error(publicType.getLine(), "constructor can't be a structure definition",
3458 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003459 }
3460
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003461 TType *type = new TType(publicType);
3462 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003463 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003464 error(publicType.getLine(), "cannot construct this type",
3465 getBasicString(publicType.getBasicType()));
3466 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003467 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003468 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003469}
3470
Olli Etuaho55bde912017-10-25 13:41:13 +03003471void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3472 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003473 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003474 TType *arrayType)
3475{
3476 if (arrayType->isUnsizedArray())
3477 {
3478 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003479 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003480 }
3481}
3482
3483TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003484 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003485 const TSourceLoc &nameLoc)
3486{
Olli Etuaho55bde912017-10-25 13:41:13 +03003487 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003488 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003489 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003490 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003491 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003492 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003493 checkIsNotReserved(nameLoc, name);
3494 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003495 return param;
3496}
3497
Olli Etuaho55bde912017-10-25 13:41:13 +03003498TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003499 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003500 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003501{
Olli Etuaho55bde912017-10-25 13:41:13 +03003502 TType *type = new TType(publicType);
3503 return parseParameterDeclarator(type, name, nameLoc);
3504}
3505
Olli Etuahofbb1c792018-01-19 16:26:59 +02003506TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003507 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003508 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003509 const TSourceLoc &arrayLoc,
3510 TPublicType *elementType)
3511{
3512 checkArrayElementIsNotArray(arrayLoc, *elementType);
3513 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003514 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003515 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003516}
3517
Olli Etuaho95ed1942018-02-01 14:01:19 +02003518bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3519 const TIntermSequence &arguments,
3520 TType type,
3521 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003522{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003523 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003524 {
3525 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3526 return false;
3527 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003528 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003529 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003530 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003531 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003532 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3533 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003534 {
3535 error(line, "constructing from a non-dereferenced array", "constructor");
3536 return false;
3537 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003538 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003539 {
3540 if (dimensionalityFromElement == 1u)
3541 {
3542 error(line, "implicitly sized array of arrays constructor argument is not an array",
3543 "constructor");
3544 }
3545 else
3546 {
3547 error(line,
3548 "implicitly sized array of arrays constructor argument dimensionality is too "
3549 "low",
3550 "constructor");
3551 }
3552 return false;
3553 }
3554 }
3555 return true;
3556}
3557
Jamie Madillb98c3a82015-07-23 14:26:04 -04003558// This function is used to test for the correctness of the parameters passed to various constructor
3559// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560//
Olli Etuaho856c4972016-08-08 11:38:39 +03003561// 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 +00003562//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003563TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003565 TType type = fnCall->constructorType();
3566 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003567 if (type.isUnsizedArray())
3568 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003569 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003570 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003571 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003572 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003573 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003574 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003575 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003576 if (type.getOutermostArraySize() == 0u)
3577 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003578 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003579 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003580 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003581 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003582 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003583 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 }
3586 }
3587 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003588 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003589
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003590 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003591 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003592 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003593 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003594
Olli Etuaho95ed1942018-02-01 14:01:19 +02003595 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003596 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003597
Olli Etuaho765924f2018-01-04 12:48:36 +02003598 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003599}
3600
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003601//
3602// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003603// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003604//
Olli Etuaho13389b62016-10-16 11:48:18 +01003605TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003606 const TTypeQualifierBuilder &typeQualifierBuilder,
3607 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003608 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003609 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003610 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003611 const TSourceLoc &instanceLine,
3612 TIntermTyped *arrayIndex,
3613 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003614{
Olli Etuaho856c4972016-08-08 11:38:39 +03003615 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003616
Olli Etuaho77ba4082016-12-16 12:01:18 +00003617 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003618
Jiajia Qinbc585152017-06-23 15:42:17 +08003619 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003620 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003621 error(typeQualifier.line,
3622 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3623 "3.10",
3624 getQualifierString(typeQualifier.qualifier));
3625 }
3626 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3627 {
3628 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003629 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003630 }
3631
Martin Radev70866b82016-07-22 15:27:42 +03003632 if (typeQualifier.invariant)
3633 {
3634 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3635 }
3636
Jiajia Qinbc585152017-06-23 15:42:17 +08003637 if (typeQualifier.qualifier != EvqBuffer)
3638 {
3639 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3640 }
Olli Etuaho43364892017-02-13 16:00:12 +00003641
jchen10af713a22017-04-19 09:10:56 +08003642 // add array index
3643 unsigned int arraySize = 0;
3644 if (arrayIndex != nullptr)
3645 {
3646 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3647 }
3648
3649 if (mShaderVersion < 310)
3650 {
3651 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3652 }
3653 else
3654 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003655 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3656 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003657 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003658
Andrei Volykhina5527072017-03-22 16:46:30 +03003659 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3660
Jamie Madill099c0f32013-06-20 11:55:52 -04003661 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003662 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003663 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3664 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003665
Jamie Madill099c0f32013-06-20 11:55:52 -04003666 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3667 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003668 if (typeQualifier.qualifier == EvqUniform)
3669 {
3670 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3671 }
3672 else if (typeQualifier.qualifier == EvqBuffer)
3673 {
3674 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3675 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003676 }
3677
Jamie Madill1566ef72013-06-20 11:55:54 -04003678 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3679 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003680 if (typeQualifier.qualifier == EvqUniform)
3681 {
3682 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3683 }
3684 else if (typeQualifier.qualifier == EvqBuffer)
3685 {
3686 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3687 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003688 }
3689
Olli Etuaho856c4972016-08-08 11:38:39 +03003690 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003691
Martin Radev2cc85b32016-08-05 16:22:53 +03003692 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3693
Jamie Madill98493dd2013-07-08 14:39:03 -04003694 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303695 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003697 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303698 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003699 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303700 {
jchen10cc2a10e2017-05-03 14:05:12 +08003701 std::string reason("unsupported type - ");
3702 reason += fieldType->getBasicString();
3703 reason += " types are not allowed in interface blocks";
3704 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003705 }
3706
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003708 switch (qualifier)
3709 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003710 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003711 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003712 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003713 if (typeQualifier.qualifier == EvqBuffer)
3714 {
3715 error(field->line(), "invalid qualifier on shader storage block member",
3716 getQualifierString(qualifier));
3717 }
3718 break;
3719 case EvqBuffer:
3720 if (typeQualifier.qualifier == EvqUniform)
3721 {
3722 error(field->line(), "invalid qualifier on uniform block member",
3723 getQualifierString(qualifier));
3724 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 break;
3726 default:
3727 error(field->line(), "invalid qualifier on interface block member",
3728 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003730 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003731
Martin Radev70866b82016-07-22 15:27:42 +03003732 if (fieldType->isInvariant())
3733 {
3734 error(field->line(), "invalid qualifier on interface block member", "invariant");
3735 }
3736
Jamie Madilla5efff92013-06-06 11:56:47 -04003737 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003738 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003739 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003740 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003741
Jamie Madill98493dd2013-07-08 14:39:03 -04003742 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003743 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003744 error(field->line(), "invalid layout qualifier: cannot be used here",
3745 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003746 }
3747
Jamie Madill98493dd2013-07-08 14:39:03 -04003748 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003749 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003750 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003751 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003752 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003753 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003754 warning(field->line(),
3755 "extraneous layout qualifier: only has an effect on matrix types",
3756 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003757 }
3758
Jamie Madill98493dd2013-07-08 14:39:03 -04003759 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003760
Olli Etuahoebee5b32017-11-23 12:56:32 +02003761 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3762 typeQualifier.qualifier != EvqBuffer)
3763 {
3764 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3765 checkIsNotUnsizedArray(field->line(),
3766 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003767 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003768 }
3769
Jiajia Qinbc585152017-06-23 15:42:17 +08003770 if (typeQualifier.qualifier == EvqBuffer)
3771 {
3772 // set memory qualifiers
3773 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3774 // qualified with a memory qualifier, it is as if all of its members were declared with
3775 // the same memory qualifier.
3776 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3777 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3778 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3779 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3780 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3781 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3782 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3783 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3784 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3785 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3786 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003787 }
3788
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003789 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003790 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003791 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003792 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003793 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003794 }
3795
Olli Etuahob60d30f2018-01-16 12:31:06 +02003796 TType *interfaceBlockType =
3797 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003798 if (arrayIndex != nullptr)
3799 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003800 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003801 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003802
Olli Etuaho195be942017-12-04 23:40:14 +02003803 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003804 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3805 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003806 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003807 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003808 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003809
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003810 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003811 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003812 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003813 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3814 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003816 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003817
3818 // set parent pointer of the field variable
3819 fieldType->setInterfaceBlock(interfaceBlock);
3820
Olli Etuahob60d30f2018-01-16 12:31:06 +02003821 fieldType->setQualifier(typeQualifier.qualifier);
3822
Olli Etuaho195be942017-12-04 23:40:14 +02003823 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003824 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003825 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303826 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003827 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003828 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003829 }
3830 }
3831 }
3832 else
3833 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003834 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003835
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003836 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003837 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303838 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003839 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003840 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003841 }
3842
Olli Etuaho195be942017-12-04 23:40:14 +02003843 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3844 blockSymbol->setLine(typeQualifier.line);
3845 TIntermDeclaration *declaration = new TIntermDeclaration();
3846 declaration->appendDeclarator(blockSymbol);
3847 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003848
3849 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003850 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003851}
3852
Olli Etuahofbb1c792018-01-19 16:26:59 +02003853void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3854 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003855{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003856 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003857
3858 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003859 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303860 if (mStructNestingLevel > 1)
3861 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003862 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003863 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003864}
3865
3866void TParseContext::exitStructDeclaration()
3867{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003868 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003869}
3870
Olli Etuaho8a176262016-08-16 14:23:01 +03003871void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872{
Jamie Madillacb4b812016-11-07 13:50:29 -05003873 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303874 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003875 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876 }
3877
Arun Patole7e7e68d2015-05-22 12:02:25 +05303878 if (field.type()->getBasicType() != EbtStruct)
3879 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003880 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881 }
3882
3883 // We're already inside a structure definition at this point, so add
3884 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303885 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3886 {
Jamie Madill41a49272014-03-18 16:10:13 -04003887 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003888 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3889 {
3890 // This may happen in case there are nested struct definitions. While they are also
3891 // invalid GLSL, they don't cause a syntax error.
3892 reasonStream << "Struct nesting";
3893 }
3894 else
3895 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003896 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003897 }
3898 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003899 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003900 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003901 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003903}
3904
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003905//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003906// Parse an array index expression
3907//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3909 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303910 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003911{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003912 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3913 {
3914 if (baseExpression->getAsSymbolNode())
3915 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303916 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003917 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918 }
3919 else
3920 {
3921 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3922 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003923
Olli Etuaho3ec75682017-07-05 17:02:55 +03003924 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003926
Jiawei Shaod8105a02017-08-08 09:54:36 +08003927 if (baseExpression->getQualifier() == EvqPerVertexIn)
3928 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003929 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003930 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3931 {
3932 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3933 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3934 }
3935 }
3936
Jamie Madill21c1e452014-12-29 11:33:41 -05003937 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3938
Olli Etuaho36b05142015-11-12 13:10:42 +02003939 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3940 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3941 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3942 // index is a constant expression.
3943 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3944 {
3945 if (baseExpression->isInterfaceBlock())
3946 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003947 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003948 switch (baseExpression->getQualifier())
3949 {
3950 case EvqPerVertexIn:
3951 break;
3952 case EvqUniform:
3953 case EvqBuffer:
3954 error(location,
3955 "array indexes for uniform block arrays and shader storage block arrays "
3956 "must be constant integral expressions",
3957 "[");
3958 break;
3959 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003960 // We can reach here only in error cases.
3961 ASSERT(mDiagnostics->numErrors() > 0);
3962 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003963 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003964 }
3965 else if (baseExpression->getQualifier() == EvqFragmentOut)
3966 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003967 error(location,
3968 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003969 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003970 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3971 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003972 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003973 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003974 }
3975
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003976 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003977 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003978 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3979 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3980 // constant fold expressions that are not constant expressions). The most compatible way to
3981 // handle this case is to report a warning instead of an error and force the index to be in
3982 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003983 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003984 int index = 0;
3985 if (indexConstantUnion->getBasicType() == EbtInt)
3986 {
3987 index = indexConstantUnion->getIConst(0);
3988 }
3989 else if (indexConstantUnion->getBasicType() == EbtUInt)
3990 {
3991 index = static_cast<int>(indexConstantUnion->getUConst(0));
3992 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003993
3994 int safeIndex = -1;
3995
Olli Etuahoebee5b32017-11-23 12:56:32 +02003996 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003997 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003998 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3999 safeIndex = 0;
4000 }
4001
4002 if (!baseExpression->getType().isUnsizedArray())
4003 {
4004 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004005 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004006 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004008 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4009 {
4010 outOfRangeError(outOfRangeIndexIsError, location,
4011 "array index for gl_FragData must be zero when "
4012 "GL_EXT_draw_buffers is disabled",
4013 "[]");
4014 safeIndex = 0;
4015 }
4016 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004017 }
4018 // Only do generic out-of-range check if similar error hasn't already been reported.
4019 if (safeIndex < 0)
4020 {
4021 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 {
4023 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4024 baseExpression->getOutermostArraySize(),
4025 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004026 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004027 else if (baseExpression->isMatrix())
4028 {
4029 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4030 baseExpression->getType().getCols(),
4031 "matrix field selection out of range");
4032 }
4033 else
4034 {
4035 ASSERT(baseExpression->isVector());
4036 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4037 baseExpression->getType().getNominalSize(),
4038 "vector field selection out of range");
4039 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004040 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004041
Olli Etuahoebee5b32017-11-23 12:56:32 +02004042 ASSERT(safeIndex >= 0);
4043 // Data of constant unions can't be changed, because it may be shared with other
4044 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4045 // sanitized object.
4046 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4047 {
4048 TConstantUnion *safeConstantUnion = new TConstantUnion();
4049 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004050 indexExpression = new TIntermConstantUnion(
4051 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4052 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004053 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004054
Olli Etuahoebee5b32017-11-23 12:56:32 +02004055 TIntermBinary *node =
4056 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4057 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004058 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004059 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004060 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061
Olli Etuaho94bbed12018-03-20 14:44:53 +02004062 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4064 node->setLine(location);
4065 // Indirect indexing can never be constant folded.
4066 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004067}
4068
Olli Etuahoebee5b32017-11-23 12:56:32 +02004069int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4070 const TSourceLoc &location,
4071 int index,
4072 int arraySize,
4073 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004074{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004075 // Should not reach here with an unsized / runtime-sized array.
4076 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004077 // A negative index should already have been checked.
4078 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004079 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004080 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004081 std::stringstream reasonStream;
4082 reasonStream << reason << " '" << index << "'";
4083 std::string token = reasonStream.str();
4084 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004085 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004086 }
4087 return index;
4088}
4089
Jamie Madillb98c3a82015-07-23 14:26:04 -04004090TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4091 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004092 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004093 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004094{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004095 if (baseExpression->isArray())
4096 {
4097 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004098 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004099 }
4100
4101 if (baseExpression->isVector())
4102 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004103 TVector<int> fieldOffsets;
4104 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4105 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004107 fieldOffsets.resize(1);
4108 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004110 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4111 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112
Olli Etuaho765924f2018-01-04 12:48:36 +02004113 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115 else if (baseExpression->getBasicType() == EbtStruct)
4116 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304117 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004118 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 {
4120 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004121 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122 }
4123 else
4124 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004125 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 {
4131 fieldFound = true;
4132 break;
4133 }
4134 }
4135 if (fieldFound)
4136 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004137 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004138 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004139 TIntermBinary *node =
4140 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4141 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004142 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004143 }
4144 else
4145 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004146 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004147 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 }
4149 }
4150 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004151 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004152 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304153 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004154 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004155 {
4156 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004157 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004158 }
4159 else
4160 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004161 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004163 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004165 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004166 {
4167 fieldFound = true;
4168 break;
4169 }
4170 }
4171 if (fieldFound)
4172 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004173 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004174 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004175 TIntermBinary *node =
4176 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4177 node->setLine(dotLocation);
4178 // Indexing interface blocks can never be constant folded.
4179 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004180 }
4181 else
4182 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004183 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004184 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 }
4186 }
4187 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004188 else
4189 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004190 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004191 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004192 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004193 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004194 }
4195 else
4196 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304197 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004198 " field selection requires structure, vector, or interface block on left hand "
4199 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004200 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004201 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004202 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004203 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004204}
4205
Olli Etuahofbb1c792018-01-19 16:26:59 +02004206TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004207 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004208{
Jamie Madill2f294c92017-11-20 14:47:26 -05004209 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004210
4211 if (qualifierType == "shared")
4212 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004213 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004214 {
4215 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4216 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004217 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004218 }
4219 else if (qualifierType == "packed")
4220 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004221 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004222 {
4223 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4224 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004225 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004226 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004227 else if (qualifierType == "std430")
4228 {
4229 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4230 qualifier.blockStorage = EbsStd430;
4231 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004232 else if (qualifierType == "std140")
4233 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004234 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 }
4236 else if (qualifierType == "row_major")
4237 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004238 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 }
4240 else if (qualifierType == "column_major")
4241 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004242 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004243 }
4244 else if (qualifierType == "location")
4245 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004246 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004247 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004248 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004249 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004250 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004251 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4252 {
4253 qualifier.yuv = true;
4254 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004255 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004256 else if (qualifierType == "rgba32f")
4257 {
4258 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4259 qualifier.imageInternalFormat = EiifRGBA32F;
4260 }
4261 else if (qualifierType == "rgba16f")
4262 {
4263 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4264 qualifier.imageInternalFormat = EiifRGBA16F;
4265 }
4266 else if (qualifierType == "r32f")
4267 {
4268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4269 qualifier.imageInternalFormat = EiifR32F;
4270 }
4271 else if (qualifierType == "rgba8")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifRGBA8;
4275 }
4276 else if (qualifierType == "rgba8_snorm")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4280 }
4281 else if (qualifierType == "rgba32i")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifRGBA32I;
4285 }
4286 else if (qualifierType == "rgba16i")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA16I;
4290 }
4291 else if (qualifierType == "rgba8i")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA8I;
4295 }
4296 else if (qualifierType == "r32i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifR32I;
4300 }
4301 else if (qualifierType == "rgba32ui")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA32UI;
4305 }
4306 else if (qualifierType == "rgba16ui")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA16UI;
4310 }
4311 else if (qualifierType == "rgba8ui")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifRGBA8UI;
4315 }
4316 else if (qualifierType == "r32ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifR32UI;
4320 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004321 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4322 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004323 {
4324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4325 qualifier.primitiveType = EptPoints;
4326 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004327 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4328 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004329 {
4330 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4331 qualifier.primitiveType = EptLines;
4332 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004333 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4334 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004335 {
4336 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4337 qualifier.primitiveType = EptLinesAdjacency;
4338 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004339 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4340 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004341 {
4342 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4343 qualifier.primitiveType = EptTriangles;
4344 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004345 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4346 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004347 {
4348 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4349 qualifier.primitiveType = EptTrianglesAdjacency;
4350 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004351 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4352 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004353 {
4354 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4355 qualifier.primitiveType = EptLineStrip;
4356 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004357 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4358 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004359 {
4360 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4361 qualifier.primitiveType = EptTriangleStrip;
4362 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004363
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004364 else
4365 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004366 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004367 }
4368
Jamie Madilla5efff92013-06-06 11:56:47 -04004369 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004370}
4371
Olli Etuahofbb1c792018-01-19 16:26:59 +02004372void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004373 const TSourceLoc &qualifierTypeLine,
4374 int intValue,
4375 const TSourceLoc &intValueLine,
4376 const std::string &intValueString,
4377 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004378 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004379{
Olli Etuaho856c4972016-08-08 11:38:39 +03004380 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004381 if (intValue < 1)
4382 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004383 std::stringstream reasonStream;
4384 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4385 std::string reason = reasonStream.str();
4386 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004387 }
4388 (*localSize)[index] = intValue;
4389}
4390
Olli Etuaho09b04a22016-12-15 13:30:26 +00004391void TParseContext::parseNumViews(int intValue,
4392 const TSourceLoc &intValueLine,
4393 const std::string &intValueString,
4394 int *numViews)
4395{
4396 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4397 // specification.
4398 if (intValue < 1)
4399 {
4400 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4401 }
4402 *numViews = intValue;
4403}
4404
Shaob5cc1192017-07-06 10:47:20 +08004405void TParseContext::parseInvocations(int intValue,
4406 const TSourceLoc &intValueLine,
4407 const std::string &intValueString,
4408 int *numInvocations)
4409{
4410 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4411 // it doesn't make sense to accept invocations <= 0.
4412 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4413 {
4414 error(intValueLine,
4415 "out of range: invocations must be in the range of [1, "
4416 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4417 intValueString.c_str());
4418 }
4419 else
4420 {
4421 *numInvocations = intValue;
4422 }
4423}
4424
4425void TParseContext::parseMaxVertices(int intValue,
4426 const TSourceLoc &intValueLine,
4427 const std::string &intValueString,
4428 int *maxVertices)
4429{
4430 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4431 // it doesn't make sense to accept max_vertices < 0.
4432 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4433 {
4434 error(
4435 intValueLine,
4436 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4437 intValueString.c_str());
4438 }
4439 else
4440 {
4441 *maxVertices = intValue;
4442 }
4443}
4444
Olli Etuahofbb1c792018-01-19 16:26:59 +02004445TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004446 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004447 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304448 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004449{
Jamie Madill2f294c92017-11-20 14:47:26 -05004450 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004451
Martin Radev802abe02016-08-04 17:48:32 +03004452 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453
Martin Radev802abe02016-08-04 17:48:32 +03004454 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004455 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004456 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004457 if (intValue < 0)
4458 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004459 error(intValueLine, "out of range: location must be non-negative",
4460 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004461 }
4462 else
4463 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004464 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004465 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004467 }
Olli Etuaho43364892017-02-13 16:00:12 +00004468 else if (qualifierType == "binding")
4469 {
4470 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4471 if (intValue < 0)
4472 {
4473 error(intValueLine, "out of range: binding must be non-negative",
4474 intValueString.c_str());
4475 }
4476 else
4477 {
4478 qualifier.binding = intValue;
4479 }
4480 }
jchen104cdac9e2017-05-08 11:01:20 +08004481 else if (qualifierType == "offset")
4482 {
4483 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4484 if (intValue < 0)
4485 {
4486 error(intValueLine, "out of range: offset must be non-negative",
4487 intValueString.c_str());
4488 }
4489 else
4490 {
4491 qualifier.offset = intValue;
4492 }
4493 }
Martin Radev802abe02016-08-04 17:48:32 +03004494 else if (qualifierType == "local_size_x")
4495 {
4496 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4497 &qualifier.localSize);
4498 }
4499 else if (qualifierType == "local_size_y")
4500 {
4501 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4502 &qualifier.localSize);
4503 }
4504 else if (qualifierType == "local_size_z")
4505 {
4506 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4507 &qualifier.localSize);
4508 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004509 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004510 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004511 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4512 {
4513 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4514 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004515 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004516 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4517 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004518 {
4519 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4520 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004521 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4522 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004523 {
4524 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4525 }
4526
Martin Radev802abe02016-08-04 17:48:32 +03004527 else
4528 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004529 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004530 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004531
Jamie Madilla5efff92013-06-06 11:56:47 -04004532 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004533}
4534
Olli Etuaho613b9592016-09-05 12:05:53 +03004535TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4536{
4537 return new TTypeQualifierBuilder(
4538 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4539 mShaderVersion);
4540}
4541
Olli Etuahocce89652017-06-19 16:04:09 +03004542TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4543 const TSourceLoc &loc)
4544{
4545 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4546 return new TStorageQualifierWrapper(qualifier, loc);
4547}
4548
4549TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4550{
4551 if (getShaderType() == GL_VERTEX_SHADER)
4552 {
4553 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4554 }
4555 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4556}
4557
4558TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4559{
4560 if (declaringFunction())
4561 {
4562 return new TStorageQualifierWrapper(EvqIn, loc);
4563 }
Shaob5cc1192017-07-06 10:47:20 +08004564
4565 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004566 {
Shaob5cc1192017-07-06 10:47:20 +08004567 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004568 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004569 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004570 {
4571 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4572 }
4573 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004574 }
Shaob5cc1192017-07-06 10:47:20 +08004575 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004576 {
Shaob5cc1192017-07-06 10:47:20 +08004577 if (mShaderVersion < 300)
4578 {
4579 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4580 }
4581 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004582 }
Shaob5cc1192017-07-06 10:47:20 +08004583 case GL_COMPUTE_SHADER:
4584 {
4585 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4586 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004587 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004588 {
4589 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4590 }
4591 default:
4592 {
4593 UNREACHABLE();
4594 return new TStorageQualifierWrapper(EvqLast, loc);
4595 }
Olli Etuahocce89652017-06-19 16:04:09 +03004596 }
Olli Etuahocce89652017-06-19 16:04:09 +03004597}
4598
4599TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4600{
4601 if (declaringFunction())
4602 {
4603 return new TStorageQualifierWrapper(EvqOut, loc);
4604 }
Shaob5cc1192017-07-06 10:47:20 +08004605 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004606 {
Shaob5cc1192017-07-06 10:47:20 +08004607 case GL_VERTEX_SHADER:
4608 {
4609 if (mShaderVersion < 300)
4610 {
4611 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4612 }
4613 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4614 }
4615 case GL_FRAGMENT_SHADER:
4616 {
4617 if (mShaderVersion < 300)
4618 {
4619 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4620 }
4621 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4622 }
4623 case GL_COMPUTE_SHADER:
4624 {
4625 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4626 return new TStorageQualifierWrapper(EvqLast, loc);
4627 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004628 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004629 {
4630 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4631 }
4632 default:
4633 {
4634 UNREACHABLE();
4635 return new TStorageQualifierWrapper(EvqLast, loc);
4636 }
Olli Etuahocce89652017-06-19 16:04:09 +03004637 }
Olli Etuahocce89652017-06-19 16:04:09 +03004638}
4639
4640TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4641{
4642 if (!declaringFunction())
4643 {
4644 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4645 }
4646 return new TStorageQualifierWrapper(EvqInOut, loc);
4647}
4648
Jamie Madillb98c3a82015-07-23 14:26:04 -04004649TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004650 TLayoutQualifier rightQualifier,
4651 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004652{
Martin Radevc28888b2016-07-22 15:27:42 +03004653 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004654 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004655}
4656
Olli Etuahofbb1c792018-01-19 16:26:59 +02004657TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4658 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004659{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004660 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004661 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004662}
4663
Olli Etuahofbb1c792018-01-19 16:26:59 +02004664TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004665 const TSourceLoc &loc,
4666 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004667{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004668 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004669 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004670}
4671
Olli Etuaho722bfb52017-10-26 17:00:11 +03004672void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4673 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004674 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004675 const TSourceLoc &location)
4676{
4677 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4678 {
4679 if ((*fieldIter)->name() == name)
4680 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004681 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004682 }
4683 }
4684}
4685
4686TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4687{
4688 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4689 ++fieldIter)
4690 {
4691 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4692 location);
4693 }
4694 return fields;
4695}
4696
Olli Etuaho4de340a2016-12-16 09:32:03 +00004697TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4698 const TFieldList *newlyAddedFields,
4699 const TSourceLoc &location)
4700{
4701 for (TField *field : *newlyAddedFields)
4702 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004703 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4704 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004705 processedFields->push_back(field);
4706 }
4707 return processedFields;
4708}
4709
Martin Radev70866b82016-07-22 15:27:42 +03004710TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4711 const TTypeQualifierBuilder &typeQualifierBuilder,
4712 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004713 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004714{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004715 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004716
Martin Radev70866b82016-07-22 15:27:42 +03004717 typeSpecifier->qualifier = typeQualifier.qualifier;
4718 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004719 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004720 typeSpecifier->invariant = typeQualifier.invariant;
4721 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304722 {
Martin Radev70866b82016-07-22 15:27:42 +03004723 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004724 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004725 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004726}
4727
Jamie Madillb98c3a82015-07-23 14:26:04 -04004728TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004729 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004730{
Martin Radev4a9cd802016-09-01 16:51:51 +03004731 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4732 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004733
Olli Etuahofbb1c792018-01-19 16:26:59 +02004734 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004735 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004736
Martin Radev4a9cd802016-09-01 16:51:51 +03004737 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004738
Olli Etuahod5f44c92017-11-29 17:15:40 +02004739 TFieldList *fieldList = new TFieldList();
4740
4741 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304742 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 TType *type = new TType(typeSpecifier);
4744 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304745 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004746 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004747 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004748 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004749 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004750
Olli Etuahod5f44c92017-11-29 17:15:40 +02004751 TField *field = new TField(type, declarator->name(), declarator->line());
4752 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4753 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754 }
4755
Olli Etuahod5f44c92017-11-29 17:15:40 +02004756 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757}
4758
Martin Radev4a9cd802016-09-01 16:51:51 +03004759TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4760 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004761 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004762 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004764 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004765 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004766 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004767 structSymbolType = SymbolType::Empty;
4768 }
4769 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004770
Jamie Madill9b820842015-02-12 10:40:10 -05004771 // Store a bool in the struct if we're at global scope, to allow us to
4772 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004773 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004774
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004775 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004777 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004778 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304779 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004780 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004781 }
4782 }
4783
4784 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004785 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004786 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004787 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004788 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 switch (qualifier)
4790 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004791 case EvqGlobal:
4792 case EvqTemporary:
4793 break;
4794 default:
4795 error(field.line(), "invalid qualifier on struct member",
4796 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004797 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004798 }
Martin Radev70866b82016-07-22 15:27:42 +03004799 if (field.type()->isInvariant())
4800 {
4801 error(field.line(), "invalid qualifier on struct member", "invariant");
4802 }
jchen104cdac9e2017-05-08 11:01:20 +08004803 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4804 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004805 {
4806 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4807 }
4808
Olli Etuahoebee5b32017-11-23 12:56:32 +02004809 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004810 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004811
Olli Etuaho43364892017-02-13 16:00:12 +00004812 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4813
4814 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004815
4816 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004817 }
4818
Martin Radev4a9cd802016-09-01 16:51:51 +03004819 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004820 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821 exitStructDeclaration();
4822
Martin Radev4a9cd802016-09-01 16:51:51 +03004823 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824}
4825
Jamie Madillb98c3a82015-07-23 14:26:04 -04004826TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004827 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004828 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004829{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004830 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004832 init->isVector())
4833 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004834 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4835 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 return nullptr;
4837 }
4838
Olli Etuaho923ecef2017-10-11 12:01:38 +03004839 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004840 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004841 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004842 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004843 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004844 }
4845
Olli Etuaho94bbed12018-03-20 14:44:53 +02004846 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004847 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4848 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004849 return node;
4850}
4851
4852TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4853{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004854 if (mSwitchNestingLevel == 0)
4855 {
4856 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 return nullptr;
4858 }
4859 if (condition == nullptr)
4860 {
4861 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004862 return nullptr;
4863 }
4864 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004865 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 {
4867 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 }
4869 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004870 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4871 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4872 // fold in case labels.
4873 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004874 {
4875 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004876 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004877 TIntermCase *node = new TIntermCase(condition);
4878 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004879 return node;
4880}
4881
4882TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4883{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 if (mSwitchNestingLevel == 0)
4885 {
4886 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 return nullptr;
4888 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004889 TIntermCase *node = new TIntermCase(nullptr);
4890 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004891 return node;
4892}
4893
Jamie Madillb98c3a82015-07-23 14:26:04 -04004894TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4895 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004896 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004897{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004898 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004899
4900 switch (op)
4901 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004902 case EOpLogicalNot:
4903 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4904 child->isVector())
4905 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004906 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004907 return nullptr;
4908 }
4909 break;
4910 case EOpBitwiseNot:
4911 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4912 child->isMatrix() || child->isArray())
4913 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004914 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004915 return nullptr;
4916 }
4917 break;
4918 case EOpPostIncrement:
4919 case EOpPreIncrement:
4920 case EOpPostDecrement:
4921 case EOpPreDecrement:
4922 case EOpNegative:
4923 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004924 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4925 child->getBasicType() == EbtBool || child->isArray() ||
4926 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004927 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004928 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 return nullptr;
4930 }
Nico Weber41b072b2018-02-09 10:01:32 -05004931 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932 // Operators for built-ins are already type checked against their prototype.
4933 default:
4934 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004935 }
4936
Jiajia Qinbc585152017-06-23 15:42:17 +08004937 if (child->getMemoryQualifier().writeonly)
4938 {
4939 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4940 return nullptr;
4941 }
4942
Olli Etuaho94bbed12018-03-20 14:44:53 +02004943 markStaticReadIfSymbol(child);
Olli Etuahof119a262016-08-19 15:54:22 +03004944 TIntermUnary *node = new TIntermUnary(op, child);
4945 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004946
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004947 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004948}
4949
Olli Etuaho09b22472015-02-11 11:47:26 +02004950TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4951{
Olli Etuahocce89652017-06-19 16:04:09 +03004952 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004953 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004954 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004955 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 return child;
4957 }
4958 return node;
4959}
4960
Jamie Madillb98c3a82015-07-23 14:26:04 -04004961TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4962 TIntermTyped *child,
4963 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004964{
Olli Etuaho856c4972016-08-08 11:38:39 +03004965 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004966 return addUnaryMath(op, child, loc);
4967}
4968
Olli Etuaho765924f2018-01-04 12:48:36 +02004969TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4970{
4971 // If we can, we should return the folded version of the expression for subsequent parsing. This
4972 // enables folding the containing expression during parsing as well, instead of the separate
4973 // FoldExpressions() step where folding nested expressions requires multiple full AST
4974 // traversals.
4975
4976 // Even if folding fails the fold() functions return some node representing the expression,
4977 // typically the original node. So "folded" can be assumed to be non-null.
4978 TIntermTyped *folded = expression->fold(mDiagnostics);
4979 ASSERT(folded != nullptr);
4980 if (folded->getQualifier() == expression->getQualifier())
4981 {
4982 // We need this expression to have the correct qualifier when validating the consuming
4983 // expression. So we can only return the folded node from here in case it has the same
4984 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4985 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4986 // 1. (true ? 1.0 : non_constant)
4987 // 2. (non_constant, 1.0)
4988 return folded;
4989 }
4990 return expression;
4991}
4992
Jamie Madillb98c3a82015-07-23 14:26:04 -04004993bool TParseContext::binaryOpCommonCheck(TOperator op,
4994 TIntermTyped *left,
4995 TIntermTyped *right,
4996 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004997{
jchen10b4cf5652017-05-05 18:51:17 +08004998 // Check opaque types are not allowed to be operands in expressions other than array indexing
4999 // and structure member selection.
5000 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5001 {
5002 switch (op)
5003 {
5004 case EOpIndexDirect:
5005 case EOpIndexIndirect:
5006 break;
jchen10b4cf5652017-05-05 18:51:17 +08005007
5008 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005009 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005010 error(loc, "Invalid operation for variables with an opaque type",
5011 GetOperatorString(op));
5012 return false;
5013 }
5014 }
jchen10cc2a10e2017-05-03 14:05:12 +08005015
Jiajia Qinbc585152017-06-23 15:42:17 +08005016 if (right->getMemoryQualifier().writeonly)
5017 {
5018 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5019 return false;
5020 }
5021
5022 if (left->getMemoryQualifier().writeonly)
5023 {
5024 switch (op)
5025 {
5026 case EOpAssign:
5027 case EOpInitialize:
5028 case EOpIndexDirect:
5029 case EOpIndexIndirect:
5030 case EOpIndexDirectStruct:
5031 case EOpIndexDirectInterfaceBlock:
5032 break;
5033 default:
5034 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5035 return false;
5036 }
5037 }
5038
Olli Etuaho244be012016-08-18 15:26:02 +03005039 if (left->getType().getStruct() || right->getType().getStruct())
5040 {
5041 switch (op)
5042 {
5043 case EOpIndexDirectStruct:
5044 ASSERT(left->getType().getStruct());
5045 break;
5046 case EOpEqual:
5047 case EOpNotEqual:
5048 case EOpAssign:
5049 case EOpInitialize:
5050 if (left->getType() != right->getType())
5051 {
5052 return false;
5053 }
5054 break;
5055 default:
5056 error(loc, "Invalid operation for structs", GetOperatorString(op));
5057 return false;
5058 }
5059 }
5060
Olli Etuaho94050052017-05-08 14:17:44 +03005061 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5062 {
5063 switch (op)
5064 {
5065 case EOpIndexDirectInterfaceBlock:
5066 ASSERT(left->getType().getInterfaceBlock());
5067 break;
5068 default:
5069 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5070 return false;
5071 }
5072 }
5073
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005074 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005075 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005076 error(loc, "array / non-array mismatch", GetOperatorString(op));
5077 return false;
5078 }
5079
5080 if (left->isArray())
5081 {
5082 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005083 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005084 {
5085 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5086 return false;
5087 }
5088
Olli Etuahoe79904c2015-03-18 16:56:42 +02005089 switch (op)
5090 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005091 case EOpEqual:
5092 case EOpNotEqual:
5093 case EOpAssign:
5094 case EOpInitialize:
5095 break;
5096 default:
5097 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5098 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005099 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005100 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005101 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005102 {
5103 error(loc, "array size mismatch", GetOperatorString(op));
5104 return false;
5105 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005106 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005107
5108 // Check ops which require integer / ivec parameters
5109 bool isBitShift = false;
5110 switch (op)
5111 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005112 case EOpBitShiftLeft:
5113 case EOpBitShiftRight:
5114 case EOpBitShiftLeftAssign:
5115 case EOpBitShiftRightAssign:
5116 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5117 // check that the basic type is an integer type.
5118 isBitShift = true;
5119 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5120 {
5121 return false;
5122 }
5123 break;
5124 case EOpBitwiseAnd:
5125 case EOpBitwiseXor:
5126 case EOpBitwiseOr:
5127 case EOpBitwiseAndAssign:
5128 case EOpBitwiseXorAssign:
5129 case EOpBitwiseOrAssign:
5130 // It is enough to check the type of only one operand, since later it
5131 // is checked that the operand types match.
5132 if (!IsInteger(left->getBasicType()))
5133 {
5134 return false;
5135 }
5136 break;
5137 default:
5138 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005139 }
5140
5141 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5142 // So the basic type should usually match.
5143 if (!isBitShift && left->getBasicType() != right->getBasicType())
5144 {
5145 return false;
5146 }
5147
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005148 // Check that:
5149 // 1. Type sizes match exactly on ops that require that.
5150 // 2. Restrictions for structs that contain arrays or samplers are respected.
5151 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005152 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005153 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 case EOpAssign:
5155 case EOpInitialize:
5156 case EOpEqual:
5157 case EOpNotEqual:
5158 // ESSL 1.00 sections 5.7, 5.8, 5.9
5159 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5160 {
5161 error(loc, "undefined operation for structs containing arrays",
5162 GetOperatorString(op));
5163 return false;
5164 }
5165 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5166 // we interpret the spec so that this extends to structs containing samplers,
5167 // similarly to ESSL 1.00 spec.
5168 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5169 left->getType().isStructureContainingSamplers())
5170 {
5171 error(loc, "undefined operation for structs containing samplers",
5172 GetOperatorString(op));
5173 return false;
5174 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005175
Olli Etuahoe1805592017-01-02 16:41:20 +00005176 if ((left->getNominalSize() != right->getNominalSize()) ||
5177 (left->getSecondarySize() != right->getSecondarySize()))
5178 {
5179 error(loc, "dimension mismatch", GetOperatorString(op));
5180 return false;
5181 }
5182 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183 case EOpLessThan:
5184 case EOpGreaterThan:
5185 case EOpLessThanEqual:
5186 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005187 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005188 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005189 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005190 return false;
5191 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005192 break;
5193 case EOpAdd:
5194 case EOpSub:
5195 case EOpDiv:
5196 case EOpIMod:
5197 case EOpBitShiftLeft:
5198 case EOpBitShiftRight:
5199 case EOpBitwiseAnd:
5200 case EOpBitwiseXor:
5201 case EOpBitwiseOr:
5202 case EOpAddAssign:
5203 case EOpSubAssign:
5204 case EOpDivAssign:
5205 case EOpIModAssign:
5206 case EOpBitShiftLeftAssign:
5207 case EOpBitShiftRightAssign:
5208 case EOpBitwiseAndAssign:
5209 case EOpBitwiseXorAssign:
5210 case EOpBitwiseOrAssign:
5211 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5212 {
5213 return false;
5214 }
5215
5216 // Are the sizes compatible?
5217 if (left->getNominalSize() != right->getNominalSize() ||
5218 left->getSecondarySize() != right->getSecondarySize())
5219 {
5220 // If the nominal sizes of operands do not match:
5221 // One of them must be a scalar.
5222 if (!left->isScalar() && !right->isScalar())
5223 return false;
5224
5225 // In the case of compound assignment other than multiply-assign,
5226 // the right side needs to be a scalar. Otherwise a vector/matrix
5227 // would be assigned to a scalar. A scalar can't be shifted by a
5228 // vector either.
5229 if (!right->isScalar() &&
5230 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5231 return false;
5232 }
5233 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005234 default:
5235 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005236 }
5237
Olli Etuahod6b14282015-03-17 14:31:35 +02005238 return true;
5239}
5240
Olli Etuaho1dded802016-08-18 18:13:13 +03005241bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5242 const TType &left,
5243 const TType &right)
5244{
5245 switch (op)
5246 {
5247 case EOpMul:
5248 case EOpMulAssign:
5249 return left.getNominalSize() == right.getNominalSize() &&
5250 left.getSecondarySize() == right.getSecondarySize();
5251 case EOpVectorTimesScalar:
5252 return true;
5253 case EOpVectorTimesScalarAssign:
5254 ASSERT(!left.isMatrix() && !right.isMatrix());
5255 return left.isVector() && !right.isVector();
5256 case EOpVectorTimesMatrix:
5257 return left.getNominalSize() == right.getRows();
5258 case EOpVectorTimesMatrixAssign:
5259 ASSERT(!left.isMatrix() && right.isMatrix());
5260 return left.isVector() && left.getNominalSize() == right.getRows() &&
5261 left.getNominalSize() == right.getCols();
5262 case EOpMatrixTimesVector:
5263 return left.getCols() == right.getNominalSize();
5264 case EOpMatrixTimesScalar:
5265 return true;
5266 case EOpMatrixTimesScalarAssign:
5267 ASSERT(left.isMatrix() && !right.isMatrix());
5268 return !right.isVector();
5269 case EOpMatrixTimesMatrix:
5270 return left.getCols() == right.getRows();
5271 case EOpMatrixTimesMatrixAssign:
5272 ASSERT(left.isMatrix() && right.isMatrix());
5273 // We need to check two things:
5274 // 1. The matrix multiplication step is valid.
5275 // 2. The result will have the same number of columns as the lvalue.
5276 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5277
5278 default:
5279 UNREACHABLE();
5280 return false;
5281 }
5282}
5283
Jamie Madillb98c3a82015-07-23 14:26:04 -04005284TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5285 TIntermTyped *left,
5286 TIntermTyped *right,
5287 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005288{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005289 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005290 return nullptr;
5291
Olli Etuahofc1806e2015-03-17 13:03:11 +02005292 switch (op)
5293 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005294 case EOpEqual:
5295 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 case EOpLessThan:
5297 case EOpGreaterThan:
5298 case EOpLessThanEqual:
5299 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 break;
5301 case EOpLogicalOr:
5302 case EOpLogicalXor:
5303 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005304 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5305 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005306 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 {
5308 return nullptr;
5309 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005310 // Basic types matching should have been already checked.
5311 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 break;
5313 case EOpAdd:
5314 case EOpSub:
5315 case EOpDiv:
5316 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005317 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5318 !right->getType().getStruct());
5319 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005320 {
5321 return nullptr;
5322 }
5323 break;
5324 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005325 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5326 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005327 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005328 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005329 {
5330 return nullptr;
5331 }
5332 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005333 default:
5334 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005335 }
5336
Olli Etuaho1dded802016-08-18 18:13:13 +03005337 if (op == EOpMul)
5338 {
5339 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5340 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5341 {
5342 return nullptr;
5343 }
5344 }
5345
Olli Etuaho3fdec912016-08-18 15:08:06 +03005346 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005347 ASSERT(op != EOpAssign);
5348 markStaticReadIfSymbol(left);
5349 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005350 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005351 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005352}
5353
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5355 TIntermTyped *left,
5356 TIntermTyped *right,
5357 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005358{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005359 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005360 if (node == 0)
5361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005362 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5363 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005364 return left;
5365 }
5366 return node;
5367}
5368
Jamie Madillb98c3a82015-07-23 14:26:04 -04005369TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5370 TIntermTyped *left,
5371 TIntermTyped *right,
5372 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005373{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005374 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005375 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005376 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005377 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5378 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005379 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005380 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005381 }
5382 return node;
5383}
5384
Jamie Madillb98c3a82015-07-23 14:26:04 -04005385TIntermTyped *TParseContext::addAssign(TOperator op,
5386 TIntermTyped *left,
5387 TIntermTyped *right,
5388 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005389{
Olli Etuahocce89652017-06-19 16:04:09 +03005390 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005391 TIntermBinary *node = nullptr;
5392 if (binaryOpCommonCheck(op, left, right, loc))
5393 {
5394 if (op == EOpMulAssign)
5395 {
5396 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5397 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5398 {
5399 node = new TIntermBinary(op, left, right);
5400 }
5401 }
5402 else
5403 {
5404 node = new TIntermBinary(op, left, right);
5405 }
5406 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005407 if (node == nullptr)
5408 {
5409 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005410 return left;
5411 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005412 if (op != EOpAssign)
5413 {
5414 markStaticReadIfSymbol(left);
5415 }
5416 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005417 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005418 return node;
5419}
5420
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005421TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5422 TIntermTyped *right,
5423 const TSourceLoc &loc)
5424{
Corentin Wallez0d959252016-07-12 17:26:32 -04005425 // WebGL2 section 5.26, the following results in an error:
5426 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005427 if (mShaderSpec == SH_WEBGL2_SPEC &&
5428 (left->isArray() || left->getBasicType() == EbtVoid ||
5429 left->getType().isStructureContainingArrays() || right->isArray() ||
5430 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005431 {
5432 error(loc,
5433 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5434 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005435 }
5436
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005437 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005438 markStaticReadIfSymbol(left);
5439 markStaticReadIfSymbol(right);
5440 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005441
5442 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005443}
5444
Olli Etuaho49300862015-02-20 14:54:49 +02005445TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5446{
5447 switch (op)
5448 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005449 case EOpContinue:
5450 if (mLoopNestingLevel <= 0)
5451 {
5452 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005453 }
5454 break;
5455 case EOpBreak:
5456 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5457 {
5458 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 }
5460 break;
5461 case EOpReturn:
5462 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5463 {
5464 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 }
5466 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005467 case EOpKill:
5468 if (mShaderType != GL_FRAGMENT_SHADER)
5469 {
5470 error(loc, "discard supported in fragment shaders only", "discard");
5471 }
5472 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005473 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005474 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005475 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005476 }
Olli Etuahocce89652017-06-19 16:04:09 +03005477 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005478}
5479
Jamie Madillb98c3a82015-07-23 14:26:04 -04005480TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005481 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005482 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005483{
Olli Etuahocce89652017-06-19 16:04:09 +03005484 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005485 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005486 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005487 ASSERT(op == EOpReturn);
5488 mFunctionReturnsValue = true;
5489 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5490 {
5491 error(loc, "void function cannot return a value", "return");
5492 }
5493 else if (*mCurrentFunctionType != expression->getType())
5494 {
5495 error(loc, "function return is not matching type:", "return");
5496 }
Olli Etuaho49300862015-02-20 14:54:49 +02005497 }
Olli Etuahocce89652017-06-19 16:04:09 +03005498 TIntermBranch *node = new TIntermBranch(op, expression);
5499 node->setLine(loc);
5500 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005501}
5502
Olli Etuaho94bbed12018-03-20 14:44:53 +02005503void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5504{
5505 if (statement != nullptr)
5506 {
5507 markStaticReadIfSymbol(statement);
5508 block->appendStatement(statement);
5509 }
5510}
5511
Martin Radev84aa2dc2017-09-11 15:51:02 +03005512void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5513{
5514 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005515 const TFunction *func = functionCall->getFunction();
5516 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005517 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005518 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005519 TIntermNode *componentNode = nullptr;
5520 TIntermSequence *arguments = functionCall->getSequence();
5521 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5522 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5523 ASSERT(sampler != nullptr);
5524 switch (sampler->getBasicType())
5525 {
5526 case EbtSampler2D:
5527 case EbtISampler2D:
5528 case EbtUSampler2D:
5529 case EbtSampler2DArray:
5530 case EbtISampler2DArray:
5531 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005532 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005533 (isTextureGatherOffset && arguments->size() == 4u))
5534 {
5535 componentNode = arguments->back();
5536 }
5537 break;
5538 case EbtSamplerCube:
5539 case EbtISamplerCube:
5540 case EbtUSamplerCube:
5541 ASSERT(!isTextureGatherOffset);
5542 if (arguments->size() == 3u)
5543 {
5544 componentNode = arguments->back();
5545 }
5546 break;
5547 case EbtSampler2DShadow:
5548 case EbtSampler2DArrayShadow:
5549 case EbtSamplerCubeShadow:
5550 break;
5551 default:
5552 UNREACHABLE();
5553 break;
5554 }
5555 if (componentNode)
5556 {
5557 const TIntermConstantUnion *componentConstantUnion =
5558 componentNode->getAsConstantUnion();
5559 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5560 {
5561 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005562 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005563 }
5564 else
5565 {
5566 int component = componentConstantUnion->getIConst(0);
5567 if (component < 0 || component > 3)
5568 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005569 error(functionCall->getLine(), "Component must be in the range [0;3]",
5570 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005571 }
5572 }
5573 }
5574 }
5575}
5576
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005577void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5578{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005579 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005580 const TFunction *func = functionCall->getFunction();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005581 TIntermNode *offset = nullptr;
5582 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005583 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005584 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005585 {
5586 offset = arguments->back();
5587 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005588 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005589 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005590 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005591 ASSERT(arguments->size() >= 3);
5592 offset = (*arguments)[2];
5593 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005594 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005595 {
5596 ASSERT(arguments->size() >= 3u);
5597 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5598 ASSERT(sampler != nullptr);
5599 switch (sampler->getBasicType())
5600 {
5601 case EbtSampler2D:
5602 case EbtISampler2D:
5603 case EbtUSampler2D:
5604 case EbtSampler2DArray:
5605 case EbtISampler2DArray:
5606 case EbtUSampler2DArray:
5607 offset = (*arguments)[2];
5608 break;
5609 case EbtSampler2DShadow:
5610 case EbtSampler2DArrayShadow:
5611 offset = (*arguments)[3];
5612 break;
5613 default:
5614 UNREACHABLE();
5615 break;
5616 }
5617 useTextureGatherOffsetConstraints = true;
5618 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005619 if (offset != nullptr)
5620 {
5621 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5622 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5623 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005624 error(functionCall->getLine(), "Texture offset must be a constant expression",
5625 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005626 }
5627 else
5628 {
5629 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5630 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005631 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005632 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5633 : mMinProgramTexelOffset;
5634 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5635 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005636 for (size_t i = 0u; i < size; ++i)
5637 {
5638 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005639 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005640 {
5641 std::stringstream tokenStream;
5642 tokenStream << offsetValue;
5643 std::string token = tokenStream.str();
5644 error(offset->getLine(), "Texture offset value out of valid range",
5645 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005646 }
5647 }
5648 }
5649 }
5650}
5651
Jiajia Qina3106c52017-11-03 09:39:39 +08005652void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5653{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005654 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005655 const TFunction *func = functionCall->getFunction();
5656 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005657 {
5658 TIntermSequence *arguments = functionCall->getSequence();
5659 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5660
5661 if (IsBufferOrSharedVariable(memNode))
5662 {
5663 return;
5664 }
5665
5666 while (memNode->getAsBinaryNode())
5667 {
5668 memNode = memNode->getAsBinaryNode()->getLeft();
5669 if (IsBufferOrSharedVariable(memNode))
5670 {
5671 return;
5672 }
5673 }
5674
5675 error(memNode->getLine(),
5676 "The value passed to the mem argument of an atomic memory function does not "
5677 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005678 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005679 }
5680}
5681
Martin Radev2cc85b32016-08-05 16:22:53 +03005682// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5683void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5684{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005685 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005686
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005687 const TFunction *func = functionCall->getFunction();
5688
5689 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005690 {
5691 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005692 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005693
Olli Etuaho485eefd2017-02-14 17:40:06 +00005694 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005695
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005696 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005697 {
5698 if (memoryQualifier.readonly)
5699 {
5700 error(imageNode->getLine(),
5701 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005702 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005703 }
5704 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005705 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005706 {
5707 if (memoryQualifier.writeonly)
5708 {
5709 error(imageNode->getLine(),
5710 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005711 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005712 }
5713 }
5714 }
5715}
5716
5717// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5718void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5719 const TFunction *functionDefinition,
5720 const TIntermAggregate *functionCall)
5721{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005722 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005723
5724 const TIntermSequence &arguments = *functionCall->getSequence();
5725
5726 ASSERT(functionDefinition->getParamCount() == arguments.size());
5727
5728 for (size_t i = 0; i < arguments.size(); ++i)
5729 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005730 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5731 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005732 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005733 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5734
5735 if (IsImage(functionArgumentType.getBasicType()))
5736 {
5737 const TMemoryQualifier &functionArgumentMemoryQualifier =
5738 functionArgumentType.getMemoryQualifier();
5739 const TMemoryQualifier &functionParameterMemoryQualifier =
5740 functionParameterType.getMemoryQualifier();
5741 if (functionArgumentMemoryQualifier.readonly &&
5742 !functionParameterMemoryQualifier.readonly)
5743 {
5744 error(functionCall->getLine(),
5745 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005746 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005747 }
5748
5749 if (functionArgumentMemoryQualifier.writeonly &&
5750 !functionParameterMemoryQualifier.writeonly)
5751 {
5752 error(functionCall->getLine(),
5753 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005754 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005755 }
Martin Radev049edfa2016-11-11 14:35:37 +02005756
5757 if (functionArgumentMemoryQualifier.coherent &&
5758 !functionParameterMemoryQualifier.coherent)
5759 {
5760 error(functionCall->getLine(),
5761 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005762 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005763 }
5764
5765 if (functionArgumentMemoryQualifier.volatileQualifier &&
5766 !functionParameterMemoryQualifier.volatileQualifier)
5767 {
5768 error(functionCall->getLine(),
5769 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005770 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005771 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005772 }
5773 }
5774}
5775
Olli Etuaho95ed1942018-02-01 14:01:19 +02005776TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005777{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005778 if (fnCall->thisNode() != nullptr)
5779 {
5780 return addMethod(fnCall, loc);
5781 }
5782 if (fnCall->isConstructor())
5783 {
5784 return addConstructor(fnCall, loc);
5785 }
5786 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005787}
5788
Olli Etuaho95ed1942018-02-01 14:01:19 +02005789TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005790{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005791 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005792 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5793 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5794 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005795 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005796 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005797 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005798 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005800 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 {
5802 error(loc, "method takes no parameters", "length");
5803 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005804 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805 {
5806 error(loc, "length can only be called on arrays", "length");
5807 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005808 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005809 mGeometryShaderInputPrimitiveType == EptUndefined)
5810 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005811 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005812 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5813 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 else
5815 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005816 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005817 node->setLine(loc);
5818 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005820 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005821}
5822
Olli Etuaho95ed1942018-02-01 14:01:19 +02005823TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005824 const TSourceLoc &loc)
5825{
Olli Etuaho697bf652018-02-16 11:50:54 +02005826 // First check whether the function has been hidden by a variable name or struct typename by
5827 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5828 // with a matching argument list.
5829 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005830 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005831 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 }
5833 else
5834 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005835 // There are no inner functions, so it's enough to look for user-defined functions in the
5836 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005837 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005838 if (symbol != nullptr)
5839 {
5840 // A user-defined function - could be an overloaded built-in as well.
5841 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5842 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5843 TIntermAggregate *callNode =
5844 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5845 callNode->setLine(loc);
5846 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5847 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5848 return callNode;
5849 }
5850
5851 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005852 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005854 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 }
5856 else
5857 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005858 // A built-in function.
5859 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005860 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005861
Olli Etuaho37b697e2018-01-29 12:19:27 +02005862 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005863 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005864 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005866 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005867 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005868 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005869 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 if (fnCandidate->getParamCount() == 1)
5871 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005872 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005873 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005874 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005875 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005876 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005877 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005878 TIntermAggregate *callNode =
5879 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005880 callNode->setLine(loc);
5881
Olli Etuahoe80825e2018-02-16 10:24:53 +02005882 // Some built-in functions have out parameters too.
5883 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5884
5885 // See if we can constant fold a built-in. Note that this may be possible
5886 // even if it is not const-qualified.
5887 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005888 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005889
5890 // This is a built-in function with no op associated with it.
5891 TIntermAggregate *callNode =
5892 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5893 callNode->setLine(loc);
5894 checkTextureOffsetConst(callNode);
5895 checkTextureGather(callNode);
5896 checkImageMemoryAccessForBuiltinFunctions(callNode);
5897 checkAtomicMemoryBuiltinFunctions(callNode);
5898 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5899 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005901 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005902
5903 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005904 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905}
5906
Jamie Madillb98c3a82015-07-23 14:26:04 -04005907TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005908 TIntermTyped *trueExpression,
5909 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005910 const TSourceLoc &loc)
5911{
Olli Etuaho56229f12017-07-10 14:16:33 +03005912 if (!checkIsScalarBool(loc, cond))
5913 {
5914 return falseExpression;
5915 }
Olli Etuaho52901742015-04-15 13:42:45 +03005916
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005917 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005918 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005919 std::stringstream reasonStream;
5920 reasonStream << "mismatching ternary operator operand types '"
5921 << trueExpression->getCompleteString() << " and '"
5922 << falseExpression->getCompleteString() << "'";
5923 std::string reason = reasonStream.str();
5924 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005925 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005926 }
Olli Etuahode318b22016-10-25 16:18:25 +01005927 if (IsOpaqueType(trueExpression->getBasicType()))
5928 {
5929 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005930 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005931 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5932 // Note that structs containing opaque types don't need to be checked as structs are
5933 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005935 return falseExpression;
5936 }
5937
Jiajia Qinbc585152017-06-23 15:42:17 +08005938 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5939 falseExpression->getMemoryQualifier().writeonly)
5940 {
5941 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5942 return falseExpression;
5943 }
5944
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005945 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005946 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005947 // ESSL 3.00.6 section 5.7:
5948 // Ternary operator support is optional for arrays. No certainty that it works across all
5949 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5950 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005951 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005952 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005954 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005955 }
Olli Etuaho94050052017-05-08 14:17:44 +03005956 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5957 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005958 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005959 return falseExpression;
5960 }
5961
Corentin Wallez0d959252016-07-12 17:26:32 -04005962 // WebGL2 section 5.26, the following results in an error:
5963 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005964 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005965 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005966 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005967 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005968 }
5969
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005970 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005971 markStaticReadIfSymbol(cond);
5972 markStaticReadIfSymbol(trueExpression);
5973 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005974 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005975 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005976}
Olli Etuaho49300862015-02-20 14:54:49 +02005977
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005978//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005979// Parse an array of strings using yyparse.
5980//
5981// Returns 0 for success.
5982//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005983int PaParseStrings(size_t count,
5984 const char *const string[],
5985 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305986 TParseContext *context)
5987{
Yunchao He4f285442017-04-21 12:15:49 +08005988 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005989 return 1;
5990
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005991 if (glslang_initialize(context))
5992 return 1;
5993
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005994 int error = glslang_scan(count, string, length, context);
5995 if (!error)
5996 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005997
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005998 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005999
alokp@chromium.org6b495712012-06-29 00:06:58 +00006000 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006001}
Jamie Madill45bcc782016-11-07 13:58:48 -05006002
6003} // namespace sh