blob: f8df1368df0c1454e1adfe89aa497ca06adf2554 [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),
Geoff Lang197d5292018-04-25 14:29:00 -0400197 mPreprocessor(mDiagnostics, &mDirectiveHandler, angle::pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500198 mScanner(nullptr),
Jamie Madillacb4b812016-11-07 13:50:29 -0500199 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
200 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300201 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
202 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500203 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500204 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000205 mNumViews(-1),
206 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000207 mMaxImageUnits(resources.MaxImageUnits),
208 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000209 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800210 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800211 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800212 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800213 mDeclaringFunction(false),
214 mGeometryShaderInputPrimitiveType(EptUndefined),
215 mGeometryShaderOutputPrimitiveType(EptUndefined),
216 mGeometryShaderInvocations(0),
217 mGeometryShaderMaxVertices(-1),
218 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Olli Etuaho94bbed12018-03-20 14:44:53 +0200219 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices)
Jamie Madillacb4b812016-11-07 13:50:29 -0500220{
Jamie Madillacb4b812016-11-07 13:50:29 -0500221}
222
jchen104cdac9e2017-05-08 11:01:20 +0800223TParseContext::~TParseContext()
224{
225}
226
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300227bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200228 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300230 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300232 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200233 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300234 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530235 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200236 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000237 return false;
238 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300239 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000240
Jamie Madillb98c3a82015-07-23 14:26:04 -0400241 enum
242 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000243 exyzw,
244 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000245 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000246 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300248 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530249 {
250 switch (compString[i])
251 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400252 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400254 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400255 break;
256 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400258 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 break;
260 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 0;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400262 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400263 break;
264 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300265 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400266 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400267 break;
268 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300269 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400270 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 break;
272 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300273 (*fieldOffsets)[i] = 1;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400274 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400275 break;
276 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300277 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400278 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 break;
280 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300281 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400282 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 break;
284 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300285 (*fieldOffsets)[i] = 2;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400286 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530288
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300290 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400291 fieldSet[i] = exyzw;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 break;
293 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300294 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400295 fieldSet[i] = ergba;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 break;
297 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300298 (*fieldOffsets)[i] = 3;
Jamie Madill50cf2be2018-06-15 09:46:57 -0400299 fieldSet[i] = estpq;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 break;
301 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200302 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400303 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000304 }
305 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300307 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530308 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200311 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 return false;
313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314
Arun Patole7e7e68d2015-05-22 12:02:25 +0530315 if (i > 0)
316 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400317 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530318 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200319 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000320 return false;
321 }
322 }
323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326}
327
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328///////////////////////////////////////////////////////////////////////
329//
330// Errors
331//
332////////////////////////////////////////////////////////////////////////
333
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334//
335// Used by flex/bison to output all syntax and parsing errors.
336//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000337void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000339 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340}
341
Olli Etuahofbb1c792018-01-19 16:26:59 +0200342void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
343{
344 mDiagnostics->error(loc, reason, token.data());
345}
346
Olli Etuaho4de340a2016-12-16 09:32:03 +0000347void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530348{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000349 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000350}
351
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200352void TParseContext::outOfRangeError(bool isError,
353 const TSourceLoc &loc,
354 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000355 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200356{
357 if (isError)
358 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000359 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200360 }
361 else
362 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000363 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200364 }
365}
366
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367//
368// Same error message for all places assignments don't work.
369//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530370void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000371{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000372 std::stringstream reasonStream;
373 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
374 std::string reason = reasonStream.str();
375 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376}
377
378//
379// Same error message for all places unary operations don't work.
380//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530381void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000383 std::stringstream reasonStream;
384 reasonStream << "wrong operand type - no operation '" << op
385 << "' exists that takes an operand of type " << operand
386 << " (or there is no acceptable conversion)";
387 std::string reason = reasonStream.str();
388 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389}
390
391//
392// Same error message for all binary operations don't work.
393//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400394void TParseContext::binaryOpError(const TSourceLoc &line,
395 const char *op,
396 TString left,
397 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000399 std::stringstream reasonStream;
400 reasonStream << "wrong operand types - no operation '" << op
401 << "' exists that takes a left-hand operand of type '" << left
402 << "' and a right operand of type '" << right
403 << "' (or there is no acceptable conversion)";
404 std::string reason = reasonStream.str();
405 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
Olli Etuaho856c4972016-08-08 11:38:39 +0300408void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
409 TPrecision precision,
410 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530411{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400412 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300413 return;
Martin Radev70866b82016-07-22 15:27:42 +0300414
415 if (precision != EbpUndefined && !SupportsPrecision(type))
416 {
417 error(line, "illegal type for precision qualifier", getBasicString(type));
418 }
419
Olli Etuaho183d7e22015-11-20 15:59:09 +0200420 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530421 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200422 switch (type)
423 {
424 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400425 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300426 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200427 case EbtInt:
428 case EbtUInt:
429 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400430 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300431 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200432 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800433 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200434 {
jchen10cc2a10e2017-05-03 14:05:12 +0800435 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300436 return;
437 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200438 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000439 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000440}
441
Olli Etuaho94bbed12018-03-20 14:44:53 +0200442void TParseContext::markStaticReadIfSymbol(TIntermNode *node)
443{
444 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
445 if (swizzleNode)
446 {
447 markStaticReadIfSymbol(swizzleNode->getOperand());
448 return;
449 }
450 TIntermBinary *binaryNode = node->getAsBinaryNode();
451 if (binaryNode)
452 {
453 switch (binaryNode->getOp())
454 {
455 case EOpIndexDirect:
456 case EOpIndexIndirect:
457 case EOpIndexDirectStruct:
458 case EOpIndexDirectInterfaceBlock:
459 markStaticReadIfSymbol(binaryNode->getLeft());
460 return;
461 default:
462 return;
463 }
464 }
465 TIntermSymbol *symbolNode = node->getAsSymbolNode();
466 if (symbolNode)
467 {
468 symbolTable.markStaticRead(symbolNode->variable());
469 }
470}
471
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472// Both test and if necessary, spit out an error, to see if the node is really
473// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300474bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475{
Olli Etuahob6fa0432016-09-28 16:28:05 +0100476 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100477 if (swizzleNode)
478 {
479 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
480 if (ok && swizzleNode->hasDuplicateOffsets())
481 {
482 error(line, " l-value of swizzle cannot have duplicate components", op);
483 return false;
484 }
485 return ok;
486 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487
Olli Etuahodaf120b2018-03-20 14:21:10 +0200488 TIntermBinary *binaryNode = node->getAsBinaryNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530489 if (binaryNode)
490 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400491 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400493 case EOpIndexDirect:
494 case EOpIndexIndirect:
495 case EOpIndexDirectStruct:
496 case EOpIndexDirectInterfaceBlock:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800497 if (node->getMemoryQualifier().readonly)
498 {
499 error(line, "can't modify a readonly variable", op);
500 return false;
501 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300502 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400503 default:
504 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000506 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300507 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509
jchen10cc2a10e2017-05-03 14:05:12 +0800510 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530511 switch (node->getQualifier())
512 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400513 case EvqConst:
514 message = "can't modify a const";
515 break;
516 case EvqConstReadOnly:
517 message = "can't modify a const";
518 break;
519 case EvqAttribute:
520 message = "can't modify an attribute";
521 break;
522 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400523 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800524 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800525 case EvqFlatIn:
526 case EvqSmoothIn:
527 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400528 message = "can't modify an input";
529 break;
530 case EvqUniform:
531 message = "can't modify a uniform";
532 break;
533 case EvqVaryingIn:
534 message = "can't modify a varying";
535 break;
536 case EvqFragCoord:
537 message = "can't modify gl_FragCoord";
538 break;
539 case EvqFrontFacing:
540 message = "can't modify gl_FrontFacing";
541 break;
542 case EvqPointCoord:
543 message = "can't modify gl_PointCoord";
544 break;
Martin Radevb0883602016-08-04 17:48:58 +0300545 case EvqNumWorkGroups:
546 message = "can't modify gl_NumWorkGroups";
547 break;
548 case EvqWorkGroupSize:
549 message = "can't modify gl_WorkGroupSize";
550 break;
551 case EvqWorkGroupID:
552 message = "can't modify gl_WorkGroupID";
553 break;
554 case EvqLocalInvocationID:
555 message = "can't modify gl_LocalInvocationID";
556 break;
557 case EvqGlobalInvocationID:
558 message = "can't modify gl_GlobalInvocationID";
559 break;
560 case EvqLocalInvocationIndex:
561 message = "can't modify gl_LocalInvocationIndex";
562 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300563 case EvqViewIDOVR:
564 message = "can't modify gl_ViewID_OVR";
565 break;
Martin Radev802abe02016-08-04 17:48:32 +0300566 case EvqComputeIn:
567 message = "can't modify work group size variable";
568 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800569 case EvqPerVertexIn:
570 message = "can't modify any member in gl_in";
571 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800572 case EvqPrimitiveIDIn:
573 message = "can't modify gl_PrimitiveIDIn";
574 break;
575 case EvqInvocationID:
576 message = "can't modify gl_InvocationID";
577 break;
578 case EvqPrimitiveID:
579 if (mShaderType == GL_FRAGMENT_SHADER)
580 {
581 message = "can't modify gl_PrimitiveID in a fragment shader";
582 }
583 break;
584 case EvqLayer:
585 if (mShaderType == GL_FRAGMENT_SHADER)
586 {
587 message = "can't modify gl_Layer in a fragment shader";
588 }
589 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400590 default:
591 //
592 // Type that can't be written to?
593 //
594 if (node->getBasicType() == EbtVoid)
595 {
596 message = "can't modify void";
597 }
jchen10cc2a10e2017-05-03 14:05:12 +0800598 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400599 {
jchen10cc2a10e2017-05-03 14:05:12 +0800600 message = "can't modify a variable with type ";
601 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300602 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800603 else if (node->getMemoryQualifier().readonly)
604 {
605 message = "can't modify a readonly variable";
606 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608
Olli Etuahodaf120b2018-03-20 14:21:10 +0200609 ASSERT(binaryNode == nullptr && swizzleNode == nullptr);
610 TIntermSymbol *symNode = node->getAsSymbolNode();
611 if (message.empty() && symNode != nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200613 symbolTable.markStaticWrite(symNode->variable());
Olli Etuaho8a176262016-08-16 14:23:01 +0300614 return true;
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000615 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200616
617 std::stringstream reasonStream;
618 reasonStream << "l-value required";
619 if (!message.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530620 {
Olli Etuahodaf120b2018-03-20 14:21:10 +0200621 if (symNode)
622 {
623 // Symbol inside an expression can't be nameless.
624 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
625 const ImmutableString &symbol = symNode->getName();
626 reasonStream << " (" << message << " \"" << symbol << "\")";
627 }
628 else
629 {
630 reasonStream << " (" << message << ")";
631 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 }
Olli Etuahodaf120b2018-03-20 14:21:10 +0200633 std::string reason = reasonStream.str();
634 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
Olli Etuaho8a176262016-08-16 14:23:01 +0300636 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639// Both test, and if necessary spit out an error, to see if the node is really
640// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300641void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Olli Etuaho383b7912016-08-05 11:22:59 +0300643 if (node->getQualifier() != EvqConst)
644 {
645 error(node->getLine(), "constant expression required", "");
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649// Both test, and if necessary spit out an error, to see if the node is really
650// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300651void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho383b7912016-08-05 11:22:59 +0300653 if (!node->isScalarInt())
654 {
655 error(node->getLine(), "integer expression required", token);
656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657}
658
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659// Both test, and if necessary spit out an error, to see if we are currently
660// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800661bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Olli Etuaho856c4972016-08-08 11:38:39 +0300663 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300664 {
665 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800666 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300667 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800668 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300671// ESSL 3.00.5 sections 3.8 and 3.9.
672// If it starts "gl_" or contains two consecutive underscores, it's reserved.
673// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200674bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200677 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300679 error(line, reservedErrMsg, "gl_");
680 return false;
681 }
682 if (sh::IsWebGLBasedSpec(mShaderSpec))
683 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200684 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530685 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300686 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300687 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200689 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530690 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300691 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300692 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 }
694 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200695 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300696 {
697 error(line,
698 "identifiers containing two consecutive underscores (__) are reserved as "
699 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200700 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300701 return false;
702 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300703 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704}
705
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300707bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200708 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200711 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530712 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200713 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300714 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000715 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200716
Olli Etuaho95ed1942018-02-01 14:01:19 +0200717 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530718 {
Olli Etuaho94bbed12018-03-20 14:44:53 +0200719 markStaticReadIfSymbol(arg);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300720 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200721 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300722 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 {
jchen10cc2a10e2017-05-03 14:05:12 +0800724 std::string reason("cannot convert a variable with type ");
725 reason += getBasicString(argTyped->getBasicType());
726 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300727 return false;
728 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800729 else if (argTyped->getMemoryQualifier().writeonly)
730 {
731 error(line, "cannot convert a variable with writeonly", "constructor");
732 return false;
733 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200734 if (argTyped->getBasicType() == EbtVoid)
735 {
736 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300737 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200738 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000739 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
Olli Etuaho856c4972016-08-08 11:38:39 +0300741 if (type.isArray())
742 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 // The size of an unsized constructor should already have been determined.
744 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200745 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 {
747 error(line, "array constructor needs one argument per array element", "constructor");
748 return false;
749 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
751 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200752 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 {
754 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300755 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500756 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300757 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500758 return false;
759 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300760 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300761 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000762 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300763 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300764 }
765 }
766 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300767 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 {
769 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200770 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300771 {
772 error(line,
773 "Number of constructor parameters does not match the number of structure fields",
774 "constructor");
775 return false;
776 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300777
778 for (size_t i = 0; i < fields.size(); i++)
779 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200780 if (i >= arguments.size() ||
781 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300782 {
783 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000784 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300785 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300786 }
787 }
788 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300789 else
790 {
791 // We're constructing a scalar, vector, or matrix.
792
793 // Note: It's okay to have too many components available, but not okay to have unused
794 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
795 // there is an extra argument, so 'overFull' will become true.
796
797 size_t size = 0;
798 bool full = false;
799 bool overFull = false;
800 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200801 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300802 {
803 const TIntermTyped *argTyped = arg->getAsTyped();
804 ASSERT(argTyped != nullptr);
805
Olli Etuaho487b63a2017-05-23 15:55:09 +0300806 if (argTyped->getBasicType() == EbtStruct)
807 {
808 error(line, "a struct cannot be used as a constructor argument for this type",
809 "constructor");
810 return false;
811 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300812 if (argTyped->getType().isArray())
813 {
814 error(line, "constructing from a non-dereferenced array", "constructor");
815 return false;
816 }
817 if (argTyped->getType().isMatrix())
818 {
819 matrixArg = true;
820 }
821
822 size += argTyped->getType().getObjectSize();
823 if (full)
824 {
825 overFull = true;
826 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300827 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300828 {
829 full = true;
830 }
831 }
832
833 if (type.isMatrix() && matrixArg)
834 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200835 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300836 {
837 error(line, "constructing matrix from matrix can only take one argument",
838 "constructor");
839 return false;
840 }
841 }
842 else
843 {
844 if (size != 1 && size < type.getObjectSize())
845 {
846 error(line, "not enough data provided for construction", "constructor");
847 return false;
848 }
849 if (overFull)
850 {
851 error(line, "too many arguments", "constructor");
852 return false;
853 }
854 }
855 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300856
Olli Etuaho8a176262016-08-16 14:23:01 +0300857 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
Jamie Madillb98c3a82015-07-23 14:26:04 -0400860// This function checks to see if a void variable has been declared and raise an error message for
861// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862//
863// returns true in case of an error
864//
Olli Etuaho856c4972016-08-08 11:38:39 +0300865bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200866 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400867 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300869 if (type == EbtVoid)
870 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200871 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300872 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
Olli Etuaho8a176262016-08-16 14:23:01 +0300875 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876}
877
Jamie Madillb98c3a82015-07-23 14:26:04 -0400878// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300879// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300880bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300882 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000884 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300885 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300887 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888}
889
Jamie Madillb98c3a82015-07-23 14:26:04 -0400890// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300891// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300892void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893{
Martin Radev4a9cd802016-09-01 16:51:51 +0300894 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530895 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000896 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530897 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
jchen10cc2a10e2017-05-03 14:05:12 +0800900bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
901 const TTypeSpecifierNonArray &pType,
902 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 if (pType.type == EbtStruct)
905 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300906 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530907 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000908 std::stringstream reasonStream;
909 reasonStream << reason << " (structure contains a sampler)";
910 std::string reasonStr = reasonStream.str();
911 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300912 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 }
jchen10cc2a10e2017-05-03 14:05:12 +0800914 // only samplers need to be checked from structs, since other opaque types can't be struct
915 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300916 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530917 }
jchen10cc2a10e2017-05-03 14:05:12 +0800918 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530919 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000920 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300921 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925}
926
Olli Etuaho856c4972016-08-08 11:38:39 +0300927void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
928 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400929{
930 if (pType.layoutQualifier.location != -1)
931 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400932 error(line, "location must only be specified for a single input or output variable",
933 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400934 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400935}
936
Olli Etuaho856c4972016-08-08 11:38:39 +0300937void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
938 const TLayoutQualifier &layoutQualifier)
939{
940 if (layoutQualifier.location != -1)
941 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000942 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
943 if (mShaderVersion >= 310)
944 {
945 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800946 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000947 }
948 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300949 }
950}
951
Qin Jiajiaca68d982017-09-18 16:41:56 +0800952void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
953 const TLayoutBlockStorage &blockStorage,
954 const TQualifier &qualifier)
955{
956 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
957 {
958 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
959 }
960}
961
Martin Radev2cc85b32016-08-05 16:22:53 +0300962void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
963 TQualifier qualifier,
964 const TType &type)
965{
Martin Radev2cc85b32016-08-05 16:22:53 +0300966 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800967 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530968 {
jchen10cc2a10e2017-05-03 14:05:12 +0800969 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300974unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530976 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000977
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200978 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
979 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
980 // fold as array size.
981 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000983 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
Olli Etuaho856c4972016-08-08 11:38:39 +0300987 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400988
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000989 if (constant->getBasicType() == EbtUInt)
990 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300991 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000992 }
993 else
994 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000996
Olli Etuaho856c4972016-08-08 11:38:39 +0300997 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000998 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400999 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001000 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001001 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001002
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001004 }
1005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 {
1008 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001010 }
1011
1012 // The size of arrays is restricted here to prevent issues further down the
1013 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1014 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1015 const unsigned int sizeLimit = 65536;
1016
Olli Etuaho856c4972016-08-08 11:38:39 +03001017 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001018 {
1019 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001020 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001021 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001022
1023 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024}
1025
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001027bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1028 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029{
Olli Etuaho8a176262016-08-16 14:23:01 +03001030 if ((elementQualifier.qualifier == EvqAttribute) ||
1031 (elementQualifier.qualifier == EvqVertexIn) ||
1032 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001035 TType(elementQualifier).getQualifierString());
1036 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
Olli Etuaho8a176262016-08-16 14:23:01 +03001039 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040}
1041
Olli Etuaho8a176262016-08-16 14:23:01 +03001042// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001043bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1044 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001046 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001047 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001048 error(line, "cannot declare arrays of arrays",
1049 TType(elementType).getCompleteString().c_str());
1050 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001051 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001052 return true;
1053}
1054
1055// Check if this qualified element type can be formed into an array. This is only called when array
1056// brackets are associated with an identifier in a declaration, like this:
1057// float a[2];
1058// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1059// are associated with the type, like this:
1060// float[2] a;
1061bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1062 const TPublicType &elementType)
1063{
1064 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1065 {
1066 return false;
1067 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001068 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1069 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1070 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001071 // Geometry shader requires each user-defined input be declared as arrays or inside input
1072 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1073 // interface matching, such variables and blocks are treated as though they were not declared
1074 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001075 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001076 sh::IsVarying(elementType.qualifier) &&
1077 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001078 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001079 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001080 TType(elementType).getCompleteString().c_str());
1081 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001082 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001083 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084}
1085
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001087void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001088 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001089 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090{
Olli Etuaho3739d232015-04-08 12:23:44 +03001091 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001093 {
1094 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001095 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001096
1097 // Generate informative error messages for ESSL1.
1098 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001099 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001100 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102 "structures containing arrays may not be declared constant since they cannot be "
1103 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001104 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001105 }
1106 else
1107 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001108 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001109 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001110 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001111 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001112 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1113 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114}
1115
Olli Etuaho2935c582015-04-08 14:32:06 +03001116// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117// and update the symbol table.
1118//
Olli Etuaho2935c582015-04-08 14:32:06 +03001119// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001121bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001122 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001123 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001124 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125{
Olli Etuaho2935c582015-04-08 14:32:06 +03001126 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127
Olli Etuahofbb1c792018-01-19 16:26:59 +02001128 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001129
Olli Etuahob60d30f2018-01-16 12:31:06 +02001130 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001131
Olli Etuaho856c4972016-08-08 11:38:39 +03001132 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001133
Olli Etuaho2935c582015-04-08 14:32:06 +03001134 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001135 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001137 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001138 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001139 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001140 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001141 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001142 return false;
1143 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001144 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001145 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001147 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001149 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 }
1151 }
1152 else
1153 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001154 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001155 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 return false;
1157 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159
Olli Etuaho8a176262016-08-16 14:23:01 +03001160 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001161 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
Olli Etuaho437664b2018-02-28 15:38:14 +02001163 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001164 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001165 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001166 return false;
1167 }
1168
Olli Etuahob60d30f2018-01-16 12:31:06 +02001169 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001170 return false;
1171
1172 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173}
1174
Martin Radev70866b82016-07-22 15:27:42 +03001175void TParseContext::checkIsParameterQualifierValid(
1176 const TSourceLoc &line,
1177 const TTypeQualifierBuilder &typeQualifierBuilder,
1178 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301179{
Olli Etuahocce89652017-06-19 16:04:09 +03001180 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001181 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001182
1183 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301184 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001185 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1186 }
1187
1188 if (!IsImage(type->getBasicType()))
1189 {
Olli Etuaho43364892017-02-13 16:00:12 +00001190 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001191 }
1192 else
1193 {
1194 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196
Martin Radev70866b82016-07-22 15:27:42 +03001197 type->setQualifier(typeQualifier.qualifier);
1198
1199 if (typeQualifier.precision != EbpUndefined)
1200 {
1201 type->setPrecision(typeQualifier.precision);
1202 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001203}
1204
Olli Etuaho703671e2017-11-08 17:47:18 +02001205template <size_t size>
1206bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1207 const std::array<TExtension, size> &extensions)
1208{
1209 ASSERT(!extensions.empty());
1210 const TExtensionBehavior &extBehavior = extensionBehavior();
1211
1212 bool canUseWithWarning = false;
1213 bool canUseWithoutWarning = false;
1214
1215 const char *errorMsgString = "";
1216 TExtension errorMsgExtension = TExtension::UNDEFINED;
1217
1218 for (TExtension extension : extensions)
1219 {
1220 auto extIter = extBehavior.find(extension);
1221 if (canUseWithWarning)
1222 {
1223 // We already have an extension that we can use, but with a warning.
1224 // See if we can use the alternative extension without a warning.
1225 if (extIter == extBehavior.end())
1226 {
1227 continue;
1228 }
1229 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1230 {
1231 canUseWithoutWarning = true;
1232 break;
1233 }
1234 continue;
1235 }
1236 if (extIter == extBehavior.end())
1237 {
1238 errorMsgString = "extension is not supported";
1239 errorMsgExtension = extension;
1240 }
1241 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1242 {
1243 errorMsgString = "extension is disabled";
1244 errorMsgExtension = extension;
1245 }
1246 else if (extIter->second == EBhWarn)
1247 {
1248 errorMsgExtension = extension;
1249 canUseWithWarning = true;
1250 }
1251 else
1252 {
1253 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1254 canUseWithoutWarning = true;
1255 break;
1256 }
1257 }
1258
1259 if (canUseWithoutWarning)
1260 {
1261 return true;
1262 }
1263 if (canUseWithWarning)
1264 {
1265 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1266 return true;
1267 }
1268 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1269 return false;
1270}
1271
1272template bool TParseContext::checkCanUseOneOfExtensions(
1273 const TSourceLoc &line,
1274 const std::array<TExtension, 1> &extensions);
1275template bool TParseContext::checkCanUseOneOfExtensions(
1276 const TSourceLoc &line,
1277 const std::array<TExtension, 2> &extensions);
1278template bool TParseContext::checkCanUseOneOfExtensions(
1279 const TSourceLoc &line,
1280 const std::array<TExtension, 3> &extensions);
1281
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001282bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001283{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001284 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001285 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286}
1287
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001288// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1289// compile-time or link-time errors are the same whether or not the declaration is empty".
1290// This function implements all the checks that are done on qualifiers regardless of if the
1291// declaration is empty.
1292void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1293 const sh::TLayoutQualifier &layoutQualifier,
1294 const TSourceLoc &location)
1295{
1296 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1297 {
1298 error(location, "Shared memory declarations cannot have layout specified", "layout");
1299 }
1300
1301 if (layoutQualifier.matrixPacking != EmpUnspecified)
1302 {
1303 error(location, "layout qualifier only valid for interface blocks",
1304 getMatrixPackingString(layoutQualifier.matrixPacking));
1305 return;
1306 }
1307
1308 if (layoutQualifier.blockStorage != EbsUnspecified)
1309 {
1310 error(location, "layout qualifier only valid for interface blocks",
1311 getBlockStorageString(layoutQualifier.blockStorage));
1312 return;
1313 }
1314
1315 if (qualifier == EvqFragmentOut)
1316 {
1317 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1318 {
1319 error(location, "invalid layout qualifier combination", "yuv");
1320 return;
1321 }
1322 }
1323 else
1324 {
1325 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1326 }
1327
Olli Etuaho95468d12017-05-04 11:14:34 +03001328 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1329 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001330 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1331 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001332 {
1333 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1334 }
1335
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001336 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001337 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001338 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001339 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001340 // We're not checking whether the uniform location is in range here since that depends on
1341 // the type of the variable.
1342 // The type can only be fully determined for non-empty declarations.
1343 }
1344 if (!canHaveLocation)
1345 {
1346 checkLocationIsNotSpecified(location, layoutQualifier);
1347 }
1348}
1349
jchen104cdac9e2017-05-08 11:01:20 +08001350void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1351 const TSourceLoc &location)
1352{
1353 if (publicType.precision != EbpHigh)
1354 {
1355 error(location, "Can only be highp", "atomic counter");
1356 }
1357 // dEQP enforces compile error if location is specified. See uniform_location.test.
1358 if (publicType.layoutQualifier.location != -1)
1359 {
1360 error(location, "location must not be set for atomic_uint", "layout");
1361 }
1362 if (publicType.layoutQualifier.binding == -1)
1363 {
1364 error(location, "no binding specified", "atomic counter");
1365 }
1366}
1367
Olli Etuaho55bde912017-10-25 13:41:13 +03001368void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001369{
Olli Etuaho55bde912017-10-25 13:41:13 +03001370 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001371 {
1372 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1373 // error. It is assumed that this applies to empty declarations as well.
1374 error(location, "empty array declaration needs to specify a size", "");
1375 }
Martin Radevb8b01222016-11-20 23:25:53 +02001376}
1377
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001378// These checks are done for all declarations that are non-empty. They're done for non-empty
1379// declarations starting a declarator list, and declarators that follow an empty declaration.
1380void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1381 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001382{
Olli Etuahofa33d582015-04-09 14:33:12 +03001383 switch (publicType.qualifier)
1384 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001385 case EvqVaryingIn:
1386 case EvqVaryingOut:
1387 case EvqAttribute:
1388 case EvqVertexIn:
1389 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001390 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001391 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001392 {
1393 error(identifierLocation, "cannot be used with a structure",
1394 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001395 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001396 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001397 break;
1398 case EvqBuffer:
1399 if (publicType.getBasicType() != EbtInterfaceBlock)
1400 {
1401 error(identifierLocation,
1402 "cannot declare buffer variables at global scope(outside a block)",
1403 getQualifierString(publicType.qualifier));
1404 return;
1405 }
1406 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001407 default:
1408 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001409 }
jchen10cc2a10e2017-05-03 14:05:12 +08001410 std::string reason(getBasicString(publicType.getBasicType()));
1411 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001412 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001413 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001414 {
1415 return;
1416 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001417
Andrei Volykhina5527072017-03-22 16:46:30 +03001418 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1419 publicType.qualifier != EvqConst) &&
1420 publicType.getBasicType() == EbtYuvCscStandardEXT)
1421 {
1422 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1423 getQualifierString(publicType.qualifier));
1424 return;
1425 }
1426
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001427 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1428 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001429 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1430 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001431 TType type(publicType);
1432 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001433 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001434 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1435 publicType.layoutQualifier);
1436 }
1437 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001439 // check for layout qualifier issues
1440 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001441
Martin Radev2cc85b32016-08-05 16:22:53 +03001442 if (IsImage(publicType.getBasicType()))
1443 {
1444
1445 switch (layoutQualifier.imageInternalFormat)
1446 {
1447 case EiifRGBA32F:
1448 case EiifRGBA16F:
1449 case EiifR32F:
1450 case EiifRGBA8:
1451 case EiifRGBA8_SNORM:
1452 if (!IsFloatImage(publicType.getBasicType()))
1453 {
1454 error(identifierLocation,
1455 "internal image format requires a floating image type",
1456 getBasicString(publicType.getBasicType()));
1457 return;
1458 }
1459 break;
1460 case EiifRGBA32I:
1461 case EiifRGBA16I:
1462 case EiifRGBA8I:
1463 case EiifR32I:
1464 if (!IsIntegerImage(publicType.getBasicType()))
1465 {
1466 error(identifierLocation,
1467 "internal image format requires an integer image type",
1468 getBasicString(publicType.getBasicType()));
1469 return;
1470 }
1471 break;
1472 case EiifRGBA32UI:
1473 case EiifRGBA16UI:
1474 case EiifRGBA8UI:
1475 case EiifR32UI:
1476 if (!IsUnsignedImage(publicType.getBasicType()))
1477 {
1478 error(identifierLocation,
1479 "internal image format requires an unsigned image type",
1480 getBasicString(publicType.getBasicType()));
1481 return;
1482 }
1483 break;
1484 case EiifUnspecified:
1485 error(identifierLocation, "layout qualifier", "No image internal format specified");
1486 return;
1487 default:
1488 error(identifierLocation, "layout qualifier", "unrecognized token");
1489 return;
1490 }
1491
1492 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1493 switch (layoutQualifier.imageInternalFormat)
1494 {
1495 case EiifR32F:
1496 case EiifR32I:
1497 case EiifR32UI:
1498 break;
1499 default:
1500 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1501 {
1502 error(identifierLocation, "layout qualifier",
1503 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1504 "image variables must be qualified readonly and/or writeonly");
1505 return;
1506 }
1507 break;
1508 }
1509 }
1510 else
1511 {
Olli Etuaho43364892017-02-13 16:00:12 +00001512 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001513 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1514 }
jchen104cdac9e2017-05-08 11:01:20 +08001515
1516 if (IsAtomicCounter(publicType.getBasicType()))
1517 {
1518 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1519 }
1520 else
1521 {
1522 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1523 }
Olli Etuaho43364892017-02-13 16:00:12 +00001524}
Martin Radev2cc85b32016-08-05 16:22:53 +03001525
Olli Etuaho43364892017-02-13 16:00:12 +00001526void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1527{
1528 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001529 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1530 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1531 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1532 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1533 // when it comes to which shaders are accepted by the compiler.
1534 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001535 if (IsImage(type.getBasicType()))
1536 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001537 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1538 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001539 }
1540 else if (IsSampler(type.getBasicType()))
1541 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001542 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1543 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001544 }
jchen104cdac9e2017-05-08 11:01:20 +08001545 else if (IsAtomicCounter(type.getBasicType()))
1546 {
1547 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1548 }
Olli Etuaho43364892017-02-13 16:00:12 +00001549 else
1550 {
1551 ASSERT(!IsOpaqueType(type.getBasicType()));
1552 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001553 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001554}
1555
Olli Etuaho856c4972016-08-08 11:38:39 +03001556void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001557 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001558 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001559{
1560
1561 if (mShaderVersion < versionRequired)
1562 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001563 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001564 }
1565}
1566
Olli Etuaho856c4972016-08-08 11:38:39 +03001567bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1568 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001569{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001570 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001571 for (size_t i = 0u; i < localSize.size(); ++i)
1572 {
1573 if (localSize[i] != -1)
1574 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001575 error(location,
1576 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1577 "global layout declaration",
1578 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001579 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001580 }
1581 }
1582
Olli Etuaho8a176262016-08-16 14:23:01 +03001583 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001584}
1585
Olli Etuaho43364892017-02-13 16:00:12 +00001586void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001587 TLayoutImageInternalFormat internalFormat)
1588{
1589 if (internalFormat != EiifUnspecified)
1590 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001591 error(location, "invalid layout qualifier: only valid when used with images",
1592 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001593 }
Olli Etuaho43364892017-02-13 16:00:12 +00001594}
1595
1596void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1597{
1598 if (binding != -1)
1599 {
1600 error(location,
1601 "invalid layout qualifier: only valid when used with opaque types or blocks",
1602 "binding");
1603 }
1604}
1605
jchen104cdac9e2017-05-08 11:01:20 +08001606void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1607{
1608 if (offset != -1)
1609 {
1610 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1611 "offset");
1612 }
1613}
1614
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001615void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1616 int binding,
1617 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001618{
1619 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001620 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001621 {
1622 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1623 }
1624}
1625
1626void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1627 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001628 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001629{
1630 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001631 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001632 {
1633 error(location, "sampler binding greater than maximum texture units", "binding");
1634 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001635}
1636
Jiajia Qinbc585152017-06-23 15:42:17 +08001637void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1638 const TQualifier &qualifier,
1639 int binding,
1640 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001641{
1642 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001643 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001644 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001645 if (binding + size > mMaxUniformBufferBindings)
1646 {
1647 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1648 "binding");
1649 }
1650 }
1651 else if (qualifier == EvqBuffer)
1652 {
1653 if (binding + size > mMaxShaderStorageBufferBindings)
1654 {
1655 error(location,
1656 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1657 "binding");
1658 }
jchen10af713a22017-04-19 09:10:56 +08001659 }
1660}
jchen104cdac9e2017-05-08 11:01:20 +08001661void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1662{
1663 if (binding >= mMaxAtomicCounterBindings)
1664 {
1665 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1666 "binding");
1667 }
1668}
jchen10af713a22017-04-19 09:10:56 +08001669
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001670void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1671 int objectLocationCount,
1672 const TLayoutQualifier &layoutQualifier)
1673{
1674 int loc = layoutQualifier.location;
1675 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1676 {
1677 error(location, "Uniform location out of range", "location");
1678 }
1679}
1680
Andrei Volykhina5527072017-03-22 16:46:30 +03001681void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1682{
1683 if (yuv != false)
1684 {
1685 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1686 }
1687}
1688
Jiajia Qinbc585152017-06-23 15:42:17 +08001689void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1690 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001691{
1692 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1693 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001694 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001695 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001696 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1697 qual == EvqConstReadOnly);
1698 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001699 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001700 markStaticReadIfSymbol(argument);
1701 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001702 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001703 if (argument->getMemoryQualifier().writeonly)
1704 {
1705 error(argument->getLine(),
1706 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1707 fnCall->functionName());
1708 return;
1709 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001710 }
1711 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001712 if (qual == EvqOut || qual == EvqInOut)
1713 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001714 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001715 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001716 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001717 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001718 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001719 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001720 }
1721 }
1722 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001723}
1724
Martin Radev70866b82016-07-22 15:27:42 +03001725void TParseContext::checkInvariantVariableQualifier(bool invariant,
1726 const TQualifier qualifier,
1727 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001728{
Martin Radev70866b82016-07-22 15:27:42 +03001729 if (!invariant)
1730 return;
1731
1732 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001733 {
Martin Radev70866b82016-07-22 15:27:42 +03001734 // input variables in the fragment shader can be also qualified as invariant
1735 if (!sh::CanBeInvariantESSL1(qualifier))
1736 {
1737 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1738 }
1739 }
1740 else
1741 {
1742 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1743 {
1744 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1745 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001746 }
1747}
1748
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001749bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001750{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001751 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001752}
1753
Jamie Madillb98c3a82015-07-23 14:26:04 -04001754void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1755 const char *extName,
1756 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001757{
Geoff Lang197d5292018-04-25 14:29:00 -04001758 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001759 srcLoc.file = loc.first_file;
1760 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001761 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001762}
1763
Jamie Madillb98c3a82015-07-23 14:26:04 -04001764void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1765 const char *name,
1766 const char *value,
1767 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001768{
Geoff Lang197d5292018-04-25 14:29:00 -04001769 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001770 srcLoc.file = loc.first_file;
1771 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001772 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001773}
1774
Martin Radev4c4c8e72016-08-04 12:25:34 +03001775sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001776{
Jamie Madill2f294c92017-11-20 14:47:26 -05001777 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001778 for (size_t i = 0u; i < result.size(); ++i)
1779 {
1780 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1781 {
1782 result[i] = 1;
1783 }
1784 else
1785 {
1786 result[i] = mComputeShaderLocalSize[i];
1787 }
1788 }
1789 return result;
1790}
1791
Olli Etuaho56229f12017-07-10 14:16:33 +03001792TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1793 const TSourceLoc &line)
1794{
1795 TIntermConstantUnion *node = new TIntermConstantUnion(
1796 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1797 node->setLine(line);
1798 return node;
1799}
1800
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801/////////////////////////////////////////////////////////////////////////////////
1802//
1803// Non-Errors.
1804//
1805/////////////////////////////////////////////////////////////////////////////////
1806
Jamie Madill5c097022014-08-20 16:38:32 -04001807const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001808 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001809 const TSymbol *symbol)
1810{
Jamie Madill5c097022014-08-20 16:38:32 -04001811 if (!symbol)
1812 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001813 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001814 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001815 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001816
1817 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001818 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001819 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001820 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001821 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001822
1823 const TVariable *variable = static_cast<const TVariable *>(symbol);
1824
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001825 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001826 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001827 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001828 }
1829
Olli Etuaho0f684632017-07-13 12:42:15 +03001830 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1831 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
Olli Etuaho59c5b892018-04-03 11:44:50 +03001832 variable->getType().getQualifier() == EvqWorkGroupSize)
Olli Etuaho0f684632017-07-13 12:42:15 +03001833 {
1834 error(location,
1835 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1836 "gl_WorkGroupSize");
1837 }
Jamie Madill5c097022014-08-20 16:38:32 -04001838 return variable;
1839}
1840
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001841TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001842 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001843 const TSymbol *symbol)
1844{
1845 const TVariable *variable = getNamedVariable(location, name, symbol);
1846
Olli Etuaho0f684632017-07-13 12:42:15 +03001847 if (!variable)
1848 {
1849 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1850 node->setLine(location);
1851 return node;
1852 }
1853
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001854 const TType &variableType = variable->getType();
Jamie Madill50cf2be2018-06-15 09:46:57 -04001855 TIntermTyped *node = nullptr;
Olli Etuaho56229f12017-07-10 14:16:33 +03001856
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001857 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001858 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001859 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001860 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001861 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001862 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001863 {
1864 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1865 // needs to be added to the AST as a constant and not as a symbol.
1866 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1867 TConstantUnion *constArray = new TConstantUnion[3];
1868 for (size_t i = 0; i < 3; ++i)
1869 {
1870 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1871 }
1872
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001873 ASSERT(variableType.getBasicType() == EbtUInt);
1874 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001875
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001877 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001878 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001879 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001880 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1881 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001882 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001883 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1884 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001885 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001886 else
1887 {
Olli Etuaho195be942017-12-04 23:40:14 +02001888 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001889 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001890 ASSERT(node != nullptr);
1891 node->setLine(location);
1892 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001893}
1894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895// Initializers show up in several places in the grammar. Have one set of
1896// code to handle them here.
1897//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001898// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001899bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001900 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001901 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001902 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001903 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904{
Olli Etuaho13389b62016-10-16 11:48:18 +01001905 ASSERT(initNode != nullptr);
1906 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907
Olli Etuahob60d30f2018-01-16 12:31:06 +02001908 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001909 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001910 // In case initializer is not an array or type has more dimensions than initializer, this
1911 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1912 // actually is an array or not. Having a non-array initializer for an unsized array will
1913 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001914 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001915 type->sizeUnsizedArrays(arraySizes);
1916 }
1917
1918 const TQualifier qualifier = type->getQualifier();
1919
1920 bool constError = false;
1921 if (qualifier == EvqConst)
1922 {
1923 if (EvqConst != initializer->getType().getQualifier())
1924 {
1925 std::stringstream reasonStream;
1926 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1927 std::string reason = reasonStream.str();
1928 error(line, reason.c_str(), "=");
1929
1930 // We're still going to declare the variable to avoid extra error messages.
1931 type->setQualifier(EvqTemporary);
1932 constError = true;
1933 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001934 }
Olli Etuaho195be942017-12-04 23:40:14 +02001935
1936 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001937 if (!declareVariable(line, identifier, type, &variable))
1938 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001939 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941
Olli Etuahob60d30f2018-01-16 12:31:06 +02001942 if (constError)
1943 {
1944 return false;
1945 }
1946
Olli Etuahob0c645e2015-05-12 14:25:36 +03001947 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001948 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001949 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001950 {
1951 // Error message does not completely match behavior with ESSL 1.00, but
1952 // we want to steer developers towards only using constant expressions.
1953 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001954 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001955 }
1956 if (globalInitWarning)
1957 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001958 warning(
1959 line,
1960 "global variable initializers should be constant expressions "
1961 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1962 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001963 }
1964
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301966 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1967 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001968 error(line, " cannot initialize this type of qualifier ",
1969 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001970 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001971 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001972
1973 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1974 intermSymbol->setLine(line);
1975
1976 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1977 {
1978 assignError(line, "=", variable->getType().getCompleteString(),
1979 initializer->getCompleteString());
1980 return false;
1981 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982
Arun Patole7e7e68d2015-05-22 12:02:25 +05301983 if (qualifier == EvqConst)
1984 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001985 // Save the constant folded value to the variable if possible.
1986 const TConstantUnion *constArray = initializer->getConstantValue();
1987 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301988 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001989 variable->shareConstPointer(constArray);
1990 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001991 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 ASSERT(*initNode == nullptr);
1993 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001994 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001995 }
1996 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001997
Olli Etuahob60d30f2018-01-16 12:31:06 +02001998 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02001999 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002000 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002001 return true;
2002}
2003
2004TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002005 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002006 TIntermTyped *initializer,
2007 const TSourceLoc &loc)
2008{
2009 checkIsScalarBool(loc, pType);
2010 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002011 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002012 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002013 {
2014 // The initializer is valid. The init condition needs to have a node - either the
2015 // initializer node, or a constant node in case the initialized variable is const and won't
2016 // be recorded in the AST.
2017 if (initNode == nullptr)
2018 {
2019 return initializer;
2020 }
2021 else
2022 {
2023 TIntermDeclaration *declaration = new TIntermDeclaration();
2024 declaration->appendDeclarator(initNode);
2025 return declaration;
2026 }
2027 }
2028 return nullptr;
2029}
2030
2031TIntermNode *TParseContext::addLoop(TLoopType type,
2032 TIntermNode *init,
2033 TIntermNode *cond,
2034 TIntermTyped *expr,
2035 TIntermNode *body,
2036 const TSourceLoc &line)
2037{
2038 TIntermNode *node = nullptr;
2039 TIntermTyped *typedCond = nullptr;
2040 if (cond)
2041 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002042 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002043 typedCond = cond->getAsTyped();
2044 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002045 if (expr)
2046 {
2047 markStaticReadIfSymbol(expr);
2048 }
2049 // In case the loop body was not parsed as a block and contains a statement that simply refers
2050 // to a variable, we need to mark it as statically used.
2051 if (body)
2052 {
2053 markStaticReadIfSymbol(body);
2054 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002055 if (cond == nullptr || typedCond)
2056 {
Olli Etuahocce89652017-06-19 16:04:09 +03002057 if (type == ELoopDoWhile)
2058 {
2059 checkIsScalarBool(line, typedCond);
2060 }
2061 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2062 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2063 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2064 !typedCond->isVector()));
2065
Olli Etuaho3ec75682017-07-05 17:02:55 +03002066 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002067 node->setLine(line);
2068 return node;
2069 }
2070
Olli Etuahocce89652017-06-19 16:04:09 +03002071 ASSERT(type != ELoopDoWhile);
2072
Olli Etuaho914b79a2017-06-19 16:03:19 +03002073 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2074 ASSERT(declaration);
2075 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2076 ASSERT(declarator->getLeft()->getAsSymbolNode());
2077
2078 // The condition is a declaration. In the AST representation we don't support declarations as
2079 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2080 // the loop.
2081 TIntermBlock *block = new TIntermBlock();
2082
2083 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2084 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2085 block->appendStatement(declareCondition);
2086
2087 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2088 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002089 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002090 block->appendStatement(loop);
2091 loop->setLine(line);
2092 block->setLine(line);
2093 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094}
2095
Olli Etuahocce89652017-06-19 16:04:09 +03002096TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2097 TIntermNodePair code,
2098 const TSourceLoc &loc)
2099{
Olli Etuaho56229f12017-07-10 14:16:33 +03002100 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002101 // In case the conditional statements were not parsed as blocks and contain a statement that
2102 // simply refers to a variable, we need to mark them as statically used.
2103 if (code.node1)
2104 {
2105 markStaticReadIfSymbol(code.node1);
2106 }
2107 if (code.node2)
2108 {
2109 markStaticReadIfSymbol(code.node2);
2110 }
Olli Etuahocce89652017-06-19 16:04:09 +03002111
2112 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002113 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002114 {
2115 if (cond->getAsConstantUnion()->getBConst(0) == true)
2116 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002117 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002118 }
2119 else
2120 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002121 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002122 }
2123 }
2124
Olli Etuaho3ec75682017-07-05 17:02:55 +03002125 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002126 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002127 node->setLine(loc);
2128
2129 return node;
2130}
2131
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002132void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2133{
2134 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2135 typeSpecifier->getBasicType());
2136
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002137 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002138 {
2139 error(typeSpecifier->getLine(), "not supported", "first-class array");
2140 typeSpecifier->clearArrayness();
2141 }
2142}
2143
Martin Radev70866b82016-07-22 15:27:42 +03002144TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302145 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002146{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002147 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002148
Martin Radev70866b82016-07-22 15:27:42 +03002149 TPublicType returnType = typeSpecifier;
2150 returnType.qualifier = typeQualifier.qualifier;
2151 returnType.invariant = typeQualifier.invariant;
2152 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002153 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002154 returnType.precision = typeSpecifier.precision;
2155
2156 if (typeQualifier.precision != EbpUndefined)
2157 {
2158 returnType.precision = typeQualifier.precision;
2159 }
2160
Martin Radev4a9cd802016-09-01 16:51:51 +03002161 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2162 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002163
Martin Radev4a9cd802016-09-01 16:51:51 +03002164 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2165 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002166
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002168
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002169 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002170 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002171 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002172 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002174 returnType.clearArrayness();
2175 }
2176
Martin Radev70866b82016-07-22 15:27:42 +03002177 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002179 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002181 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002182 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002183
Martin Radev70866b82016-07-22 15:27:42 +03002184 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002187 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002188 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002189 }
2190 }
2191 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002192 {
Martin Radev70866b82016-07-22 15:27:42 +03002193 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002194 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002195 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002196 }
Martin Radev70866b82016-07-22 15:27:42 +03002197 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2198 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002199 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002200 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2201 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002202 }
Martin Radev70866b82016-07-22 15:27:42 +03002203 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002204 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002205 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002206 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002207 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002208 }
2209
2210 return returnType;
2211}
2212
Olli Etuaho856c4972016-08-08 11:38:39 +03002213void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2214 const TPublicType &type,
2215 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002216{
2217 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002218 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002219 {
2220 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002221 }
2222
2223 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2224 switch (qualifier)
2225 {
2226 case EvqVertexIn:
2227 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002228 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002229 {
2230 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002231 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002232 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002233 return;
2234 case EvqFragmentOut:
2235 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002236 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 {
2238 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002239 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002240 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002241 return;
2242 default:
2243 break;
2244 }
2245
2246 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2247 // restrictions.
2248 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002249 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2250 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2252 {
2253 error(qualifierLocation, "must use 'flat' interpolation here",
2254 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 }
2256
Martin Radev4a9cd802016-09-01 16:51:51 +03002257 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002258 {
2259 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2260 // These restrictions are only implied by the ESSL 3.00 spec, but
2261 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002262 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 {
2264 error(qualifierLocation, "cannot be an array of structures",
2265 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002266 }
2267 if (type.isStructureContainingArrays())
2268 {
2269 error(qualifierLocation, "cannot be a structure containing an array",
2270 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002271 }
2272 if (type.isStructureContainingType(EbtStruct))
2273 {
2274 error(qualifierLocation, "cannot be a structure containing a structure",
2275 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002276 }
2277 if (type.isStructureContainingType(EbtBool))
2278 {
2279 error(qualifierLocation, "cannot be a structure containing a bool",
2280 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002281 }
2282 }
2283}
2284
Martin Radev2cc85b32016-08-05 16:22:53 +03002285void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2286{
2287 if (qualifier.getType() == QtStorage)
2288 {
2289 const TStorageQualifierWrapper &storageQualifier =
2290 static_cast<const TStorageQualifierWrapper &>(qualifier);
2291 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2292 !symbolTable.atGlobalLevel())
2293 {
2294 error(storageQualifier.getLine(),
2295 "Local variables can only use the const storage qualifier.",
2296 storageQualifier.getQualifierString().c_str());
2297 }
2298 }
2299}
2300
Olli Etuaho43364892017-02-13 16:00:12 +00002301void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002302 const TSourceLoc &location)
2303{
Jiajia Qinbc585152017-06-23 15:42:17 +08002304 const std::string reason(
2305 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2306 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002307 if (memoryQualifier.readonly)
2308 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002309 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002310 }
2311 if (memoryQualifier.writeonly)
2312 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002313 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002314 }
Martin Radev049edfa2016-11-11 14:35:37 +02002315 if (memoryQualifier.coherent)
2316 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002317 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002318 }
2319 if (memoryQualifier.restrictQualifier)
2320 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002321 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002322 }
2323 if (memoryQualifier.volatileQualifier)
2324 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002325 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002326 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002327}
2328
jchen104cdac9e2017-05-08 11:01:20 +08002329// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2330// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002331void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2332 const TSourceLoc &loc,
2333 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002334{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002335 if (!IsAtomicCounter(type->getBasicType()))
2336 {
2337 return;
2338 }
2339
2340 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2341 : kAtomicCounterSize;
2342 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2343 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002344 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002345 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002346 {
2347 offset = bindingState.appendSpan(size);
2348 }
2349 else
2350 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002351 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002352 }
2353 if (offset == -1)
2354 {
2355 error(loc, "Offset overlapping", "atomic counter");
2356 return;
2357 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002358 layoutQualifier.offset = offset;
2359 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002360}
2361
Olli Etuaho454c34c2017-10-25 16:35:56 +03002362void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002363 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002364 TType *type)
2365{
2366 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2367 {
2368 if (type->isArray() && type->getOutermostArraySize() == 0u)
2369 {
2370 // Set size for the unsized geometry shader inputs if they are declared after a valid
2371 // input primitive declaration.
2372 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2373 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002374 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002375 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002376 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002377 }
2378 else
2379 {
2380 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2381 // An input can be declared without an array size if there is a previous layout
2382 // which specifies the size.
2383 error(location,
2384 "Missing a valid input primitive declaration before declaring an unsized "
2385 "array input",
2386 token);
2387 }
2388 }
2389 else if (type->isArray())
2390 {
2391 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2392 }
2393 else
2394 {
2395 error(location, "Geometry shader input variable must be declared as an array", token);
2396 }
2397 }
2398}
2399
Olli Etuaho13389b62016-10-16 11:48:18 +01002400TIntermDeclaration *TParseContext::parseSingleDeclaration(
2401 TPublicType &publicType,
2402 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002403 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002404{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002405 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002406 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2407 mDirectiveHandler.pragma().stdgl.invariantAll)
2408 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002409 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002410
2411 // The directive handler has already taken care of rejecting invalid uses of this pragma
2412 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2413 // affected variable declarations:
2414 //
2415 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2416 // elsewhere, in TranslatorGLSL.)
2417 //
2418 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2419 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2420 // the way this is currently implemented we have to enable this compiler option before
2421 // parsing the shader and determining the shading language version it uses. If this were
2422 // implemented as a post-pass, the workaround could be more targeted.
2423 //
2424 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2425 // the specification, but there are desktop OpenGL drivers that expect that this is the
2426 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2427 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2428 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002429 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002430 }
2431 }
2432
Olli Etuahofbb1c792018-01-19 16:26:59 +02002433 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002434
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2436 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002437
Jamie Madill50cf2be2018-06-15 09:46:57 -04002438 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002440
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002441 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002442 if (emptyDeclaration)
2443 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002444 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 // In most cases we don't need to create a symbol node for an empty declaration.
2446 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002447 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 {
Olli Etuaho195be942017-12-04 23:40:14 +02002449 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002450 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002451 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002452 }
jchen104cdac9e2017-05-08 11:01:20 +08002453 else if (IsAtomicCounter(publicType.getBasicType()))
2454 {
2455 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2456 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002457 }
2458 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002459 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002460 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002461
Olli Etuahob60d30f2018-01-16 12:31:06 +02002462 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuahob60d30f2018-01-16 12:31:06 +02002464 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002465
Olli Etuaho2935c582015-04-08 14:32:06 +03002466 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002467 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 {
Olli Etuaho195be942017-12-04 23:40:14 +02002469 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002470 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002471 }
2472
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002473 TIntermDeclaration *declaration = new TIntermDeclaration();
2474 declaration->setLine(identifierOrTypeLocation);
2475 if (symbol)
2476 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002477 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002478 declaration->appendDeclarator(symbol);
2479 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002480 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002481}
2482
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002483TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2484 TPublicType &elementType,
2485 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002486 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002487 const TSourceLoc &indexLocation,
2488 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002489{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002491
Olli Etuaho55bde912017-10-25 13:41:13 +03002492 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002493 identifierLocation);
2494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuahob60d30f2018-01-16 12:31:06 +02002499 TType *arrayType = new TType(elementType);
2500 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002501
Olli Etuahofbb1c792018-01-19 16:26:59 +02002502 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002503
Olli Etuahob60d30f2018-01-16 12:31:06 +02002504 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002505
Olli Etuahob60d30f2018-01-16 12:31:06 +02002506 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002507
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 TIntermDeclaration *declaration = new TIntermDeclaration();
2509 declaration->setLine(identifierLocation);
2510
Olli Etuaho195be942017-12-04 23:40:14 +02002511 TVariable *variable = nullptr;
2512 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002513 {
Olli Etuaho195be942017-12-04 23:40:14 +02002514 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002515 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 declaration->appendDeclarator(symbol);
2517 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002518
Olli Etuaho13389b62016-10-16 11:48:18 +01002519 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002520}
2521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2523 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002524 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002525 const TSourceLoc &initLocation,
2526 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002527{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002528 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002529
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002530 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2531 identifierLocation);
2532
2533 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002534
Olli Etuaho13389b62016-10-16 11:48:18 +01002535 TIntermDeclaration *declaration = new TIntermDeclaration();
2536 declaration->setLine(identifierLocation);
2537
2538 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002539 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002540 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002541 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002542 if (initNode)
2543 {
2544 declaration->appendDeclarator(initNode);
2545 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002546 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002547 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002548}
2549
Olli Etuaho13389b62016-10-16 11:48:18 +01002550TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002551 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002552 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002553 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002554 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002555 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002556 const TSourceLoc &initLocation,
2557 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002559 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuaho55bde912017-10-25 13:41:13 +03002561 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002562 identifierLocation);
2563
Olli Etuaho55bde912017-10-25 13:41:13 +03002564 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567
Olli Etuahob60d30f2018-01-16 12:31:06 +02002568 TType *arrayType = new TType(elementType);
2569 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 TIntermDeclaration *declaration = new TIntermDeclaration();
2572 declaration->setLine(identifierLocation);
2573
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002574 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002575 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002576 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002577 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 if (initNode)
2579 {
2580 declaration->appendDeclarator(initNode);
2581 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002582 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002583
2584 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002585}
2586
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002587TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002588 const TTypeQualifierBuilder &typeQualifierBuilder,
2589 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002590 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002591 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002592{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002593 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002594
Martin Radev70866b82016-07-22 15:27:42 +03002595 if (!typeQualifier.invariant)
2596 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002597 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002598 return nullptr;
2599 }
2600 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2601 {
2602 return nullptr;
2603 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002604 if (!symbol)
2605 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002606 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002607 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 }
Martin Radev70866b82016-07-22 15:27:42 +03002609 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002610 {
Martin Radev70866b82016-07-22 15:27:42 +03002611 error(identifierLoc, "invariant declaration specifies qualifier",
2612 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002613 }
Martin Radev70866b82016-07-22 15:27:42 +03002614 if (typeQualifier.precision != EbpUndefined)
2615 {
2616 error(identifierLoc, "invariant declaration specifies precision",
2617 getPrecisionString(typeQualifier.precision));
2618 }
2619 if (!typeQualifier.layoutQualifier.isEmpty())
2620 {
2621 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2622 }
2623
2624 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002625 if (!variable)
2626 {
2627 return nullptr;
2628 }
Martin Radev70866b82016-07-22 15:27:42 +03002629 const TType &type = variable->getType();
2630
2631 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2632 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002633 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002634
Olli Etuaho76b2c382018-03-19 15:51:29 +02002635 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002636
Olli Etuaho195be942017-12-04 23:40:14 +02002637 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002638 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002639
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002640 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002641}
2642
Olli Etuaho13389b62016-10-16 11:48:18 +01002643void TParseContext::parseDeclarator(TPublicType &publicType,
2644 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002645 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002646 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002647{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002648 // If the declaration starting this declarator list was empty (example: int,), some checks were
2649 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002651 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002652 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2653 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002654 }
2655
Olli Etuaho856c4972016-08-08 11:38:39 +03002656 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002657
Olli Etuahob60d30f2018-01-16 12:31:06 +02002658 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002659
Olli Etuahofbb1c792018-01-19 16:26:59 +02002660 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002661
Olli Etuahob60d30f2018-01-16 12:31:06 +02002662 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002663
Olli Etuahob60d30f2018-01-16 12:31:06 +02002664 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002665
Olli Etuaho195be942017-12-04 23:40:14 +02002666 TVariable *variable = nullptr;
2667 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002668 {
Olli Etuaho195be942017-12-04 23:40:14 +02002669 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002670 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 declarationOut->appendDeclarator(symbol);
2672 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002673}
2674
Olli Etuaho55bde912017-10-25 13:41:13 +03002675void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002676 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002677 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002679 const TVector<unsigned int> &arraySizes,
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 Etuaho55bde912017-10-25 13:41:13 +03002686 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002687 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002691
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002693 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002694 TType *arrayType = new TType(elementType);
2695 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002696
Olli Etuahofbb1c792018-01-19 16:26:59 +02002697 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002698
Olli Etuahob60d30f2018-01-16 12:31:06 +02002699 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002700
Olli Etuahob60d30f2018-01-16 12:31:06 +02002701 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002702
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002703 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002704 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002705 {
Olli Etuaho195be942017-12-04 23:40:14 +02002706 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002707 symbol->setLine(identifierLocation);
2708 declarationOut->appendDeclarator(symbol);
2709 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002710 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002711}
2712
Olli Etuaho13389b62016-10-16 11:48:18 +01002713void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2714 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002715 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002716 const TSourceLoc &initLocation,
2717 TIntermTyped *initializer,
2718 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002719{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002720 // If the declaration starting this declarator list was empty (example: int,), some checks were
2721 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002722 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002723 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002724 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2725 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002726 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002727
Olli Etuaho856c4972016-08-08 11:38:39 +03002728 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002729
Olli Etuaho13389b62016-10-16 11:48:18 +01002730 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002731 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002732 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 {
2734 //
2735 // build the intermediate representation
2736 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002737 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002738 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002739 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002740 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002741 }
2742}
2743
Olli Etuaho55bde912017-10-25 13:41:13 +03002744void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002745 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002746 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002748 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002749 const TSourceLoc &initLocation,
2750 TIntermTyped *initializer,
2751 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002752{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002753 // If the declaration starting this declarator list was empty (example: int,), some checks were
2754 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002755 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002756 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002757 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759 }
2760
Olli Etuaho55bde912017-10-25 13:41:13 +03002761 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
Olli Etuaho55bde912017-10-25 13:41:13 +03002763 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764
Olli Etuahob60d30f2018-01-16 12:31:06 +02002765 TType *arrayType = new TType(elementType);
2766 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767
2768 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002769 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002770 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002771 {
2772 if (initNode)
2773 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002774 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002775 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002776 }
2777}
2778
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002779TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2780{
2781 // It's simpler to parse an empty statement as a constant expression rather than having a
2782 // different type of node just for empty statements, that will be pruned from the AST anyway.
2783 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2784 node->setLine(location);
2785 return node;
2786}
2787
jchen104cdac9e2017-05-08 11:01:20 +08002788void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2789 const TSourceLoc &location)
2790{
2791 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2792 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2793 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2794 {
2795 error(location, "Requires both binding and offset", "layout");
2796 return;
2797 }
2798 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2799}
2800
Olli Etuahocce89652017-06-19 16:04:09 +03002801void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2802 const TPublicType &type,
2803 const TSourceLoc &loc)
2804{
2805 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2806 !getFragmentPrecisionHigh())
2807 {
2808 error(loc, "precision is not supported in fragment shader", "highp");
2809 }
2810
2811 if (!CanSetDefaultPrecisionOnType(type))
2812 {
2813 error(loc, "illegal type argument for default precision qualifier",
2814 getBasicString(type.getBasicType()));
2815 return;
2816 }
2817 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2818}
2819
Shaob5cc1192017-07-06 10:47:20 +08002820bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2821{
2822 switch (typeQualifier.layoutQualifier.primitiveType)
2823 {
2824 case EptLines:
2825 case EptLinesAdjacency:
2826 case EptTriangles:
2827 case EptTrianglesAdjacency:
2828 return typeQualifier.qualifier == EvqGeometryIn;
2829
2830 case EptLineStrip:
2831 case EptTriangleStrip:
2832 return typeQualifier.qualifier == EvqGeometryOut;
2833
2834 case EptPoints:
2835 return true;
2836
2837 default:
2838 UNREACHABLE();
2839 return false;
2840 }
2841}
2842
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002843void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2844 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002845{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002846 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002847 {
2848 error(line,
2849 "Array size or input primitive declaration doesn't match the size of earlier sized "
2850 "array inputs.",
2851 "layout");
2852 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002853}
2854
Shaob5cc1192017-07-06 10:47:20 +08002855bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2856{
2857 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2858
2859 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2860
2861 if (layoutQualifier.maxVertices != -1)
2862 {
2863 error(typeQualifier.line,
2864 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2865 return false;
2866 }
2867
2868 // Set mGeometryInputPrimitiveType if exists
2869 if (layoutQualifier.primitiveType != EptUndefined)
2870 {
2871 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2872 {
2873 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2874 return false;
2875 }
2876
2877 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2878 {
2879 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002880 setGeometryShaderInputArraySize(
2881 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2882 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002883 }
2884 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2885 {
2886 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2887 "layout");
2888 return false;
2889 }
2890 }
2891
2892 // Set mGeometryInvocations if exists
2893 if (layoutQualifier.invocations > 0)
2894 {
2895 if (mGeometryShaderInvocations == 0)
2896 {
2897 mGeometryShaderInvocations = layoutQualifier.invocations;
2898 }
2899 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2900 {
2901 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2902 "layout");
2903 return false;
2904 }
2905 }
2906
2907 return true;
2908}
2909
2910bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2911{
2912 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2913
2914 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2915
2916 if (layoutQualifier.invocations > 0)
2917 {
2918 error(typeQualifier.line,
2919 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2920 return false;
2921 }
2922
2923 // Set mGeometryOutputPrimitiveType if exists
2924 if (layoutQualifier.primitiveType != EptUndefined)
2925 {
2926 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2927 {
2928 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2929 return false;
2930 }
2931
2932 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2933 {
2934 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2935 }
2936 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2937 {
2938 error(typeQualifier.line,
2939 "primitive doesn't match earlier output primitive declaration", "layout");
2940 return false;
2941 }
2942 }
2943
2944 // Set mGeometryMaxVertices if exists
2945 if (layoutQualifier.maxVertices > -1)
2946 {
2947 if (mGeometryShaderMaxVertices == -1)
2948 {
2949 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2950 }
2951 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2952 {
2953 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2954 "layout");
2955 return false;
2956 }
2957 }
2958
2959 return true;
2960}
2961
Martin Radev70866b82016-07-22 15:27:42 +03002962void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002963{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002964 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002965 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002966
Martin Radev70866b82016-07-22 15:27:42 +03002967 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2968 typeQualifier.line);
2969
Jamie Madillc2128ff2016-07-04 10:26:17 -04002970 // It should never be the case, but some strange parser errors can send us here.
2971 if (layoutQualifier.isEmpty())
2972 {
2973 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002974 return;
2975 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002976
Martin Radev802abe02016-08-04 17:48:32 +03002977 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002978 {
Olli Etuaho43364892017-02-13 16:00:12 +00002979 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 return;
2981 }
2982
Olli Etuaho43364892017-02-13 16:00:12 +00002983 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2984
2985 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002986
2987 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2988
Andrei Volykhina5527072017-03-22 16:46:30 +03002989 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2990
jchen104cdac9e2017-05-08 11:01:20 +08002991 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2992
Qin Jiajiaca68d982017-09-18 16:41:56 +08002993 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2994 typeQualifier.qualifier);
2995
Martin Radev802abe02016-08-04 17:48:32 +03002996 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002997 {
Martin Radev802abe02016-08-04 17:48:32 +03002998 if (mComputeShaderLocalSizeDeclared &&
2999 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3000 {
3001 error(typeQualifier.line, "Work group size does not match the previous declaration",
3002 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003003 return;
3004 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003005
Martin Radev802abe02016-08-04 17:48:32 +03003006 if (mShaderVersion < 310)
3007 {
3008 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003009 return;
3010 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003011
Martin Radev4c4c8e72016-08-04 12:25:34 +03003012 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003013 {
3014 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003015 return;
3016 }
3017
3018 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003019 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003020
3021 const TConstantUnion *maxComputeWorkGroupSizeData =
3022 maxComputeWorkGroupSize->getConstPointer();
3023
3024 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3025 {
3026 if (layoutQualifier.localSize[i] != -1)
3027 {
3028 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3029 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3030 if (mComputeShaderLocalSize[i] < 1 ||
3031 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3032 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003033 std::stringstream reasonStream;
3034 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3035 << maxComputeWorkGroupSizeValue;
3036 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003037
Olli Etuaho4de340a2016-12-16 09:32:03 +00003038 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003039 return;
3040 }
3041 }
3042 }
3043
3044 mComputeShaderLocalSizeDeclared = true;
3045 }
Shaob5cc1192017-07-06 10:47:20 +08003046 else if (typeQualifier.qualifier == EvqGeometryIn)
3047 {
3048 if (mShaderVersion < 310)
3049 {
3050 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3051 return;
3052 }
3053
3054 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3055 {
3056 return;
3057 }
3058 }
3059 else if (typeQualifier.qualifier == EvqGeometryOut)
3060 {
3061 if (mShaderVersion < 310)
3062 {
3063 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3064 "layout");
3065 return;
3066 }
3067
3068 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3069 {
3070 return;
3071 }
3072 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003073 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3074 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003075 {
3076 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3077 // specification.
3078 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3079 {
3080 error(typeQualifier.line, "Number of views does not match the previous declaration",
3081 "layout");
3082 return;
3083 }
3084
3085 if (layoutQualifier.numViews == -1)
3086 {
3087 error(typeQualifier.line, "No num_views specified", "layout");
3088 return;
3089 }
3090
3091 if (layoutQualifier.numViews > mMaxNumViews)
3092 {
3093 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3094 "layout");
3095 return;
3096 }
3097
3098 mNumViews = layoutQualifier.numViews;
3099 }
Martin Radev802abe02016-08-04 17:48:32 +03003100 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003101 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003102 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003103 {
Martin Radev802abe02016-08-04 17:48:32 +03003104 return;
3105 }
3106
Jiajia Qinbc585152017-06-23 15:42:17 +08003107 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003108 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003109 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003110 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003111 return;
3112 }
3113
3114 if (mShaderVersion < 300)
3115 {
3116 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3117 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003118 return;
3119 }
3120
Olli Etuaho09b04a22016-12-15 13:30:26 +00003121 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003122
3123 if (layoutQualifier.matrixPacking != EmpUnspecified)
3124 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003125 if (typeQualifier.qualifier == EvqUniform)
3126 {
3127 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3128 }
3129 else if (typeQualifier.qualifier == EvqBuffer)
3130 {
3131 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3132 }
Martin Radev802abe02016-08-04 17:48:32 +03003133 }
3134
3135 if (layoutQualifier.blockStorage != EbsUnspecified)
3136 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003137 if (typeQualifier.qualifier == EvqUniform)
3138 {
3139 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3140 }
3141 else if (typeQualifier.qualifier == EvqBuffer)
3142 {
3143 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3144 }
Martin Radev802abe02016-08-04 17:48:32 +03003145 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003146 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003147}
3148
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003149TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3150 const TFunction &function,
3151 const TSourceLoc &location,
3152 bool insertParametersToSymbolTable)
3153{
Olli Etuahobed35d72017-12-20 16:36:26 +02003154 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003155
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003156 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003157 prototype->setLine(location);
3158
3159 for (size_t i = 0; i < function.getParamCount(); i++)
3160 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003161 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003162
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3164 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003165 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003166 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003167 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003168 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003169 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003171 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003172 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003174 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003175 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003176 }
3177 else
3178 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003179 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003180 {
3181 error(location, "function parameter array must be sized at compile time", "[]");
3182 // We don't need to size the arrays since the parameter is unnamed and hence
3183 // inaccessible.
3184 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003185 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 }
3187 return prototype;
3188}
3189
Olli Etuaho16c745a2017-01-16 17:02:27 +00003190TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3191 const TFunction &parsedFunction,
3192 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003193{
Olli Etuaho476197f2016-10-11 13:59:08 +01003194 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3195 // first declaration. Either way the instance in the symbol table is used to track whether the
3196 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003197 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003198 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003199 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3200
3201 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003202 {
3203 // ESSL 1.00.17 section 4.2.7.
3204 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3205 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003206 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003207
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003208 TIntermFunctionPrototype *prototype =
3209 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003210
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003211 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003212
3213 if (!symbolTable.atGlobalLevel())
3214 {
3215 // ESSL 3.00.4 section 4.2.4.
3216 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003217 }
3218
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003219 return prototype;
3220}
3221
Olli Etuaho336b1472016-10-05 16:37:55 +01003222TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003223 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003224 TIntermBlock *functionBody,
3225 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003226{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003227 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3229 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003230 error(location,
3231 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232 }
3233
Olli Etuahof51fdd22016-10-03 10:03:40 +01003234 if (functionBody == nullptr)
3235 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003236 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003237 functionBody->setLine(location);
3238 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003239 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003240 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003241 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003242
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003243 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003244 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245}
3246
Olli Etuaho476197f2016-10-11 13:59:08 +01003247void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003248 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003249 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003250{
Olli Etuaho476197f2016-10-11 13:59:08 +01003251 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003252
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003253 bool wasDefined = false;
3254 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3255 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003256 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003257 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003258 }
Jamie Madill185fb402015-06-12 15:48:48 -04003259
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003260 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003261 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003263
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003264 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003265 setLoopNestingLevel(0);
3266}
3267
Jamie Madillb98c3a82015-07-23 14:26:04 -04003268TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003269{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003270 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003271 // We don't know at this point whether this is a function definition or a prototype.
3272 // The definition production code will check for redefinitions.
3273 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003274 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303275
Olli Etuahod80f2942017-11-06 12:44:45 +02003276 for (size_t i = 0u; i < function->getParamCount(); ++i)
3277 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003278 const TVariable *param = function->getParam(i);
3279 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003280 {
3281 // ESSL 3.00.6 section 12.10.
3282 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003283 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003284 }
3285 }
3286
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003287 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303288 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003289 const UnmangledBuiltIn *builtIn =
3290 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3291 if (builtIn &&
3292 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3293 {
3294 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3295 // functions. Therefore overloading or redefining builtin functions is an error.
3296 error(location, "Name of a built-in function cannot be redeclared as function",
3297 function->name());
3298 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303299 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003300 else
3301 {
3302 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3303 // this applies to redeclarations as well.
3304 const TSymbol *builtIn =
3305 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3306 if (builtIn)
3307 {
3308 error(location, "built-in functions cannot be redefined", function->name());
3309 }
3310 }
3311
3312 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3313 // here.
3314 const TFunction *prevDec =
3315 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3316 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003317 {
3318 if (prevDec->getReturnType() != function->getReturnType())
3319 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003320 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003321 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003322 }
3323 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3324 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003325 if (prevDec->getParam(i)->getType().getQualifier() !=
3326 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003327 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003328 error(location,
3329 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003330 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003331 }
3332 }
3333 }
3334
Jamie Madill185fb402015-06-12 15:48:48 -04003335 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003336 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3337 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003338 if (prevSym)
3339 {
3340 if (!prevSym->isFunction())
3341 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003342 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003343 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003344 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003345 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003346 // Parsing is at the inner scope level of the function's arguments and body statement at this
3347 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3348 // scope.
3349 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003350
Olli Etuaho78d13742017-01-18 13:06:10 +00003351 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003352 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003353 {
3354 if (function->getParamCount() > 0)
3355 {
3356 error(location, "function cannot take any parameter(s)", "main");
3357 }
3358 if (function->getReturnType().getBasicType() != EbtVoid)
3359 {
3360 error(location, "main function cannot return a value",
3361 function->getReturnType().getBasicString());
3362 }
3363 }
3364
Jamie Madill185fb402015-06-12 15:48:48 -04003365 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003366 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3367 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003368 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3369 //
3370 return function;
3371}
3372
Olli Etuaho9de84a52016-06-14 17:36:01 +03003373TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003374 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003375 const TSourceLoc &location)
3376{
3377 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3378 {
3379 error(location, "no qualifiers allowed for function return",
3380 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003381 }
3382 if (!type.layoutQualifier.isEmpty())
3383 {
3384 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003385 }
jchen10cc2a10e2017-05-03 14:05:12 +08003386 // make sure an opaque type is not involved as well...
3387 std::string reason(getBasicString(type.getBasicType()));
3388 reason += "s can't be function return values";
3389 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003390 if (mShaderVersion < 300)
3391 {
3392 // Array return values are forbidden, but there's also no valid syntax for declaring array
3393 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003394 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003395
3396 if (type.isStructureContainingArrays())
3397 {
3398 // ESSL 1.00.17 section 6.1 Function Definitions
3399 error(location, "structures containing arrays can't be function return values",
3400 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003401 }
3402 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003403
3404 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003405 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003406}
3407
Olli Etuaho697bf652018-02-16 11:50:54 +02003408TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3409 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003410{
Olli Etuaho697bf652018-02-16 11:50:54 +02003411 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003412}
3413
Olli Etuaho95ed1942018-02-01 14:01:19 +02003414TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003415{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003416 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003417 {
3418 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3419 "[]");
3420 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003421 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003422 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003423 error(publicType.getLine(), "constructor can't be a structure definition",
3424 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003425 }
3426
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003427 TType *type = new TType(publicType);
3428 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003429 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003430 error(publicType.getLine(), "cannot construct this type",
3431 getBasicString(publicType.getBasicType()));
3432 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003433 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003434 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003435}
3436
Olli Etuaho55bde912017-10-25 13:41:13 +03003437void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3438 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003439 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003440 TType *arrayType)
3441{
3442 if (arrayType->isUnsizedArray())
3443 {
3444 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003445 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003446 }
3447}
3448
3449TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003450 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003451 const TSourceLoc &nameLoc)
3452{
Olli Etuaho55bde912017-10-25 13:41:13 +03003453 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003454 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003455 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003456 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003457 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003458 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003459 checkIsNotReserved(nameLoc, name);
3460 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003461 return param;
3462}
3463
Olli Etuaho55bde912017-10-25 13:41:13 +03003464TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003465 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003466 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003467{
Olli Etuaho55bde912017-10-25 13:41:13 +03003468 TType *type = new TType(publicType);
3469 return parseParameterDeclarator(type, name, nameLoc);
3470}
3471
Olli Etuahofbb1c792018-01-19 16:26:59 +02003472TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003473 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003474 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003475 const TSourceLoc &arrayLoc,
3476 TPublicType *elementType)
3477{
3478 checkArrayElementIsNotArray(arrayLoc, *elementType);
3479 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003480 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003481 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003482}
3483
Olli Etuaho95ed1942018-02-01 14:01:19 +02003484bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3485 const TIntermSequence &arguments,
3486 TType type,
3487 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003488{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003489 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003490 {
3491 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3492 return false;
3493 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003494 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003495 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003496 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003497 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003498 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3499 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003500 {
3501 error(line, "constructing from a non-dereferenced array", "constructor");
3502 return false;
3503 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003504 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003505 {
3506 if (dimensionalityFromElement == 1u)
3507 {
3508 error(line, "implicitly sized array of arrays constructor argument is not an array",
3509 "constructor");
3510 }
3511 else
3512 {
3513 error(line,
3514 "implicitly sized array of arrays constructor argument dimensionality is too "
3515 "low",
3516 "constructor");
3517 }
3518 return false;
3519 }
3520 }
3521 return true;
3522}
3523
Jamie Madillb98c3a82015-07-23 14:26:04 -04003524// This function is used to test for the correctness of the parameters passed to various constructor
3525// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003526//
Olli Etuaho856c4972016-08-08 11:38:39 +03003527// 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 +00003528//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003529TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003530{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003531 TType type = fnCall->constructorType();
3532 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003533 if (type.isUnsizedArray())
3534 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003535 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003536 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003537 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003538 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003539 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003540 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003541 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003542 if (type.getOutermostArraySize() == 0u)
3543 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003544 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003545 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003546 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003547 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003548 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003549 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003550 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003551 }
3552 }
3553 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003554 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003555
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003556 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003557 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003558 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003559 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003560
Olli Etuaho95ed1942018-02-01 14:01:19 +02003561 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003562 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003563
Olli Etuaho765924f2018-01-04 12:48:36 +02003564 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003565}
3566
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003567//
3568// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003569// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003570//
Olli Etuaho13389b62016-10-16 11:48:18 +01003571TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003572 const TTypeQualifierBuilder &typeQualifierBuilder,
3573 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003574 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003575 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003576 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003577 const TSourceLoc &instanceLine,
3578 TIntermTyped *arrayIndex,
3579 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003580{
Olli Etuaho856c4972016-08-08 11:38:39 +03003581 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003582
Olli Etuaho77ba4082016-12-16 12:01:18 +00003583 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003584
Jiajia Qinbc585152017-06-23 15:42:17 +08003585 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003586 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003587 error(typeQualifier.line,
3588 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3589 "3.10",
3590 getQualifierString(typeQualifier.qualifier));
3591 }
3592 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3593 {
3594 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003595 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003596 }
3597
Martin Radev70866b82016-07-22 15:27:42 +03003598 if (typeQualifier.invariant)
3599 {
3600 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3601 }
3602
Jiajia Qinbc585152017-06-23 15:42:17 +08003603 if (typeQualifier.qualifier != EvqBuffer)
3604 {
3605 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3606 }
Olli Etuaho43364892017-02-13 16:00:12 +00003607
jchen10af713a22017-04-19 09:10:56 +08003608 // add array index
3609 unsigned int arraySize = 0;
3610 if (arrayIndex != nullptr)
3611 {
3612 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3613 }
3614
3615 if (mShaderVersion < 310)
3616 {
3617 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3618 }
3619 else
3620 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003621 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3622 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003623 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003624
Andrei Volykhina5527072017-03-22 16:46:30 +03003625 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3626
Jamie Madill099c0f32013-06-20 11:55:52 -04003627 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003628 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003629 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3630 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003631
Jamie Madill099c0f32013-06-20 11:55:52 -04003632 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 if (typeQualifier.qualifier == EvqUniform)
3635 {
3636 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3637 }
3638 else if (typeQualifier.qualifier == EvqBuffer)
3639 {
3640 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3641 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003642 }
3643
Jamie Madill1566ef72013-06-20 11:55:54 -04003644 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3645 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003646 if (typeQualifier.qualifier == EvqUniform)
3647 {
3648 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3649 }
3650 else if (typeQualifier.qualifier == EvqBuffer)
3651 {
3652 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3653 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003654 }
3655
Olli Etuaho856c4972016-08-08 11:38:39 +03003656 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003657
Martin Radev2cc85b32016-08-05 16:22:53 +03003658 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3659
Jamie Madill98493dd2013-07-08 14:39:03 -04003660 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303661 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3662 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003663 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303664 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003665 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303666 {
jchen10cc2a10e2017-05-03 14:05:12 +08003667 std::string reason("unsupported type - ");
3668 reason += fieldType->getBasicString();
3669 reason += " types are not allowed in interface blocks";
3670 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003671 }
3672
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003674 switch (qualifier)
3675 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003676 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003677 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003678 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003679 if (typeQualifier.qualifier == EvqBuffer)
3680 {
3681 error(field->line(), "invalid qualifier on shader storage block member",
3682 getQualifierString(qualifier));
3683 }
3684 break;
3685 case EvqBuffer:
3686 if (typeQualifier.qualifier == EvqUniform)
3687 {
3688 error(field->line(), "invalid qualifier on uniform block member",
3689 getQualifierString(qualifier));
3690 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003691 break;
3692 default:
3693 error(field->line(), "invalid qualifier on interface block member",
3694 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003695 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003696 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003697
Martin Radev70866b82016-07-22 15:27:42 +03003698 if (fieldType->isInvariant())
3699 {
3700 error(field->line(), "invalid qualifier on interface block member", "invariant");
3701 }
3702
Jamie Madilla5efff92013-06-06 11:56:47 -04003703 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003704 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003705 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003706 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003707
Jamie Madill98493dd2013-07-08 14:39:03 -04003708 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003709 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003710 error(field->line(), "invalid layout qualifier: cannot be used here",
3711 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003712 }
3713
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003715 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003717 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003718 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003719 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003720 warning(field->line(),
3721 "extraneous layout qualifier: only has an effect on matrix types",
3722 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003723 }
3724
Jamie Madill98493dd2013-07-08 14:39:03 -04003725 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003726
Olli Etuahoebee5b32017-11-23 12:56:32 +02003727 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3728 typeQualifier.qualifier != EvqBuffer)
3729 {
3730 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3731 checkIsNotUnsizedArray(field->line(),
3732 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003733 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003734 }
3735
Jiajia Qinbc585152017-06-23 15:42:17 +08003736 if (typeQualifier.qualifier == EvqBuffer)
3737 {
3738 // set memory qualifiers
3739 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3740 // qualified with a memory qualifier, it is as if all of its members were declared with
3741 // the same memory qualifier.
3742 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3743 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3744 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3745 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3746 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3747 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3748 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3749 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3750 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3751 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3752 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003753 }
3754
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003755 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003756 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003757 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003758 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003759 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003760 }
3761
Olli Etuahob60d30f2018-01-16 12:31:06 +02003762 TType *interfaceBlockType =
3763 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003764 if (arrayIndex != nullptr)
3765 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003766 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003767 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003768
Olli Etuaho195be942017-12-04 23:40:14 +02003769 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003770 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3771 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003772 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003773 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003774 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003775
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003776 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003777 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003778 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003779 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3780 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003781 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003782 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003783
3784 // set parent pointer of the field variable
3785 fieldType->setInterfaceBlock(interfaceBlock);
3786
Olli Etuahob60d30f2018-01-16 12:31:06 +02003787 fieldType->setQualifier(typeQualifier.qualifier);
3788
Olli Etuaho195be942017-12-04 23:40:14 +02003789 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003790 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003791 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303792 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003793 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003794 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003795 }
3796 }
3797 }
3798 else
3799 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003800 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003801
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003802 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003803 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303804 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003805 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003806 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003807 }
3808
Olli Etuaho195be942017-12-04 23:40:14 +02003809 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3810 blockSymbol->setLine(typeQualifier.line);
3811 TIntermDeclaration *declaration = new TIntermDeclaration();
3812 declaration->appendDeclarator(blockSymbol);
3813 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003814
3815 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003816 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003817}
3818
Olli Etuahofbb1c792018-01-19 16:26:59 +02003819void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3820 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003821{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003822 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003823
3824 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003825 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303826 if (mStructNestingLevel > 1)
3827 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003828 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003829 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003830}
3831
3832void TParseContext::exitStructDeclaration()
3833{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003834 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003835}
3836
Olli Etuaho8a176262016-08-16 14:23:01 +03003837void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003838{
Jamie Madillacb4b812016-11-07 13:50:29 -05003839 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303840 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003841 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003842 }
3843
Arun Patole7e7e68d2015-05-22 12:02:25 +05303844 if (field.type()->getBasicType() != EbtStruct)
3845 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003846 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003847 }
3848
3849 // We're already inside a structure definition at this point, so add
3850 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303851 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3852 {
Jamie Madill41a49272014-03-18 16:10:13 -04003853 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003854 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3855 {
3856 // This may happen in case there are nested struct definitions. While they are also
3857 // invalid GLSL, they don't cause a syntax error.
3858 reasonStream << "Struct nesting";
3859 }
3860 else
3861 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003862 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003863 }
3864 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003865 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003866 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003867 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003869}
3870
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003871//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003872// Parse an array index expression
3873//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003874TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3875 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303876 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003877{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003878 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3879 {
3880 if (baseExpression->getAsSymbolNode())
3881 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303882 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003883 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003884 }
3885 else
3886 {
3887 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3888 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003889
Olli Etuaho3ec75682017-07-05 17:02:55 +03003890 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003891 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003892
Jiawei Shaod8105a02017-08-08 09:54:36 +08003893 if (baseExpression->getQualifier() == EvqPerVertexIn)
3894 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003895 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003896 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3897 {
3898 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3899 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3900 }
3901 }
3902
Jamie Madill21c1e452014-12-29 11:33:41 -05003903 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3904
Olli Etuaho36b05142015-11-12 13:10:42 +02003905 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3906 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3907 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3908 // index is a constant expression.
3909 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3910 {
3911 if (baseExpression->isInterfaceBlock())
3912 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003913 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003914 switch (baseExpression->getQualifier())
3915 {
3916 case EvqPerVertexIn:
3917 break;
3918 case EvqUniform:
3919 case EvqBuffer:
3920 error(location,
3921 "array indexes for uniform block arrays and shader storage block arrays "
3922 "must be constant integral expressions",
3923 "[");
3924 break;
3925 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003926 // We can reach here only in error cases.
3927 ASSERT(mDiagnostics->numErrors() > 0);
3928 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003929 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003930 }
3931 else if (baseExpression->getQualifier() == EvqFragmentOut)
3932 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003933 error(location,
3934 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003935 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003936 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3937 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003938 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003939 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003940 }
3941
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003942 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003943 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003944 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3945 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3946 // constant fold expressions that are not constant expressions). The most compatible way to
3947 // handle this case is to report a warning instead of an error and force the index to be in
3948 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003949 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003950 int index = 0;
3951 if (indexConstantUnion->getBasicType() == EbtInt)
3952 {
3953 index = indexConstantUnion->getIConst(0);
3954 }
3955 else if (indexConstantUnion->getBasicType() == EbtUInt)
3956 {
3957 index = static_cast<int>(indexConstantUnion->getUConst(0));
3958 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003959
3960 int safeIndex = -1;
3961
Olli Etuahoebee5b32017-11-23 12:56:32 +02003962 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003963 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003964 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3965 safeIndex = 0;
3966 }
3967
3968 if (!baseExpression->getType().isUnsizedArray())
3969 {
3970 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003971 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003972 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003973 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003974 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3975 {
3976 outOfRangeError(outOfRangeIndexIsError, location,
3977 "array index for gl_FragData must be zero when "
3978 "GL_EXT_draw_buffers is disabled",
3979 "[]");
3980 safeIndex = 0;
3981 }
3982 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003983 }
3984 // Only do generic out-of-range check if similar error hasn't already been reported.
3985 if (safeIndex < 0)
3986 {
3987 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02003988 {
3989 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3990 baseExpression->getOutermostArraySize(),
3991 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003993 else if (baseExpression->isMatrix())
3994 {
3995 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3996 baseExpression->getType().getCols(),
3997 "matrix field selection out of range");
3998 }
3999 else
4000 {
4001 ASSERT(baseExpression->isVector());
4002 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4003 baseExpression->getType().getNominalSize(),
4004 "vector field selection out of range");
4005 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004006 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004007
Olli Etuahoebee5b32017-11-23 12:56:32 +02004008 ASSERT(safeIndex >= 0);
4009 // Data of constant unions can't be changed, because it may be shared with other
4010 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4011 // sanitized object.
4012 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4013 {
4014 TConstantUnion *safeConstantUnion = new TConstantUnion();
4015 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004016 indexExpression = new TIntermConstantUnion(
4017 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4018 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004020
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 TIntermBinary *node =
4022 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4023 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004024 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004025 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004027
Olli Etuaho94bbed12018-03-20 14:44:53 +02004028 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4030 node->setLine(location);
4031 // Indirect indexing can never be constant folded.
4032 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004033}
4034
Olli Etuahoebee5b32017-11-23 12:56:32 +02004035int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4036 const TSourceLoc &location,
4037 int index,
4038 int arraySize,
4039 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004040{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004041 // Should not reach here with an unsized / runtime-sized array.
4042 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004043 // A negative index should already have been checked.
4044 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004045 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004046 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004047 std::stringstream reasonStream;
4048 reasonStream << reason << " '" << index << "'";
4049 std::string token = reasonStream.str();
4050 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004052 }
4053 return index;
4054}
4055
Jamie Madillb98c3a82015-07-23 14:26:04 -04004056TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4057 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004058 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004059 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004060{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004061 if (baseExpression->isArray())
4062 {
4063 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004064 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004065 }
4066
4067 if (baseExpression->isVector())
4068 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004069 TVector<int> fieldOffsets;
4070 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4071 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004073 fieldOffsets.resize(1);
4074 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004075 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004076 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4077 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078
Olli Etuaho765924f2018-01-04 12:48:36 +02004079 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004080 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081 else if (baseExpression->getBasicType() == EbtStruct)
4082 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304083 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004084 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085 {
4086 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004087 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 }
4089 else
4090 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004091 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004092 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004093 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004094 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004095 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004096 {
4097 fieldFound = true;
4098 break;
4099 }
4100 }
4101 if (fieldFound)
4102 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004103 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004104 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004105 TIntermBinary *node =
4106 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4107 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004108 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 }
4110 else
4111 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004112 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004113 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
4115 }
4116 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004117 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004118 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304119 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004120 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004121 {
4122 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004123 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004124 }
4125 else
4126 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004127 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004128 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004130 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004131 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004132 {
4133 fieldFound = true;
4134 break;
4135 }
4136 }
4137 if (fieldFound)
4138 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004139 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004141 TIntermBinary *node =
4142 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4143 node->setLine(dotLocation);
4144 // Indexing interface blocks can never be constant folded.
4145 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004146 }
4147 else
4148 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004149 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004150 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004151 }
4152 }
4153 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004154 else
4155 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004156 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004157 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004158 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004159 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004160 }
4161 else
4162 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304163 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004164 " field selection requires structure, vector, or interface block on left hand "
4165 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004166 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004168 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004169 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004170}
4171
Olli Etuahofbb1c792018-01-19 16:26:59 +02004172TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004173 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004174{
Jamie Madill2f294c92017-11-20 14:47:26 -05004175 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004176
4177 if (qualifierType == "shared")
4178 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004179 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004180 {
4181 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4182 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004183 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004184 }
4185 else if (qualifierType == "packed")
4186 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004187 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004188 {
4189 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4190 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004191 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004192 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004193 else if (qualifierType == "std430")
4194 {
4195 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4196 qualifier.blockStorage = EbsStd430;
4197 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004198 else if (qualifierType == "std140")
4199 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004200 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004201 }
4202 else if (qualifierType == "row_major")
4203 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004204 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004205 }
4206 else if (qualifierType == "column_major")
4207 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004208 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004209 }
4210 else if (qualifierType == "location")
4211 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004212 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004213 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004214 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004215 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004216 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004217 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4218 {
4219 qualifier.yuv = true;
4220 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004221 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004222 else if (qualifierType == "rgba32f")
4223 {
4224 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4225 qualifier.imageInternalFormat = EiifRGBA32F;
4226 }
4227 else if (qualifierType == "rgba16f")
4228 {
4229 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4230 qualifier.imageInternalFormat = EiifRGBA16F;
4231 }
4232 else if (qualifierType == "r32f")
4233 {
4234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4235 qualifier.imageInternalFormat = EiifR32F;
4236 }
4237 else if (qualifierType == "rgba8")
4238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.imageInternalFormat = EiifRGBA8;
4241 }
4242 else if (qualifierType == "rgba8_snorm")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4246 }
4247 else if (qualifierType == "rgba32i")
4248 {
4249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4250 qualifier.imageInternalFormat = EiifRGBA32I;
4251 }
4252 else if (qualifierType == "rgba16i")
4253 {
4254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4255 qualifier.imageInternalFormat = EiifRGBA16I;
4256 }
4257 else if (qualifierType == "rgba8i")
4258 {
4259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4260 qualifier.imageInternalFormat = EiifRGBA8I;
4261 }
4262 else if (qualifierType == "r32i")
4263 {
4264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4265 qualifier.imageInternalFormat = EiifR32I;
4266 }
4267 else if (qualifierType == "rgba32ui")
4268 {
4269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4270 qualifier.imageInternalFormat = EiifRGBA32UI;
4271 }
4272 else if (qualifierType == "rgba16ui")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.imageInternalFormat = EiifRGBA16UI;
4276 }
4277 else if (qualifierType == "rgba8ui")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifRGBA8UI;
4281 }
4282 else if (qualifierType == "r32ui")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifR32UI;
4286 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004287 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4288 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004289 {
4290 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4291 qualifier.primitiveType = EptPoints;
4292 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004293 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4294 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.primitiveType = EptLines;
4298 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004299 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4300 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.primitiveType = EptLinesAdjacency;
4304 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004305 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4306 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.primitiveType = EptTriangles;
4310 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004311 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4312 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004313 {
4314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4315 qualifier.primitiveType = EptTrianglesAdjacency;
4316 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004317 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4318 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.primitiveType = EptLineStrip;
4322 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004323 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4324 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004325 {
4326 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4327 qualifier.primitiveType = EptTriangleStrip;
4328 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004329
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004330 else
4331 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004332 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004333 }
4334
Jamie Madilla5efff92013-06-06 11:56:47 -04004335 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004336}
4337
Olli Etuahofbb1c792018-01-19 16:26:59 +02004338void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004339 const TSourceLoc &qualifierTypeLine,
4340 int intValue,
4341 const TSourceLoc &intValueLine,
4342 const std::string &intValueString,
4343 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004344 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004345{
Olli Etuaho856c4972016-08-08 11:38:39 +03004346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004347 if (intValue < 1)
4348 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004349 std::stringstream reasonStream;
4350 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4351 std::string reason = reasonStream.str();
4352 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004353 }
4354 (*localSize)[index] = intValue;
4355}
4356
Olli Etuaho09b04a22016-12-15 13:30:26 +00004357void TParseContext::parseNumViews(int intValue,
4358 const TSourceLoc &intValueLine,
4359 const std::string &intValueString,
4360 int *numViews)
4361{
4362 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4363 // specification.
4364 if (intValue < 1)
4365 {
4366 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4367 }
4368 *numViews = intValue;
4369}
4370
Shaob5cc1192017-07-06 10:47:20 +08004371void TParseContext::parseInvocations(int intValue,
4372 const TSourceLoc &intValueLine,
4373 const std::string &intValueString,
4374 int *numInvocations)
4375{
4376 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4377 // it doesn't make sense to accept invocations <= 0.
4378 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4379 {
4380 error(intValueLine,
4381 "out of range: invocations must be in the range of [1, "
4382 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4383 intValueString.c_str());
4384 }
4385 else
4386 {
4387 *numInvocations = intValue;
4388 }
4389}
4390
4391void TParseContext::parseMaxVertices(int intValue,
4392 const TSourceLoc &intValueLine,
4393 const std::string &intValueString,
4394 int *maxVertices)
4395{
4396 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4397 // it doesn't make sense to accept max_vertices < 0.
4398 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4399 {
4400 error(
4401 intValueLine,
4402 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4403 intValueString.c_str());
4404 }
4405 else
4406 {
4407 *maxVertices = intValue;
4408 }
4409}
4410
Olli Etuahofbb1c792018-01-19 16:26:59 +02004411TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004412 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004413 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304414 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004415{
Jamie Madill2f294c92017-11-20 14:47:26 -05004416 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004417
Martin Radev802abe02016-08-04 17:48:32 +03004418 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004419
Martin Radev802abe02016-08-04 17:48:32 +03004420 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004421 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004422 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004423 if (intValue < 0)
4424 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004425 error(intValueLine, "out of range: location must be non-negative",
4426 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004427 }
4428 else
4429 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004430 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004431 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004432 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004433 }
Olli Etuaho43364892017-02-13 16:00:12 +00004434 else if (qualifierType == "binding")
4435 {
4436 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4437 if (intValue < 0)
4438 {
4439 error(intValueLine, "out of range: binding must be non-negative",
4440 intValueString.c_str());
4441 }
4442 else
4443 {
4444 qualifier.binding = intValue;
4445 }
4446 }
jchen104cdac9e2017-05-08 11:01:20 +08004447 else if (qualifierType == "offset")
4448 {
4449 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4450 if (intValue < 0)
4451 {
4452 error(intValueLine, "out of range: offset must be non-negative",
4453 intValueString.c_str());
4454 }
4455 else
4456 {
4457 qualifier.offset = intValue;
4458 }
4459 }
Martin Radev802abe02016-08-04 17:48:32 +03004460 else if (qualifierType == "local_size_x")
4461 {
4462 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4463 &qualifier.localSize);
4464 }
4465 else if (qualifierType == "local_size_y")
4466 {
4467 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4468 &qualifier.localSize);
4469 }
4470 else if (qualifierType == "local_size_z")
4471 {
4472 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4473 &qualifier.localSize);
4474 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004475 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004476 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004477 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4478 {
4479 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4480 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004481 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004482 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4483 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004484 {
4485 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4486 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004487 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4488 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004489 {
4490 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4491 }
4492
Martin Radev802abe02016-08-04 17:48:32 +03004493 else
4494 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004495 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004496 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004497
Jamie Madilla5efff92013-06-06 11:56:47 -04004498 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004499}
4500
Olli Etuaho613b9592016-09-05 12:05:53 +03004501TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4502{
4503 return new TTypeQualifierBuilder(
4504 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4505 mShaderVersion);
4506}
4507
Olli Etuahocce89652017-06-19 16:04:09 +03004508TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4509 const TSourceLoc &loc)
4510{
4511 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4512 return new TStorageQualifierWrapper(qualifier, loc);
4513}
4514
4515TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4516{
4517 if (getShaderType() == GL_VERTEX_SHADER)
4518 {
4519 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4520 }
4521 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4522}
4523
4524TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4525{
4526 if (declaringFunction())
4527 {
4528 return new TStorageQualifierWrapper(EvqIn, loc);
4529 }
Shaob5cc1192017-07-06 10:47:20 +08004530
4531 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004532 {
Shaob5cc1192017-07-06 10:47:20 +08004533 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004534 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004535 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004536 {
4537 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4538 }
4539 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004540 }
Shaob5cc1192017-07-06 10:47:20 +08004541 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004542 {
Shaob5cc1192017-07-06 10:47:20 +08004543 if (mShaderVersion < 300)
4544 {
4545 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4546 }
4547 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004548 }
Shaob5cc1192017-07-06 10:47:20 +08004549 case GL_COMPUTE_SHADER:
4550 {
4551 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4552 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004553 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004554 {
4555 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4556 }
4557 default:
4558 {
4559 UNREACHABLE();
4560 return new TStorageQualifierWrapper(EvqLast, loc);
4561 }
Olli Etuahocce89652017-06-19 16:04:09 +03004562 }
Olli Etuahocce89652017-06-19 16:04:09 +03004563}
4564
4565TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4566{
4567 if (declaringFunction())
4568 {
4569 return new TStorageQualifierWrapper(EvqOut, loc);
4570 }
Shaob5cc1192017-07-06 10:47:20 +08004571 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004572 {
Shaob5cc1192017-07-06 10:47:20 +08004573 case GL_VERTEX_SHADER:
4574 {
4575 if (mShaderVersion < 300)
4576 {
4577 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4578 }
4579 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4580 }
4581 case GL_FRAGMENT_SHADER:
4582 {
4583 if (mShaderVersion < 300)
4584 {
4585 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4586 }
4587 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4588 }
4589 case GL_COMPUTE_SHADER:
4590 {
4591 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4592 return new TStorageQualifierWrapper(EvqLast, loc);
4593 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004594 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004595 {
4596 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4597 }
4598 default:
4599 {
4600 UNREACHABLE();
4601 return new TStorageQualifierWrapper(EvqLast, loc);
4602 }
Olli Etuahocce89652017-06-19 16:04:09 +03004603 }
Olli Etuahocce89652017-06-19 16:04:09 +03004604}
4605
4606TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4607{
4608 if (!declaringFunction())
4609 {
4610 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4611 }
4612 return new TStorageQualifierWrapper(EvqInOut, loc);
4613}
4614
Jamie Madillb98c3a82015-07-23 14:26:04 -04004615TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004616 TLayoutQualifier rightQualifier,
4617 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004618{
Martin Radevc28888b2016-07-22 15:27:42 +03004619 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004620 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004621}
4622
Olli Etuahofbb1c792018-01-19 16:26:59 +02004623TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4624 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004625{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004626 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004627 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004628}
4629
Olli Etuahofbb1c792018-01-19 16:26:59 +02004630TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004631 const TSourceLoc &loc,
4632 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004633{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004634 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004635 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004636}
4637
Olli Etuaho722bfb52017-10-26 17:00:11 +03004638void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4639 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004640 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004641 const TSourceLoc &location)
4642{
4643 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4644 {
4645 if ((*fieldIter)->name() == name)
4646 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004647 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004648 }
4649 }
4650}
4651
4652TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4653{
4654 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4655 ++fieldIter)
4656 {
4657 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4658 location);
4659 }
4660 return fields;
4661}
4662
Olli Etuaho4de340a2016-12-16 09:32:03 +00004663TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4664 const TFieldList *newlyAddedFields,
4665 const TSourceLoc &location)
4666{
4667 for (TField *field : *newlyAddedFields)
4668 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004669 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4670 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004671 processedFields->push_back(field);
4672 }
4673 return processedFields;
4674}
4675
Martin Radev70866b82016-07-22 15:27:42 +03004676TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4677 const TTypeQualifierBuilder &typeQualifierBuilder,
4678 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004679 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004680{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004681 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004682
Martin Radev70866b82016-07-22 15:27:42 +03004683 typeSpecifier->qualifier = typeQualifier.qualifier;
4684 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004685 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004686 typeSpecifier->invariant = typeQualifier.invariant;
4687 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304688 {
Martin Radev70866b82016-07-22 15:27:42 +03004689 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004690 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004691 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004692}
4693
Jamie Madillb98c3a82015-07-23 14:26:04 -04004694TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004695 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004696{
Martin Radev4a9cd802016-09-01 16:51:51 +03004697 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4698 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004699
Olli Etuahofbb1c792018-01-19 16:26:59 +02004700 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004701 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004702
Martin Radev4a9cd802016-09-01 16:51:51 +03004703 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004704
Olli Etuahod5f44c92017-11-29 17:15:40 +02004705 TFieldList *fieldList = new TFieldList();
4706
4707 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304708 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004709 TType *type = new TType(typeSpecifier);
4710 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304711 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004712 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004713 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004714 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004715 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004716
Jamie Madillf5557ac2018-06-15 09:46:58 -04004717 TField *field =
4718 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004719 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4720 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004721 }
4722
Olli Etuahod5f44c92017-11-29 17:15:40 +02004723 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004724}
4725
Martin Radev4a9cd802016-09-01 16:51:51 +03004726TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4727 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004728 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004729 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004730{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004731 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004732 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004733 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004734 structSymbolType = SymbolType::Empty;
4735 }
4736 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004737
Jamie Madill9b820842015-02-12 10:40:10 -05004738 // Store a bool in the struct if we're at global scope, to allow us to
4739 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004740 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004741
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004742 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004743 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004744 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004745 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304746 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004747 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748 }
4749 }
4750
4751 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004752 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004753 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004754 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004755 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004756 switch (qualifier)
4757 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004758 case EvqGlobal:
4759 case EvqTemporary:
4760 break;
4761 default:
4762 error(field.line(), "invalid qualifier on struct member",
4763 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004764 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004765 }
Martin Radev70866b82016-07-22 15:27:42 +03004766 if (field.type()->isInvariant())
4767 {
4768 error(field.line(), "invalid qualifier on struct member", "invariant");
4769 }
jchen104cdac9e2017-05-08 11:01:20 +08004770 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4771 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004772 {
4773 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4774 }
4775
Olli Etuahoebee5b32017-11-23 12:56:32 +02004776 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004777 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004778
Olli Etuaho43364892017-02-13 16:00:12 +00004779 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4780
4781 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004782
4783 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 }
4785
Martin Radev4a9cd802016-09-01 16:51:51 +03004786 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004787 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004788 exitStructDeclaration();
4789
Martin Radev4a9cd802016-09-01 16:51:51 +03004790 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004791}
4792
Jamie Madillb98c3a82015-07-23 14:26:04 -04004793TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004794 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004795 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004796{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004797 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004798 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004799 init->isVector())
4800 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004801 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4802 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004803 return nullptr;
4804 }
4805
Olli Etuaho923ecef2017-10-11 12:01:38 +03004806 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004807 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004808 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004809 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004810 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004811 }
4812
Olli Etuaho94bbed12018-03-20 14:44:53 +02004813 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004814 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4815 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004816 return node;
4817}
4818
4819TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4820{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004821 if (mSwitchNestingLevel == 0)
4822 {
4823 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004824 return nullptr;
4825 }
4826 if (condition == nullptr)
4827 {
4828 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004829 return nullptr;
4830 }
4831 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004833 {
4834 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004835 }
4836 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004837 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4838 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4839 // fold in case labels.
4840 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004841 {
4842 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004844 TIntermCase *node = new TIntermCase(condition);
4845 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004846 return node;
4847}
4848
4849TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4850{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 if (mSwitchNestingLevel == 0)
4852 {
4853 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004854 return nullptr;
4855 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004856 TIntermCase *node = new TIntermCase(nullptr);
4857 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004858 return node;
4859}
4860
Jamie Madillb98c3a82015-07-23 14:26:04 -04004861TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4862 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004863 const TSourceLoc &loc,
4864 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004865{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004866 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004867
4868 switch (op)
4869 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004870 case EOpLogicalNot:
4871 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4872 child->isVector())
4873 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004874 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004875 return nullptr;
4876 }
4877 break;
4878 case EOpBitwiseNot:
4879 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4880 child->isMatrix() || child->isArray())
4881 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004882 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004883 return nullptr;
4884 }
4885 break;
4886 case EOpPostIncrement:
4887 case EOpPreIncrement:
4888 case EOpPostDecrement:
4889 case EOpPreDecrement:
4890 case EOpNegative:
4891 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004892 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4893 child->getBasicType() == EbtBool || child->isArray() ||
4894 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004895 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004896 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004897 return nullptr;
4898 }
Nico Weber41b072b2018-02-09 10:01:32 -05004899 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004900 // Operators for built-ins are already type checked against their prototype.
4901 default:
4902 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004903 }
4904
Jiajia Qinbc585152017-06-23 15:42:17 +08004905 if (child->getMemoryQualifier().writeonly)
4906 {
4907 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4908 return nullptr;
4909 }
4910
Olli Etuaho94bbed12018-03-20 14:44:53 +02004911 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004912 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03004913 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004914
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004915 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004916}
4917
Olli Etuaho09b22472015-02-11 11:47:26 +02004918TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4919{
Olli Etuahocce89652017-06-19 16:04:09 +03004920 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004921 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004922 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004923 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004924 return child;
4925 }
4926 return node;
4927}
4928
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4930 TIntermTyped *child,
4931 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004932{
Olli Etuaho856c4972016-08-08 11:38:39 +03004933 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004934 return addUnaryMath(op, child, loc);
4935}
4936
Olli Etuaho765924f2018-01-04 12:48:36 +02004937TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4938{
4939 // If we can, we should return the folded version of the expression for subsequent parsing. This
4940 // enables folding the containing expression during parsing as well, instead of the separate
4941 // FoldExpressions() step where folding nested expressions requires multiple full AST
4942 // traversals.
4943
4944 // Even if folding fails the fold() functions return some node representing the expression,
4945 // typically the original node. So "folded" can be assumed to be non-null.
4946 TIntermTyped *folded = expression->fold(mDiagnostics);
4947 ASSERT(folded != nullptr);
4948 if (folded->getQualifier() == expression->getQualifier())
4949 {
4950 // We need this expression to have the correct qualifier when validating the consuming
4951 // expression. So we can only return the folded node from here in case it has the same
4952 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4953 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4954 // 1. (true ? 1.0 : non_constant)
4955 // 2. (non_constant, 1.0)
4956 return folded;
4957 }
4958 return expression;
4959}
4960
Jamie Madillb98c3a82015-07-23 14:26:04 -04004961bool TParseContext::binaryOpCommonCheck(TOperator op,
4962 TIntermTyped *left,
4963 TIntermTyped *right,
4964 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004965{
jchen10b4cf5652017-05-05 18:51:17 +08004966 // Check opaque types are not allowed to be operands in expressions other than array indexing
4967 // and structure member selection.
4968 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4969 {
4970 switch (op)
4971 {
4972 case EOpIndexDirect:
4973 case EOpIndexIndirect:
4974 break;
jchen10b4cf5652017-05-05 18:51:17 +08004975
4976 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004977 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08004978 error(loc, "Invalid operation for variables with an opaque type",
4979 GetOperatorString(op));
4980 return false;
4981 }
4982 }
jchen10cc2a10e2017-05-03 14:05:12 +08004983
Jiajia Qinbc585152017-06-23 15:42:17 +08004984 if (right->getMemoryQualifier().writeonly)
4985 {
4986 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4987 return false;
4988 }
4989
4990 if (left->getMemoryQualifier().writeonly)
4991 {
4992 switch (op)
4993 {
4994 case EOpAssign:
4995 case EOpInitialize:
4996 case EOpIndexDirect:
4997 case EOpIndexIndirect:
4998 case EOpIndexDirectStruct:
4999 case EOpIndexDirectInterfaceBlock:
5000 break;
5001 default:
5002 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5003 return false;
5004 }
5005 }
5006
Olli Etuaho244be012016-08-18 15:26:02 +03005007 if (left->getType().getStruct() || right->getType().getStruct())
5008 {
5009 switch (op)
5010 {
5011 case EOpIndexDirectStruct:
5012 ASSERT(left->getType().getStruct());
5013 break;
5014 case EOpEqual:
5015 case EOpNotEqual:
5016 case EOpAssign:
5017 case EOpInitialize:
5018 if (left->getType() != right->getType())
5019 {
5020 return false;
5021 }
5022 break;
5023 default:
5024 error(loc, "Invalid operation for structs", GetOperatorString(op));
5025 return false;
5026 }
5027 }
5028
Olli Etuaho94050052017-05-08 14:17:44 +03005029 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5030 {
5031 switch (op)
5032 {
5033 case EOpIndexDirectInterfaceBlock:
5034 ASSERT(left->getType().getInterfaceBlock());
5035 break;
5036 default:
5037 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5038 return false;
5039 }
5040 }
5041
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005042 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005043 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005044 error(loc, "array / non-array mismatch", GetOperatorString(op));
5045 return false;
5046 }
5047
5048 if (left->isArray())
5049 {
5050 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005051 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005052 {
5053 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5054 return false;
5055 }
5056
Olli Etuahoe79904c2015-03-18 16:56:42 +02005057 switch (op)
5058 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005059 case EOpEqual:
5060 case EOpNotEqual:
5061 case EOpAssign:
5062 case EOpInitialize:
5063 break;
5064 default:
5065 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5066 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005067 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005068 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005069 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005070 {
5071 error(loc, "array size mismatch", GetOperatorString(op));
5072 return false;
5073 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005074 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005075
5076 // Check ops which require integer / ivec parameters
5077 bool isBitShift = false;
5078 switch (op)
5079 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005080 case EOpBitShiftLeft:
5081 case EOpBitShiftRight:
5082 case EOpBitShiftLeftAssign:
5083 case EOpBitShiftRightAssign:
5084 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5085 // check that the basic type is an integer type.
5086 isBitShift = true;
5087 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5088 {
5089 return false;
5090 }
5091 break;
5092 case EOpBitwiseAnd:
5093 case EOpBitwiseXor:
5094 case EOpBitwiseOr:
5095 case EOpBitwiseAndAssign:
5096 case EOpBitwiseXorAssign:
5097 case EOpBitwiseOrAssign:
5098 // It is enough to check the type of only one operand, since later it
5099 // is checked that the operand types match.
5100 if (!IsInteger(left->getBasicType()))
5101 {
5102 return false;
5103 }
5104 break;
5105 default:
5106 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005107 }
5108
5109 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5110 // So the basic type should usually match.
5111 if (!isBitShift && left->getBasicType() != right->getBasicType())
5112 {
5113 return false;
5114 }
5115
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005116 // Check that:
5117 // 1. Type sizes match exactly on ops that require that.
5118 // 2. Restrictions for structs that contain arrays or samplers are respected.
5119 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005120 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005121 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005122 case EOpAssign:
5123 case EOpInitialize:
5124 case EOpEqual:
5125 case EOpNotEqual:
5126 // ESSL 1.00 sections 5.7, 5.8, 5.9
5127 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5128 {
5129 error(loc, "undefined operation for structs containing arrays",
5130 GetOperatorString(op));
5131 return false;
5132 }
5133 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5134 // we interpret the spec so that this extends to structs containing samplers,
5135 // similarly to ESSL 1.00 spec.
5136 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5137 left->getType().isStructureContainingSamplers())
5138 {
5139 error(loc, "undefined operation for structs containing samplers",
5140 GetOperatorString(op));
5141 return false;
5142 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005143
Olli Etuahoe1805592017-01-02 16:41:20 +00005144 if ((left->getNominalSize() != right->getNominalSize()) ||
5145 (left->getSecondarySize() != right->getSecondarySize()))
5146 {
5147 error(loc, "dimension mismatch", GetOperatorString(op));
5148 return false;
5149 }
5150 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005151 case EOpLessThan:
5152 case EOpGreaterThan:
5153 case EOpLessThanEqual:
5154 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005155 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005156 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005157 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005158 return false;
5159 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005160 break;
5161 case EOpAdd:
5162 case EOpSub:
5163 case EOpDiv:
5164 case EOpIMod:
5165 case EOpBitShiftLeft:
5166 case EOpBitShiftRight:
5167 case EOpBitwiseAnd:
5168 case EOpBitwiseXor:
5169 case EOpBitwiseOr:
5170 case EOpAddAssign:
5171 case EOpSubAssign:
5172 case EOpDivAssign:
5173 case EOpIModAssign:
5174 case EOpBitShiftLeftAssign:
5175 case EOpBitShiftRightAssign:
5176 case EOpBitwiseAndAssign:
5177 case EOpBitwiseXorAssign:
5178 case EOpBitwiseOrAssign:
5179 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5180 {
5181 return false;
5182 }
5183
5184 // Are the sizes compatible?
5185 if (left->getNominalSize() != right->getNominalSize() ||
5186 left->getSecondarySize() != right->getSecondarySize())
5187 {
5188 // If the nominal sizes of operands do not match:
5189 // One of them must be a scalar.
5190 if (!left->isScalar() && !right->isScalar())
5191 return false;
5192
5193 // In the case of compound assignment other than multiply-assign,
5194 // the right side needs to be a scalar. Otherwise a vector/matrix
5195 // would be assigned to a scalar. A scalar can't be shifted by a
5196 // vector either.
5197 if (!right->isScalar() &&
5198 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5199 return false;
5200 }
5201 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005202 default:
5203 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005204 }
5205
Olli Etuahod6b14282015-03-17 14:31:35 +02005206 return true;
5207}
5208
Olli Etuaho1dded802016-08-18 18:13:13 +03005209bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5210 const TType &left,
5211 const TType &right)
5212{
5213 switch (op)
5214 {
5215 case EOpMul:
5216 case EOpMulAssign:
5217 return left.getNominalSize() == right.getNominalSize() &&
5218 left.getSecondarySize() == right.getSecondarySize();
5219 case EOpVectorTimesScalar:
5220 return true;
5221 case EOpVectorTimesScalarAssign:
5222 ASSERT(!left.isMatrix() && !right.isMatrix());
5223 return left.isVector() && !right.isVector();
5224 case EOpVectorTimesMatrix:
5225 return left.getNominalSize() == right.getRows();
5226 case EOpVectorTimesMatrixAssign:
5227 ASSERT(!left.isMatrix() && right.isMatrix());
5228 return left.isVector() && left.getNominalSize() == right.getRows() &&
5229 left.getNominalSize() == right.getCols();
5230 case EOpMatrixTimesVector:
5231 return left.getCols() == right.getNominalSize();
5232 case EOpMatrixTimesScalar:
5233 return true;
5234 case EOpMatrixTimesScalarAssign:
5235 ASSERT(left.isMatrix() && !right.isMatrix());
5236 return !right.isVector();
5237 case EOpMatrixTimesMatrix:
5238 return left.getCols() == right.getRows();
5239 case EOpMatrixTimesMatrixAssign:
5240 ASSERT(left.isMatrix() && right.isMatrix());
5241 // We need to check two things:
5242 // 1. The matrix multiplication step is valid.
5243 // 2. The result will have the same number of columns as the lvalue.
5244 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5245
5246 default:
5247 UNREACHABLE();
5248 return false;
5249 }
5250}
5251
Jamie Madillb98c3a82015-07-23 14:26:04 -04005252TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5253 TIntermTyped *left,
5254 TIntermTyped *right,
5255 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005256{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005257 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005258 return nullptr;
5259
Olli Etuahofc1806e2015-03-17 13:03:11 +02005260 switch (op)
5261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005262 case EOpEqual:
5263 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005264 case EOpLessThan:
5265 case EOpGreaterThan:
5266 case EOpLessThanEqual:
5267 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005268 break;
5269 case EOpLogicalOr:
5270 case EOpLogicalXor:
5271 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005272 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5273 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005274 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005275 {
5276 return nullptr;
5277 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005278 // Basic types matching should have been already checked.
5279 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280 break;
5281 case EOpAdd:
5282 case EOpSub:
5283 case EOpDiv:
5284 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005285 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5286 !right->getType().getStruct());
5287 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 {
5289 return nullptr;
5290 }
5291 break;
5292 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005293 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5294 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005296 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 {
5298 return nullptr;
5299 }
5300 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 default:
5302 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005303 }
5304
Olli Etuaho1dded802016-08-18 18:13:13 +03005305 if (op == EOpMul)
5306 {
5307 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5308 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5309 {
5310 return nullptr;
5311 }
5312 }
5313
Olli Etuaho3fdec912016-08-18 15:08:06 +03005314 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005315 ASSERT(op != EOpAssign);
5316 markStaticReadIfSymbol(left);
5317 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005318 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005319 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005320}
5321
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5323 TIntermTyped *left,
5324 TIntermTyped *right,
5325 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005326{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005327 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005328 if (node == 0)
5329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5331 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005332 return left;
5333 }
5334 return node;
5335}
5336
Jamie Madillb98c3a82015-07-23 14:26:04 -04005337TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5338 TIntermTyped *left,
5339 TIntermTyped *right,
5340 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005341{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005342 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005343 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005344 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005345 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5346 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005347 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005348 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005349 }
5350 return node;
5351}
5352
Jamie Madillb98c3a82015-07-23 14:26:04 -04005353TIntermTyped *TParseContext::addAssign(TOperator op,
5354 TIntermTyped *left,
5355 TIntermTyped *right,
5356 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005357{
Olli Etuahocce89652017-06-19 16:04:09 +03005358 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005359 TIntermBinary *node = nullptr;
5360 if (binaryOpCommonCheck(op, left, right, loc))
5361 {
5362 if (op == EOpMulAssign)
5363 {
5364 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5365 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5366 {
5367 node = new TIntermBinary(op, left, right);
5368 }
5369 }
5370 else
5371 {
5372 node = new TIntermBinary(op, left, right);
5373 }
5374 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005375 if (node == nullptr)
5376 {
5377 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005378 return left;
5379 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005380 if (op != EOpAssign)
5381 {
5382 markStaticReadIfSymbol(left);
5383 }
5384 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005385 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005386 return node;
5387}
5388
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005389TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5390 TIntermTyped *right,
5391 const TSourceLoc &loc)
5392{
Corentin Wallez0d959252016-07-12 17:26:32 -04005393 // WebGL2 section 5.26, the following results in an error:
5394 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005395 if (mShaderSpec == SH_WEBGL2_SPEC &&
5396 (left->isArray() || left->getBasicType() == EbtVoid ||
5397 left->getType().isStructureContainingArrays() || right->isArray() ||
5398 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005399 {
5400 error(loc,
5401 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5402 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005403 }
5404
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005405 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005406 markStaticReadIfSymbol(left);
5407 markStaticReadIfSymbol(right);
5408 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005409
5410 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005411}
5412
Olli Etuaho49300862015-02-20 14:54:49 +02005413TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5414{
5415 switch (op)
5416 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005417 case EOpContinue:
5418 if (mLoopNestingLevel <= 0)
5419 {
5420 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005421 }
5422 break;
5423 case EOpBreak:
5424 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5425 {
5426 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005427 }
5428 break;
5429 case EOpReturn:
5430 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5431 {
5432 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005433 }
5434 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005435 case EOpKill:
5436 if (mShaderType != GL_FRAGMENT_SHADER)
5437 {
5438 error(loc, "discard supported in fragment shaders only", "discard");
5439 }
5440 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005441 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005442 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005443 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005444 }
Olli Etuahocce89652017-06-19 16:04:09 +03005445 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005446}
5447
Jamie Madillb98c3a82015-07-23 14:26:04 -04005448TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005449 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005451{
Olli Etuahocce89652017-06-19 16:04:09 +03005452 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005453 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005454 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005455 ASSERT(op == EOpReturn);
5456 mFunctionReturnsValue = true;
5457 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5458 {
5459 error(loc, "void function cannot return a value", "return");
5460 }
5461 else if (*mCurrentFunctionType != expression->getType())
5462 {
5463 error(loc, "function return is not matching type:", "return");
5464 }
Olli Etuaho49300862015-02-20 14:54:49 +02005465 }
Olli Etuahocce89652017-06-19 16:04:09 +03005466 TIntermBranch *node = new TIntermBranch(op, expression);
5467 node->setLine(loc);
5468 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005469}
5470
Olli Etuaho94bbed12018-03-20 14:44:53 +02005471void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5472{
5473 if (statement != nullptr)
5474 {
5475 markStaticReadIfSymbol(statement);
5476 block->appendStatement(statement);
5477 }
5478}
5479
Martin Radev84aa2dc2017-09-11 15:51:02 +03005480void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5481{
5482 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005483 const TFunction *func = functionCall->getFunction();
5484 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005485 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005486 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005487 TIntermNode *componentNode = nullptr;
5488 TIntermSequence *arguments = functionCall->getSequence();
5489 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5490 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5491 ASSERT(sampler != nullptr);
5492 switch (sampler->getBasicType())
5493 {
5494 case EbtSampler2D:
5495 case EbtISampler2D:
5496 case EbtUSampler2D:
5497 case EbtSampler2DArray:
5498 case EbtISampler2DArray:
5499 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005500 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005501 (isTextureGatherOffset && arguments->size() == 4u))
5502 {
5503 componentNode = arguments->back();
5504 }
5505 break;
5506 case EbtSamplerCube:
5507 case EbtISamplerCube:
5508 case EbtUSamplerCube:
5509 ASSERT(!isTextureGatherOffset);
5510 if (arguments->size() == 3u)
5511 {
5512 componentNode = arguments->back();
5513 }
5514 break;
5515 case EbtSampler2DShadow:
5516 case EbtSampler2DArrayShadow:
5517 case EbtSamplerCubeShadow:
5518 break;
5519 default:
5520 UNREACHABLE();
5521 break;
5522 }
5523 if (componentNode)
5524 {
5525 const TIntermConstantUnion *componentConstantUnion =
5526 componentNode->getAsConstantUnion();
5527 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5528 {
5529 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005530 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005531 }
5532 else
5533 {
5534 int component = componentConstantUnion->getIConst(0);
5535 if (component < 0 || component > 3)
5536 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005537 error(functionCall->getLine(), "Component must be in the range [0;3]",
5538 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005539 }
5540 }
5541 }
5542 }
5543}
5544
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005545void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5546{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005547 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005548 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005549 TIntermNode *offset = nullptr;
5550 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005551 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005552 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005553 {
5554 offset = arguments->back();
5555 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005556 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005557 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005558 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 ASSERT(arguments->size() >= 3);
5560 offset = (*arguments)[2];
5561 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005562 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005563 {
5564 ASSERT(arguments->size() >= 3u);
5565 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5566 ASSERT(sampler != nullptr);
5567 switch (sampler->getBasicType())
5568 {
5569 case EbtSampler2D:
5570 case EbtISampler2D:
5571 case EbtUSampler2D:
5572 case EbtSampler2DArray:
5573 case EbtISampler2DArray:
5574 case EbtUSampler2DArray:
5575 offset = (*arguments)[2];
5576 break;
5577 case EbtSampler2DShadow:
5578 case EbtSampler2DArrayShadow:
5579 offset = (*arguments)[3];
5580 break;
5581 default:
5582 UNREACHABLE();
5583 break;
5584 }
5585 useTextureGatherOffsetConstraints = true;
5586 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005587 if (offset != nullptr)
5588 {
5589 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5590 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5591 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005592 error(functionCall->getLine(), "Texture offset must be a constant expression",
5593 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005594 }
5595 else
5596 {
5597 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5598 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005599 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005600 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5601 : mMinProgramTexelOffset;
5602 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5603 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005604 for (size_t i = 0u; i < size; ++i)
5605 {
5606 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005607 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 {
5609 std::stringstream tokenStream;
5610 tokenStream << offsetValue;
5611 std::string token = tokenStream.str();
5612 error(offset->getLine(), "Texture offset value out of valid range",
5613 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005614 }
5615 }
5616 }
5617 }
5618}
5619
Jiajia Qina3106c52017-11-03 09:39:39 +08005620void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5621{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005622 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005623 const TFunction *func = functionCall->getFunction();
5624 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005625 {
5626 TIntermSequence *arguments = functionCall->getSequence();
5627 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5628
5629 if (IsBufferOrSharedVariable(memNode))
5630 {
5631 return;
5632 }
5633
5634 while (memNode->getAsBinaryNode())
5635 {
5636 memNode = memNode->getAsBinaryNode()->getLeft();
5637 if (IsBufferOrSharedVariable(memNode))
5638 {
5639 return;
5640 }
5641 }
5642
5643 error(memNode->getLine(),
5644 "The value passed to the mem argument of an atomic memory function does not "
5645 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005646 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005647 }
5648}
5649
Martin Radev2cc85b32016-08-05 16:22:53 +03005650// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5651void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5652{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005653 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005654
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005655 const TFunction *func = functionCall->getFunction();
5656
5657 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005658 {
5659 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005660 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005661
Olli Etuaho485eefd2017-02-14 17:40:06 +00005662 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005663
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005664 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005665 {
5666 if (memoryQualifier.readonly)
5667 {
5668 error(imageNode->getLine(),
5669 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005670 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005671 }
5672 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005673 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005674 {
5675 if (memoryQualifier.writeonly)
5676 {
5677 error(imageNode->getLine(),
5678 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005679 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005680 }
5681 }
5682 }
5683}
5684
5685// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5686void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5687 const TFunction *functionDefinition,
5688 const TIntermAggregate *functionCall)
5689{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005690 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005691
5692 const TIntermSequence &arguments = *functionCall->getSequence();
5693
5694 ASSERT(functionDefinition->getParamCount() == arguments.size());
5695
5696 for (size_t i = 0; i < arguments.size(); ++i)
5697 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005698 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5699 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005700 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005701 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5702
5703 if (IsImage(functionArgumentType.getBasicType()))
5704 {
5705 const TMemoryQualifier &functionArgumentMemoryQualifier =
5706 functionArgumentType.getMemoryQualifier();
5707 const TMemoryQualifier &functionParameterMemoryQualifier =
5708 functionParameterType.getMemoryQualifier();
5709 if (functionArgumentMemoryQualifier.readonly &&
5710 !functionParameterMemoryQualifier.readonly)
5711 {
5712 error(functionCall->getLine(),
5713 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005714 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005715 }
5716
5717 if (functionArgumentMemoryQualifier.writeonly &&
5718 !functionParameterMemoryQualifier.writeonly)
5719 {
5720 error(functionCall->getLine(),
5721 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005722 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 }
Martin Radev049edfa2016-11-11 14:35:37 +02005724
5725 if (functionArgumentMemoryQualifier.coherent &&
5726 !functionParameterMemoryQualifier.coherent)
5727 {
5728 error(functionCall->getLine(),
5729 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005730 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005731 }
5732
5733 if (functionArgumentMemoryQualifier.volatileQualifier &&
5734 !functionParameterMemoryQualifier.volatileQualifier)
5735 {
5736 error(functionCall->getLine(),
5737 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005738 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005739 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005740 }
5741 }
5742}
5743
Olli Etuaho95ed1942018-02-01 14:01:19 +02005744TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005745{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005746 if (fnCall->thisNode() != nullptr)
5747 {
5748 return addMethod(fnCall, loc);
5749 }
5750 if (fnCall->isConstructor())
5751 {
5752 return addConstructor(fnCall, loc);
5753 }
5754 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005755}
5756
Olli Etuaho95ed1942018-02-01 14:01:19 +02005757TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005758{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005759 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005760 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5761 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5762 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005763 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005764 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005765 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005766 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005768 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769 {
5770 error(loc, "method takes no parameters", "length");
5771 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005772 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 {
5774 error(loc, "length can only be called on arrays", "length");
5775 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005776 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005777 mGeometryShaderInputPrimitiveType == EptUndefined)
5778 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005779 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005780 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5781 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005782 else
5783 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005784 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005785 node->setLine(loc);
5786 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005788 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005789}
5790
Olli Etuaho95ed1942018-02-01 14:01:19 +02005791TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005792 const TSourceLoc &loc)
5793{
Olli Etuaho697bf652018-02-16 11:50:54 +02005794 // First check whether the function has been hidden by a variable name or struct typename by
5795 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5796 // with a matching argument list.
5797 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005798 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005799 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005800 }
5801 else
5802 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005803 // There are no inner functions, so it's enough to look for user-defined functions in the
5804 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005805 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005806 if (symbol != nullptr)
5807 {
5808 // A user-defined function - could be an overloaded built-in as well.
5809 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5810 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5811 TIntermAggregate *callNode =
5812 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5813 callNode->setLine(loc);
5814 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5815 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5816 return callNode;
5817 }
5818
5819 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005821 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005822 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005823 }
5824 else
5825 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005826 // A built-in function.
5827 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005829
Olli Etuaho37b697e2018-01-29 12:19:27 +02005830 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005831 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005832 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005833 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005834 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005835 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005836 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 if (fnCandidate->getParamCount() == 1)
5839 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005841 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005842 TIntermTyped *callNode =
5843 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005844 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005845 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005847 TIntermAggregate *callNode =
5848 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005849 callNode->setLine(loc);
5850
Olli Etuahoe80825e2018-02-16 10:24:53 +02005851 // Some built-in functions have out parameters too.
5852 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5853
5854 // See if we can constant fold a built-in. Note that this may be possible
5855 // even if it is not const-qualified.
5856 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005857 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005858
5859 // This is a built-in function with no op associated with it.
5860 TIntermAggregate *callNode =
5861 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5862 callNode->setLine(loc);
5863 checkTextureOffsetConst(callNode);
5864 checkTextureGather(callNode);
5865 checkImageMemoryAccessForBuiltinFunctions(callNode);
5866 checkAtomicMemoryBuiltinFunctions(callNode);
5867 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5868 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005869 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005871
5872 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005873 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005874}
5875
Jamie Madillb98c3a82015-07-23 14:26:04 -04005876TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005877 TIntermTyped *trueExpression,
5878 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005879 const TSourceLoc &loc)
5880{
Olli Etuaho56229f12017-07-10 14:16:33 +03005881 if (!checkIsScalarBool(loc, cond))
5882 {
5883 return falseExpression;
5884 }
Olli Etuaho52901742015-04-15 13:42:45 +03005885
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005886 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005887 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005888 std::stringstream reasonStream;
5889 reasonStream << "mismatching ternary operator operand types '"
5890 << trueExpression->getCompleteString() << " and '"
5891 << falseExpression->getCompleteString() << "'";
5892 std::string reason = reasonStream.str();
5893 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005894 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005895 }
Olli Etuahode318b22016-10-25 16:18:25 +01005896 if (IsOpaqueType(trueExpression->getBasicType()))
5897 {
5898 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005899 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005900 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5901 // Note that structs containing opaque types don't need to be checked as structs are
5902 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005903 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005904 return falseExpression;
5905 }
5906
Jiajia Qinbc585152017-06-23 15:42:17 +08005907 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5908 falseExpression->getMemoryQualifier().writeonly)
5909 {
5910 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5911 return falseExpression;
5912 }
5913
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005914 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005915 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005916 // ESSL 3.00.6 section 5.7:
5917 // Ternary operator support is optional for arrays. No certainty that it works across all
5918 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5919 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005920 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005921 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005922 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005923 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005924 }
Olli Etuaho94050052017-05-08 14:17:44 +03005925 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5926 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005927 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005928 return falseExpression;
5929 }
5930
Corentin Wallez0d959252016-07-12 17:26:32 -04005931 // WebGL2 section 5.26, the following results in an error:
5932 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005933 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005934 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005935 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005936 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005937 }
5938
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005939 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005940 markStaticReadIfSymbol(cond);
5941 markStaticReadIfSymbol(trueExpression);
5942 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005943 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005944 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005945}
Olli Etuaho49300862015-02-20 14:54:49 +02005946
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005947//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005948// Parse an array of strings using yyparse.
5949//
5950// Returns 0 for success.
5951//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005952int PaParseStrings(size_t count,
5953 const char *const string[],
5954 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305955 TParseContext *context)
5956{
Yunchao He4f285442017-04-21 12:15:49 +08005957 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005958 return 1;
5959
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005960 if (glslang_initialize(context))
5961 return 1;
5962
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005963 int error = glslang_scan(count, string, length, context);
5964 if (!error)
5965 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005966
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005967 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005968
alokp@chromium.org6b495712012-06-29 00:06:58 +00005969 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005970}
Jamie Madill45bcc782016-11-07 13:58:48 -05005971
5972} // namespace sh