blob: 81eb6cdc8c3df47b05e9ca7fdd8b967cb259a9f5 [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//
Olli Etuaho72e35892018-06-20 11:43:08 +0300370void TParseContext::assignError(const TSourceLoc &line,
371 const char *op,
372 const TType &left,
373 const TType &right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000374{
Olli Etuaho72e35892018-06-20 11:43:08 +0300375 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000376 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
Olli Etuaho72e35892018-06-20 11:43:08 +0300377 error(line, reasonStream.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000378}
379
380//
381// Same error message for all places unary operations don't work.
382//
Olli Etuaho72e35892018-06-20 11:43:08 +0300383void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, const TType &operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000384{
Olli Etuaho72e35892018-06-20 11:43:08 +0300385 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000386 reasonStream << "wrong operand type - no operation '" << op
387 << "' exists that takes an operand of type " << operand
388 << " (or there is no acceptable conversion)";
Olli Etuaho72e35892018-06-20 11:43:08 +0300389 error(line, reasonStream.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390}
391
392//
393// Same error message for all binary operations don't work.
394//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400395void TParseContext::binaryOpError(const TSourceLoc &line,
396 const char *op,
Olli Etuaho72e35892018-06-20 11:43:08 +0300397 const TType &left,
398 const TType &right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000399{
Olli Etuaho72e35892018-06-20 11:43:08 +0300400 TInfoSinkBase reasonStream;
Olli Etuaho4de340a2016-12-16 09:32:03 +0000401 reasonStream << "wrong operand types - no operation '" << op
402 << "' exists that takes a left-hand operand of type '" << left
403 << "' and a right operand of type '" << right
404 << "' (or there is no acceptable conversion)";
Olli Etuaho72e35892018-06-20 11:43:08 +0300405 error(line, reasonStream.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 Etuaho72e35892018-06-20 11:43:08 +03001048 TInfoSinkBase typeString;
1049 typeString << TType(elementType);
1050 error(line, "cannot declare arrays of arrays", typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001051 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001052 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001053 return true;
1054}
1055
1056// Check if this qualified element type can be formed into an array. This is only called when array
1057// brackets are associated with an identifier in a declaration, like this:
1058// float a[2];
1059// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1060// are associated with the type, like this:
1061// float[2] a;
1062bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1063 const TPublicType &elementType)
1064{
1065 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1066 {
1067 return false;
1068 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001069 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1070 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1071 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001072 // Geometry shader requires each user-defined input be declared as arrays or inside input
1073 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1074 // interface matching, such variables and blocks are treated as though they were not declared
1075 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001076 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001077 sh::IsVarying(elementType.qualifier) &&
1078 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001079 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001080 TInfoSinkBase typeString;
1081 typeString << TType(elementType);
Olli Etuahoe0803872017-08-23 15:30:23 +03001082 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho72e35892018-06-20 11:43:08 +03001083 typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001084 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001085 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001086 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087}
1088
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001090void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001091 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093{
Olli Etuaho3739d232015-04-08 12:23:44 +03001094 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001095 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001096 {
1097 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001098 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001099
1100 // Generate informative error messages for ESSL1.
1101 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001102 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001103 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001105 "structures containing arrays may not be declared constant since they cannot be "
1106 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001107 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001108 }
1109 else
1110 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001111 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001112 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001113 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001114 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001115 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1116 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117}
1118
Olli Etuaho2935c582015-04-08 14:32:06 +03001119// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120// and update the symbol table.
1121//
Olli Etuaho2935c582015-04-08 14:32:06 +03001122// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001125 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001126 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001127 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128{
Olli Etuaho2935c582015-04-08 14:32:06 +03001129 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130
Olli Etuahofbb1c792018-01-19 16:26:59 +02001131 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001132
Olli Etuahob60d30f2018-01-16 12:31:06 +02001133 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001134
Olli Etuaho856c4972016-08-08 11:38:39 +03001135 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136
Olli Etuaho2935c582015-04-08 14:32:06 +03001137 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001138 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001139 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001140 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001141 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001142 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001143 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001144 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001145 return false;
1146 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001147 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001148 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001149 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001150 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001152 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001153 }
1154 }
1155 else
1156 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001158 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001159 return false;
1160 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001161 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162
Olli Etuaho8a176262016-08-16 14:23:01 +03001163 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001164 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165
Olli Etuaho437664b2018-02-28 15:38:14 +02001166 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001167 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001168 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001169 return false;
1170 }
1171
Olli Etuahob60d30f2018-01-16 12:31:06 +02001172 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001173 return false;
1174
1175 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176}
1177
Martin Radev70866b82016-07-22 15:27:42 +03001178void TParseContext::checkIsParameterQualifierValid(
1179 const TSourceLoc &line,
1180 const TTypeQualifierBuilder &typeQualifierBuilder,
1181 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301182{
Olli Etuahocce89652017-06-19 16:04:09 +03001183 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001184 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001185
1186 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301187 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001188 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1189 }
1190
1191 if (!IsImage(type->getBasicType()))
1192 {
Olli Etuaho43364892017-02-13 16:00:12 +00001193 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001194 }
1195 else
1196 {
1197 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001199
Martin Radev70866b82016-07-22 15:27:42 +03001200 type->setQualifier(typeQualifier.qualifier);
1201
1202 if (typeQualifier.precision != EbpUndefined)
1203 {
1204 type->setPrecision(typeQualifier.precision);
1205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001206}
1207
Olli Etuaho703671e2017-11-08 17:47:18 +02001208template <size_t size>
1209bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1210 const std::array<TExtension, size> &extensions)
1211{
1212 ASSERT(!extensions.empty());
1213 const TExtensionBehavior &extBehavior = extensionBehavior();
1214
1215 bool canUseWithWarning = false;
1216 bool canUseWithoutWarning = false;
1217
1218 const char *errorMsgString = "";
1219 TExtension errorMsgExtension = TExtension::UNDEFINED;
1220
1221 for (TExtension extension : extensions)
1222 {
1223 auto extIter = extBehavior.find(extension);
1224 if (canUseWithWarning)
1225 {
1226 // We already have an extension that we can use, but with a warning.
1227 // See if we can use the alternative extension without a warning.
1228 if (extIter == extBehavior.end())
1229 {
1230 continue;
1231 }
1232 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1233 {
1234 canUseWithoutWarning = true;
1235 break;
1236 }
1237 continue;
1238 }
1239 if (extIter == extBehavior.end())
1240 {
1241 errorMsgString = "extension is not supported";
1242 errorMsgExtension = extension;
1243 }
1244 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1245 {
1246 errorMsgString = "extension is disabled";
1247 errorMsgExtension = extension;
1248 }
1249 else if (extIter->second == EBhWarn)
1250 {
1251 errorMsgExtension = extension;
1252 canUseWithWarning = true;
1253 }
1254 else
1255 {
1256 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1257 canUseWithoutWarning = true;
1258 break;
1259 }
1260 }
1261
1262 if (canUseWithoutWarning)
1263 {
1264 return true;
1265 }
1266 if (canUseWithWarning)
1267 {
1268 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1269 return true;
1270 }
1271 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1272 return false;
1273}
1274
1275template bool TParseContext::checkCanUseOneOfExtensions(
1276 const TSourceLoc &line,
1277 const std::array<TExtension, 1> &extensions);
1278template bool TParseContext::checkCanUseOneOfExtensions(
1279 const TSourceLoc &line,
1280 const std::array<TExtension, 2> &extensions);
1281template bool TParseContext::checkCanUseOneOfExtensions(
1282 const TSourceLoc &line,
1283 const std::array<TExtension, 3> &extensions);
1284
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001285bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001286{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001287 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001288 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289}
1290
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001291// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1292// compile-time or link-time errors are the same whether or not the declaration is empty".
1293// This function implements all the checks that are done on qualifiers regardless of if the
1294// declaration is empty.
1295void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1296 const sh::TLayoutQualifier &layoutQualifier,
1297 const TSourceLoc &location)
1298{
1299 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1300 {
1301 error(location, "Shared memory declarations cannot have layout specified", "layout");
1302 }
1303
1304 if (layoutQualifier.matrixPacking != EmpUnspecified)
1305 {
1306 error(location, "layout qualifier only valid for interface blocks",
1307 getMatrixPackingString(layoutQualifier.matrixPacking));
1308 return;
1309 }
1310
1311 if (layoutQualifier.blockStorage != EbsUnspecified)
1312 {
1313 error(location, "layout qualifier only valid for interface blocks",
1314 getBlockStorageString(layoutQualifier.blockStorage));
1315 return;
1316 }
1317
1318 if (qualifier == EvqFragmentOut)
1319 {
1320 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1321 {
1322 error(location, "invalid layout qualifier combination", "yuv");
1323 return;
1324 }
1325 }
1326 else
1327 {
1328 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1329 }
1330
Olli Etuaho95468d12017-05-04 11:14:34 +03001331 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1332 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001333 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1334 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001335 {
1336 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1337 }
1338
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001339 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001340 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001341 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001342 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001343 // We're not checking whether the uniform location is in range here since that depends on
1344 // the type of the variable.
1345 // The type can only be fully determined for non-empty declarations.
1346 }
1347 if (!canHaveLocation)
1348 {
1349 checkLocationIsNotSpecified(location, layoutQualifier);
1350 }
1351}
1352
jchen104cdac9e2017-05-08 11:01:20 +08001353void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1354 const TSourceLoc &location)
1355{
1356 if (publicType.precision != EbpHigh)
1357 {
1358 error(location, "Can only be highp", "atomic counter");
1359 }
1360 // dEQP enforces compile error if location is specified. See uniform_location.test.
1361 if (publicType.layoutQualifier.location != -1)
1362 {
1363 error(location, "location must not be set for atomic_uint", "layout");
1364 }
1365 if (publicType.layoutQualifier.binding == -1)
1366 {
1367 error(location, "no binding specified", "atomic counter");
1368 }
1369}
1370
Olli Etuaho55bde912017-10-25 13:41:13 +03001371void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001372{
Olli Etuaho55bde912017-10-25 13:41:13 +03001373 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001374 {
1375 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1376 // error. It is assumed that this applies to empty declarations as well.
1377 error(location, "empty array declaration needs to specify a size", "");
1378 }
Martin Radevb8b01222016-11-20 23:25:53 +02001379}
1380
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001381// These checks are done for all declarations that are non-empty. They're done for non-empty
1382// declarations starting a declarator list, and declarators that follow an empty declaration.
1383void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1384 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001385{
Olli Etuahofa33d582015-04-09 14:33:12 +03001386 switch (publicType.qualifier)
1387 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001388 case EvqVaryingIn:
1389 case EvqVaryingOut:
1390 case EvqAttribute:
1391 case EvqVertexIn:
1392 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001393 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001394 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001395 {
1396 error(identifierLocation, "cannot be used with a structure",
1397 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001398 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001399 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001400 break;
1401 case EvqBuffer:
1402 if (publicType.getBasicType() != EbtInterfaceBlock)
1403 {
1404 error(identifierLocation,
1405 "cannot declare buffer variables at global scope(outside a block)",
1406 getQualifierString(publicType.qualifier));
1407 return;
1408 }
1409 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001410 default:
1411 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001412 }
jchen10cc2a10e2017-05-03 14:05:12 +08001413 std::string reason(getBasicString(publicType.getBasicType()));
1414 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001415 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001416 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001417 {
1418 return;
1419 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001420
Andrei Volykhina5527072017-03-22 16:46:30 +03001421 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1422 publicType.qualifier != EvqConst) &&
1423 publicType.getBasicType() == EbtYuvCscStandardEXT)
1424 {
1425 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1426 getQualifierString(publicType.qualifier));
1427 return;
1428 }
1429
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001430 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1431 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001432 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1433 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001434 TType type(publicType);
1435 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001436 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001437 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1438 publicType.layoutQualifier);
1439 }
1440 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001441
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001442 // check for layout qualifier issues
1443 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001444
Martin Radev2cc85b32016-08-05 16:22:53 +03001445 if (IsImage(publicType.getBasicType()))
1446 {
1447
1448 switch (layoutQualifier.imageInternalFormat)
1449 {
1450 case EiifRGBA32F:
1451 case EiifRGBA16F:
1452 case EiifR32F:
1453 case EiifRGBA8:
1454 case EiifRGBA8_SNORM:
1455 if (!IsFloatImage(publicType.getBasicType()))
1456 {
1457 error(identifierLocation,
1458 "internal image format requires a floating image type",
1459 getBasicString(publicType.getBasicType()));
1460 return;
1461 }
1462 break;
1463 case EiifRGBA32I:
1464 case EiifRGBA16I:
1465 case EiifRGBA8I:
1466 case EiifR32I:
1467 if (!IsIntegerImage(publicType.getBasicType()))
1468 {
1469 error(identifierLocation,
1470 "internal image format requires an integer image type",
1471 getBasicString(publicType.getBasicType()));
1472 return;
1473 }
1474 break;
1475 case EiifRGBA32UI:
1476 case EiifRGBA16UI:
1477 case EiifRGBA8UI:
1478 case EiifR32UI:
1479 if (!IsUnsignedImage(publicType.getBasicType()))
1480 {
1481 error(identifierLocation,
1482 "internal image format requires an unsigned image type",
1483 getBasicString(publicType.getBasicType()));
1484 return;
1485 }
1486 break;
1487 case EiifUnspecified:
1488 error(identifierLocation, "layout qualifier", "No image internal format specified");
1489 return;
1490 default:
1491 error(identifierLocation, "layout qualifier", "unrecognized token");
1492 return;
1493 }
1494
1495 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1496 switch (layoutQualifier.imageInternalFormat)
1497 {
1498 case EiifR32F:
1499 case EiifR32I:
1500 case EiifR32UI:
1501 break;
1502 default:
1503 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1504 {
1505 error(identifierLocation, "layout qualifier",
1506 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1507 "image variables must be qualified readonly and/or writeonly");
1508 return;
1509 }
1510 break;
1511 }
1512 }
1513 else
1514 {
Olli Etuaho43364892017-02-13 16:00:12 +00001515 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001516 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1517 }
jchen104cdac9e2017-05-08 11:01:20 +08001518
1519 if (IsAtomicCounter(publicType.getBasicType()))
1520 {
1521 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1522 }
1523 else
1524 {
1525 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1526 }
Olli Etuaho43364892017-02-13 16:00:12 +00001527}
Martin Radev2cc85b32016-08-05 16:22:53 +03001528
Olli Etuaho43364892017-02-13 16:00:12 +00001529void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1530{
1531 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1533 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1534 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1535 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1536 // when it comes to which shaders are accepted by the compiler.
1537 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001538 if (IsImage(type.getBasicType()))
1539 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001540 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1541 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001542 }
1543 else if (IsSampler(type.getBasicType()))
1544 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001545 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1546 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001547 }
jchen104cdac9e2017-05-08 11:01:20 +08001548 else if (IsAtomicCounter(type.getBasicType()))
1549 {
1550 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1551 }
Olli Etuaho43364892017-02-13 16:00:12 +00001552 else
1553 {
1554 ASSERT(!IsOpaqueType(type.getBasicType()));
1555 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001556 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001557}
1558
Olli Etuaho856c4972016-08-08 11:38:39 +03001559void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001560 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001561 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001562{
1563
1564 if (mShaderVersion < versionRequired)
1565 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001566 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001567 }
1568}
1569
Olli Etuaho856c4972016-08-08 11:38:39 +03001570bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1571 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001572{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001573 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001574 for (size_t i = 0u; i < localSize.size(); ++i)
1575 {
1576 if (localSize[i] != -1)
1577 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001578 error(location,
1579 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1580 "global layout declaration",
1581 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001582 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001583 }
1584 }
1585
Olli Etuaho8a176262016-08-16 14:23:01 +03001586 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001587}
1588
Olli Etuaho43364892017-02-13 16:00:12 +00001589void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001590 TLayoutImageInternalFormat internalFormat)
1591{
1592 if (internalFormat != EiifUnspecified)
1593 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001594 error(location, "invalid layout qualifier: only valid when used with images",
1595 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001596 }
Olli Etuaho43364892017-02-13 16:00:12 +00001597}
1598
1599void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1600{
1601 if (binding != -1)
1602 {
1603 error(location,
1604 "invalid layout qualifier: only valid when used with opaque types or blocks",
1605 "binding");
1606 }
1607}
1608
jchen104cdac9e2017-05-08 11:01:20 +08001609void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1610{
1611 if (offset != -1)
1612 {
1613 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1614 "offset");
1615 }
1616}
1617
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1619 int binding,
1620 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001621{
1622 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001623 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001624 {
1625 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1626 }
1627}
1628
1629void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1630 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001631 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001632{
1633 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001634 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001635 {
1636 error(location, "sampler binding greater than maximum texture units", "binding");
1637 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001638}
1639
Jiajia Qinbc585152017-06-23 15:42:17 +08001640void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1641 const TQualifier &qualifier,
1642 int binding,
1643 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001644{
1645 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001646 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001647 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001648 if (binding + size > mMaxUniformBufferBindings)
1649 {
1650 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1651 "binding");
1652 }
1653 }
1654 else if (qualifier == EvqBuffer)
1655 {
1656 if (binding + size > mMaxShaderStorageBufferBindings)
1657 {
1658 error(location,
1659 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1660 "binding");
1661 }
jchen10af713a22017-04-19 09:10:56 +08001662 }
1663}
jchen104cdac9e2017-05-08 11:01:20 +08001664void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1665{
1666 if (binding >= mMaxAtomicCounterBindings)
1667 {
1668 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1669 "binding");
1670 }
1671}
jchen10af713a22017-04-19 09:10:56 +08001672
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001673void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1674 int objectLocationCount,
1675 const TLayoutQualifier &layoutQualifier)
1676{
1677 int loc = layoutQualifier.location;
1678 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1679 {
1680 error(location, "Uniform location out of range", "location");
1681 }
1682}
1683
Andrei Volykhina5527072017-03-22 16:46:30 +03001684void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1685{
1686 if (yuv != false)
1687 {
1688 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1689 }
1690}
1691
Jiajia Qinbc585152017-06-23 15:42:17 +08001692void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1693 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001694{
1695 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1696 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001697 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001698 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001699 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1700 qual == EvqConstReadOnly);
1701 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001702 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001703 markStaticReadIfSymbol(argument);
1704 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001705 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001706 if (argument->getMemoryQualifier().writeonly)
1707 {
1708 error(argument->getLine(),
1709 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1710 fnCall->functionName());
1711 return;
1712 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001713 }
1714 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001715 if (qual == EvqOut || qual == EvqInOut)
1716 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001717 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001718 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001719 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001720 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001721 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001722 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001723 }
1724 }
1725 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001726}
1727
Martin Radev70866b82016-07-22 15:27:42 +03001728void TParseContext::checkInvariantVariableQualifier(bool invariant,
1729 const TQualifier qualifier,
1730 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731{
Martin Radev70866b82016-07-22 15:27:42 +03001732 if (!invariant)
1733 return;
1734
1735 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001736 {
Martin Radev70866b82016-07-22 15:27:42 +03001737 // input variables in the fragment shader can be also qualified as invariant
1738 if (!sh::CanBeInvariantESSL1(qualifier))
1739 {
1740 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1741 }
1742 }
1743 else
1744 {
1745 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1746 {
1747 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1748 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001749 }
1750}
1751
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001752bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001753{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001754 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001755}
1756
Jamie Madillb98c3a82015-07-23 14:26:04 -04001757void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1758 const char *extName,
1759 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001760{
Geoff Lang197d5292018-04-25 14:29:00 -04001761 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001762 srcLoc.file = loc.first_file;
1763 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001764 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001765}
1766
Jamie Madillb98c3a82015-07-23 14:26:04 -04001767void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1768 const char *name,
1769 const char *value,
1770 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001771{
Geoff Lang197d5292018-04-25 14:29:00 -04001772 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001773 srcLoc.file = loc.first_file;
1774 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001775 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001776}
1777
Martin Radev4c4c8e72016-08-04 12:25:34 +03001778sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001779{
Jamie Madill2f294c92017-11-20 14:47:26 -05001780 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001781 for (size_t i = 0u; i < result.size(); ++i)
1782 {
1783 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1784 {
1785 result[i] = 1;
1786 }
1787 else
1788 {
1789 result[i] = mComputeShaderLocalSize[i];
1790 }
1791 }
1792 return result;
1793}
1794
Olli Etuaho56229f12017-07-10 14:16:33 +03001795TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1796 const TSourceLoc &line)
1797{
1798 TIntermConstantUnion *node = new TIntermConstantUnion(
1799 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1800 node->setLine(line);
1801 return node;
1802}
1803
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804/////////////////////////////////////////////////////////////////////////////////
1805//
1806// Non-Errors.
1807//
1808/////////////////////////////////////////////////////////////////////////////////
1809
Jamie Madill5c097022014-08-20 16:38:32 -04001810const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001811 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001812 const TSymbol *symbol)
1813{
Jamie Madill5c097022014-08-20 16:38:32 -04001814 if (!symbol)
1815 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001816 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001817 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001818 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001819
1820 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001821 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001822 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001823 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001824 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001825
1826 const TVariable *variable = static_cast<const TVariable *>(symbol);
1827
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001828 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001829 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001830 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001831 }
1832
Olli Etuaho0f684632017-07-13 12:42:15 +03001833 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1834 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
Olli Etuaho59c5b892018-04-03 11:44:50 +03001835 variable->getType().getQualifier() == EvqWorkGroupSize)
Olli Etuaho0f684632017-07-13 12:42:15 +03001836 {
1837 error(location,
1838 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1839 "gl_WorkGroupSize");
1840 }
Jamie Madill5c097022014-08-20 16:38:32 -04001841 return variable;
1842}
1843
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001844TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001845 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001846 const TSymbol *symbol)
1847{
1848 const TVariable *variable = getNamedVariable(location, name, symbol);
1849
Olli Etuaho0f684632017-07-13 12:42:15 +03001850 if (!variable)
1851 {
1852 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1853 node->setLine(location);
1854 return node;
1855 }
1856
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001857 const TType &variableType = variable->getType();
Jamie Madill50cf2be2018-06-15 09:46:57 -04001858 TIntermTyped *node = nullptr;
Olli Etuaho56229f12017-07-10 14:16:33 +03001859
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001860 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001861 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001862 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001863 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001864 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001865 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001866 {
1867 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1868 // needs to be added to the AST as a constant and not as a symbol.
1869 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1870 TConstantUnion *constArray = new TConstantUnion[3];
1871 for (size_t i = 0; i < 3; ++i)
1872 {
1873 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1874 }
1875
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 ASSERT(variableType.getBasicType() == EbtUInt);
1877 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001878
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001879 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001880 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001881 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001882 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001883 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1884 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001885 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001886 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1887 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001888 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001889 else
1890 {
Olli Etuaho195be942017-12-04 23:40:14 +02001891 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001892 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001893 ASSERT(node != nullptr);
1894 node->setLine(location);
1895 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896}
1897
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898// Initializers show up in several places in the grammar. Have one set of
1899// code to handle them here.
1900//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001901// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001902bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001903 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001904 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001905 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001906 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907{
Olli Etuaho13389b62016-10-16 11:48:18 +01001908 ASSERT(initNode != nullptr);
1909 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910
Olli Etuahob60d30f2018-01-16 12:31:06 +02001911 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001912 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001913 // In case initializer is not an array or type has more dimensions than initializer, this
1914 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1915 // actually is an array or not. Having a non-array initializer for an unsized array will
1916 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001917 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001918 type->sizeUnsizedArrays(arraySizes);
1919 }
1920
1921 const TQualifier qualifier = type->getQualifier();
1922
1923 bool constError = false;
1924 if (qualifier == EvqConst)
1925 {
1926 if (EvqConst != initializer->getType().getQualifier())
1927 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001928 TInfoSinkBase reasonStream;
1929 reasonStream << "assigning non-constant to '" << *type << "'";
1930 error(line, reasonStream.c_str(), "=");
Olli Etuahob60d30f2018-01-16 12:31:06 +02001931
1932 // We're still going to declare the variable to avoid extra error messages.
1933 type->setQualifier(EvqTemporary);
1934 constError = true;
1935 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001936 }
Olli Etuaho195be942017-12-04 23:40:14 +02001937
1938 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001939 if (!declareVariable(line, identifier, type, &variable))
1940 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001941 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001942 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943
Olli Etuahob60d30f2018-01-16 12:31:06 +02001944 if (constError)
1945 {
1946 return false;
1947 }
1948
Olli Etuahob0c645e2015-05-12 14:25:36 +03001949 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001950 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001951 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001952 {
1953 // Error message does not completely match behavior with ESSL 1.00, but
1954 // we want to steer developers towards only using constant expressions.
1955 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001956 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001957 }
1958 if (globalInitWarning)
1959 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001960 warning(
1961 line,
1962 "global variable initializers should be constant expressions "
1963 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1964 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001965 }
1966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301968 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1969 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001970 error(line, " cannot initialize this type of qualifier ",
1971 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001972 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001973 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001974
1975 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1976 intermSymbol->setLine(line);
1977
1978 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1979 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001980 assignError(line, "=", variable->getType(), initializer->getType());
Olli Etuahob60d30f2018-01-16 12:31:06 +02001981 return false;
1982 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001983
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (qualifier == EvqConst)
1985 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001986 // Save the constant folded value to the variable if possible.
1987 const TConstantUnion *constArray = initializer->getConstantValue();
1988 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301989 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001990 variable->shareConstPointer(constArray);
1991 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001992 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001993 ASSERT(*initNode == nullptr);
1994 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001995 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001996 }
1997 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001998
Olli Etuahob60d30f2018-01-16 12:31:06 +02001999 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002000 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002001 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002002 return true;
2003}
2004
2005TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002006 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002007 TIntermTyped *initializer,
2008 const TSourceLoc &loc)
2009{
2010 checkIsScalarBool(loc, pType);
2011 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002012 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002013 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002014 {
2015 // The initializer is valid. The init condition needs to have a node - either the
2016 // initializer node, or a constant node in case the initialized variable is const and won't
2017 // be recorded in the AST.
2018 if (initNode == nullptr)
2019 {
2020 return initializer;
2021 }
2022 else
2023 {
2024 TIntermDeclaration *declaration = new TIntermDeclaration();
2025 declaration->appendDeclarator(initNode);
2026 return declaration;
2027 }
2028 }
2029 return nullptr;
2030}
2031
2032TIntermNode *TParseContext::addLoop(TLoopType type,
2033 TIntermNode *init,
2034 TIntermNode *cond,
2035 TIntermTyped *expr,
2036 TIntermNode *body,
2037 const TSourceLoc &line)
2038{
2039 TIntermNode *node = nullptr;
2040 TIntermTyped *typedCond = nullptr;
2041 if (cond)
2042 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002043 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002044 typedCond = cond->getAsTyped();
2045 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002046 if (expr)
2047 {
2048 markStaticReadIfSymbol(expr);
2049 }
2050 // In case the loop body was not parsed as a block and contains a statement that simply refers
2051 // to a variable, we need to mark it as statically used.
2052 if (body)
2053 {
2054 markStaticReadIfSymbol(body);
2055 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002056 if (cond == nullptr || typedCond)
2057 {
Olli Etuahocce89652017-06-19 16:04:09 +03002058 if (type == ELoopDoWhile)
2059 {
2060 checkIsScalarBool(line, typedCond);
2061 }
2062 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2063 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2064 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2065 !typedCond->isVector()));
2066
Olli Etuaho3ec75682017-07-05 17:02:55 +03002067 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002068 node->setLine(line);
2069 return node;
2070 }
2071
Olli Etuahocce89652017-06-19 16:04:09 +03002072 ASSERT(type != ELoopDoWhile);
2073
Olli Etuaho914b79a2017-06-19 16:03:19 +03002074 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2075 ASSERT(declaration);
2076 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2077 ASSERT(declarator->getLeft()->getAsSymbolNode());
2078
2079 // The condition is a declaration. In the AST representation we don't support declarations as
2080 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2081 // the loop.
2082 TIntermBlock *block = new TIntermBlock();
2083
2084 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2085 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2086 block->appendStatement(declareCondition);
2087
2088 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2089 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002090 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002091 block->appendStatement(loop);
2092 loop->setLine(line);
2093 block->setLine(line);
2094 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095}
2096
Olli Etuahocce89652017-06-19 16:04:09 +03002097TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2098 TIntermNodePair code,
2099 const TSourceLoc &loc)
2100{
Olli Etuaho56229f12017-07-10 14:16:33 +03002101 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002102 // In case the conditional statements were not parsed as blocks and contain a statement that
2103 // simply refers to a variable, we need to mark them as statically used.
2104 if (code.node1)
2105 {
2106 markStaticReadIfSymbol(code.node1);
2107 }
2108 if (code.node2)
2109 {
2110 markStaticReadIfSymbol(code.node2);
2111 }
Olli Etuahocce89652017-06-19 16:04:09 +03002112
2113 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002114 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002115 {
2116 if (cond->getAsConstantUnion()->getBConst(0) == true)
2117 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002118 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002119 }
2120 else
2121 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002122 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002123 }
2124 }
2125
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002127 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002128 node->setLine(loc);
2129
2130 return node;
2131}
2132
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002133void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2134{
2135 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2136 typeSpecifier->getBasicType());
2137
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002138 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002139 {
2140 error(typeSpecifier->getLine(), "not supported", "first-class array");
2141 typeSpecifier->clearArrayness();
2142 }
2143}
2144
Martin Radev70866b82016-07-22 15:27:42 +03002145TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302146 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002147{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002148 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002149
Martin Radev70866b82016-07-22 15:27:42 +03002150 TPublicType returnType = typeSpecifier;
2151 returnType.qualifier = typeQualifier.qualifier;
2152 returnType.invariant = typeQualifier.invariant;
2153 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002154 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002155 returnType.precision = typeSpecifier.precision;
2156
2157 if (typeQualifier.precision != EbpUndefined)
2158 {
2159 returnType.precision = typeQualifier.precision;
2160 }
2161
Martin Radev4a9cd802016-09-01 16:51:51 +03002162 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2163 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2166 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002167
Martin Radev4a9cd802016-09-01 16:51:51 +03002168 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002169
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002170 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002171 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002172 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002173 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002175 returnType.clearArrayness();
2176 }
2177
Martin Radev70866b82016-07-22 15:27:42 +03002178 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002180 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002181 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002182 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002183 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002184
Martin Radev70866b82016-07-22 15:27:42 +03002185 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002187 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002188 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002189 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 }
2191 }
2192 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002193 {
Martin Radev70866b82016-07-22 15:27:42 +03002194 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002195 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002197 }
Martin Radev70866b82016-07-22 15:27:42 +03002198 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2199 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002200 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2202 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002203 }
Martin Radev70866b82016-07-22 15:27:42 +03002204 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002205 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002206 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002207 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002208 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002209 }
2210
2211 return returnType;
2212}
2213
Olli Etuaho856c4972016-08-08 11:38:39 +03002214void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2215 const TPublicType &type,
2216 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002217{
2218 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002219 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 {
2221 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002222 }
2223
2224 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2225 switch (qualifier)
2226 {
2227 case EvqVertexIn:
2228 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002229 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 {
2231 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002233 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 return;
2235 case EvqFragmentOut:
2236 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002237 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 {
2239 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002240 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002241 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 return;
2243 default:
2244 break;
2245 }
2246
2247 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2248 // restrictions.
2249 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002250 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2251 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002252 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2253 {
2254 error(qualifierLocation, "must use 'flat' interpolation here",
2255 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 }
2257
Martin Radev4a9cd802016-09-01 16:51:51 +03002258 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 {
2260 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2261 // These restrictions are only implied by the ESSL 3.00 spec, but
2262 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002263 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 {
2265 error(qualifierLocation, "cannot be an array of structures",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268 if (type.isStructureContainingArrays())
2269 {
2270 error(qualifierLocation, "cannot be a structure containing an array",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 if (type.isStructureContainingType(EbtStruct))
2274 {
2275 error(qualifierLocation, "cannot be a structure containing a structure",
2276 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 }
2278 if (type.isStructureContainingType(EbtBool))
2279 {
2280 error(qualifierLocation, "cannot be a structure containing a bool",
2281 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002282 }
2283 }
2284}
2285
Martin Radev2cc85b32016-08-05 16:22:53 +03002286void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2287{
2288 if (qualifier.getType() == QtStorage)
2289 {
2290 const TStorageQualifierWrapper &storageQualifier =
2291 static_cast<const TStorageQualifierWrapper &>(qualifier);
2292 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2293 !symbolTable.atGlobalLevel())
2294 {
2295 error(storageQualifier.getLine(),
2296 "Local variables can only use the const storage qualifier.",
2297 storageQualifier.getQualifierString().c_str());
2298 }
2299 }
2300}
2301
Olli Etuaho43364892017-02-13 16:00:12 +00002302void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 const TSourceLoc &location)
2304{
Jiajia Qinbc585152017-06-23 15:42:17 +08002305 const std::string reason(
2306 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2307 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002308 if (memoryQualifier.readonly)
2309 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002311 }
2312 if (memoryQualifier.writeonly)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002315 }
Martin Radev049edfa2016-11-11 14:35:37 +02002316 if (memoryQualifier.coherent)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002319 }
2320 if (memoryQualifier.restrictQualifier)
2321 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002322 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002323 }
2324 if (memoryQualifier.volatileQualifier)
2325 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002326 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002327 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002328}
2329
jchen104cdac9e2017-05-08 11:01:20 +08002330// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2331// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002332void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2333 const TSourceLoc &loc,
2334 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002335{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336 if (!IsAtomicCounter(type->getBasicType()))
2337 {
2338 return;
2339 }
2340
2341 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2342 : kAtomicCounterSize;
2343 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2344 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002345 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002346 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002347 {
2348 offset = bindingState.appendSpan(size);
2349 }
2350 else
2351 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002352 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002353 }
2354 if (offset == -1)
2355 {
2356 error(loc, "Offset overlapping", "atomic counter");
2357 return;
2358 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002359 layoutQualifier.offset = offset;
2360 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002361}
2362
Olli Etuaho454c34c2017-10-25 16:35:56 +03002363void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002364 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002365 TType *type)
2366{
2367 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2368 {
2369 if (type->isArray() && type->getOutermostArraySize() == 0u)
2370 {
2371 // Set size for the unsized geometry shader inputs if they are declared after a valid
2372 // input primitive declaration.
2373 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2374 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002375 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002376 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002377 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002378 }
2379 else
2380 {
2381 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2382 // An input can be declared without an array size if there is a previous layout
2383 // which specifies the size.
2384 error(location,
2385 "Missing a valid input primitive declaration before declaring an unsized "
2386 "array input",
2387 token);
2388 }
2389 }
2390 else if (type->isArray())
2391 {
2392 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2393 }
2394 else
2395 {
2396 error(location, "Geometry shader input variable must be declared as an array", token);
2397 }
2398 }
2399}
2400
Olli Etuaho13389b62016-10-16 11:48:18 +01002401TIntermDeclaration *TParseContext::parseSingleDeclaration(
2402 TPublicType &publicType,
2403 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002404 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002405{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002406 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002407 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2408 mDirectiveHandler.pragma().stdgl.invariantAll)
2409 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002410 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002411
2412 // The directive handler has already taken care of rejecting invalid uses of this pragma
2413 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2414 // affected variable declarations:
2415 //
2416 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2417 // elsewhere, in TranslatorGLSL.)
2418 //
2419 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2420 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2421 // the way this is currently implemented we have to enable this compiler option before
2422 // parsing the shader and determining the shading language version it uses. If this were
2423 // implemented as a post-pass, the workaround could be more targeted.
2424 //
2425 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2426 // the specification, but there are desktop OpenGL drivers that expect that this is the
2427 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2428 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2429 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002430 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002431 }
2432 }
2433
Olli Etuahofbb1c792018-01-19 16:26:59 +02002434 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002435
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002436 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2437 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002438
Jamie Madill50cf2be2018-06-15 09:46:57 -04002439 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002440 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002441
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002443 if (emptyDeclaration)
2444 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002445 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 // In most cases we don't need to create a symbol node for an empty declaration.
2447 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002448 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002449 {
Olli Etuaho195be942017-12-04 23:40:14 +02002450 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002451 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002452 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 }
jchen104cdac9e2017-05-08 11:01:20 +08002454 else if (IsAtomicCounter(publicType.getBasicType()))
2455 {
2456 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2457 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002458 }
2459 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002460 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002461 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002462
Olli Etuahob60d30f2018-01-16 12:31:06 +02002463 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002464
Olli Etuahob60d30f2018-01-16 12:31:06 +02002465 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002466
Olli Etuaho2935c582015-04-08 14:32:06 +03002467 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002468 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002469 {
Olli Etuaho195be942017-12-04 23:40:14 +02002470 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002472 }
2473
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002474 TIntermDeclaration *declaration = new TIntermDeclaration();
2475 declaration->setLine(identifierOrTypeLocation);
2476 if (symbol)
2477 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002478 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 declaration->appendDeclarator(symbol);
2480 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002481 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002482}
2483
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002484TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2485 TPublicType &elementType,
2486 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002487 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002488 const TSourceLoc &indexLocation,
2489 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002490{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002491 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002492
Olli Etuaho55bde912017-10-25 13:41:13 +03002493 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002494 identifierLocation);
2495
Olli Etuaho55bde912017-10-25 13:41:13 +03002496 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002497
Olli Etuaho55bde912017-10-25 13:41:13 +03002498 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuahob60d30f2018-01-16 12:31:06 +02002500 TType *arrayType = new TType(elementType);
2501 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002502
Olli Etuahofbb1c792018-01-19 16:26:59 +02002503 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002504
Olli Etuahob60d30f2018-01-16 12:31:06 +02002505 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002506
Olli Etuahob60d30f2018-01-16 12:31:06 +02002507 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002508
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 TIntermDeclaration *declaration = new TIntermDeclaration();
2510 declaration->setLine(identifierLocation);
2511
Olli Etuaho195be942017-12-04 23:40:14 +02002512 TVariable *variable = nullptr;
2513 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 {
Olli Etuaho195be942017-12-04 23:40:14 +02002515 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002516 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 declaration->appendDeclarator(symbol);
2518 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002519
Olli Etuaho13389b62016-10-16 11:48:18 +01002520 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002521}
2522
Olli Etuaho13389b62016-10-16 11:48:18 +01002523TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2524 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002525 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002526 const TSourceLoc &initLocation,
2527 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002528{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002529 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002530
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002531 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2532 identifierLocation);
2533
2534 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002535
Olli Etuaho13389b62016-10-16 11:48:18 +01002536 TIntermDeclaration *declaration = new TIntermDeclaration();
2537 declaration->setLine(identifierLocation);
2538
2539 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002540 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002541 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002542 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002543 if (initNode)
2544 {
2545 declaration->appendDeclarator(initNode);
2546 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002547 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002548 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002549}
2550
Olli Etuaho13389b62016-10-16 11:48:18 +01002551TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002552 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002553 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002554 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002556 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 const TSourceLoc &initLocation,
2558 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002559{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002560 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002561
Olli Etuaho55bde912017-10-25 13:41:13 +03002562 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002563 identifierLocation);
2564
Olli Etuaho55bde912017-10-25 13:41:13 +03002565 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002566
Olli Etuaho55bde912017-10-25 13:41:13 +03002567 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002568
Olli Etuahob60d30f2018-01-16 12:31:06 +02002569 TType *arrayType = new TType(elementType);
2570 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002571
Olli Etuaho13389b62016-10-16 11:48:18 +01002572 TIntermDeclaration *declaration = new TIntermDeclaration();
2573 declaration->setLine(identifierLocation);
2574
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002577 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002578 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002579 if (initNode)
2580 {
2581 declaration->appendDeclarator(initNode);
2582 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002583 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002584
2585 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002586}
2587
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002588TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002589 const TTypeQualifierBuilder &typeQualifierBuilder,
2590 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002591 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002592 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002593{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002594 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002595
Martin Radev70866b82016-07-22 15:27:42 +03002596 if (!typeQualifier.invariant)
2597 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002598 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002599 return nullptr;
2600 }
2601 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2602 {
2603 return nullptr;
2604 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002605 if (!symbol)
2606 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002607 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002608 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002609 }
Martin Radev70866b82016-07-22 15:27:42 +03002610 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 {
Martin Radev70866b82016-07-22 15:27:42 +03002612 error(identifierLoc, "invariant declaration specifies qualifier",
2613 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002614 }
Martin Radev70866b82016-07-22 15:27:42 +03002615 if (typeQualifier.precision != EbpUndefined)
2616 {
2617 error(identifierLoc, "invariant declaration specifies precision",
2618 getPrecisionString(typeQualifier.precision));
2619 }
2620 if (!typeQualifier.layoutQualifier.isEmpty())
2621 {
2622 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2623 }
2624
2625 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002626 if (!variable)
2627 {
2628 return nullptr;
2629 }
Martin Radev70866b82016-07-22 15:27:42 +03002630 const TType &type = variable->getType();
2631
2632 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2633 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002634 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002635
Olli Etuaho76b2c382018-03-19 15:51:29 +02002636 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002637
Olli Etuaho195be942017-12-04 23:40:14 +02002638 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002639 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002640
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002641 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002642}
2643
Olli Etuaho13389b62016-10-16 11:48:18 +01002644void TParseContext::parseDeclarator(TPublicType &publicType,
2645 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002646 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002647 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002648{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002649 // If the declaration starting this declarator list was empty (example: int,), some checks were
2650 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002651 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002652 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002653 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2654 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002655 }
2656
Olli Etuaho856c4972016-08-08 11:38:39 +03002657 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002658
Olli Etuahob60d30f2018-01-16 12:31:06 +02002659 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002660
Olli Etuahofbb1c792018-01-19 16:26:59 +02002661 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002662
Olli Etuahob60d30f2018-01-16 12:31:06 +02002663 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002664
Olli Etuahob60d30f2018-01-16 12:31:06 +02002665 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002666
Olli Etuaho195be942017-12-04 23:40:14 +02002667 TVariable *variable = nullptr;
2668 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 {
Olli Etuaho195be942017-12-04 23:40:14 +02002670 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002671 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002672 declarationOut->appendDeclarator(symbol);
2673 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002674}
2675
Olli Etuaho55bde912017-10-25 13:41:13 +03002676void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002678 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002679 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002680 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002681 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002682{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002683 // If the declaration starting this declarator list was empty (example: int,), some checks were
2684 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002685 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002686 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002687 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002688 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002689 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002690
Olli Etuaho55bde912017-10-25 13:41:13 +03002691 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002692
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002694 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002695 TType *arrayType = new TType(elementType);
2696 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002697
Olli Etuahofbb1c792018-01-19 16:26:59 +02002698 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002699
Olli Etuahob60d30f2018-01-16 12:31:06 +02002700 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002701
Olli Etuahob60d30f2018-01-16 12:31:06 +02002702 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002703
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002704 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002705 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002706 {
Olli Etuaho195be942017-12-04 23:40:14 +02002707 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002708 symbol->setLine(identifierLocation);
2709 declarationOut->appendDeclarator(symbol);
2710 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002711 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002712}
2713
Olli Etuaho13389b62016-10-16 11:48:18 +01002714void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2715 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002716 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002717 const TSourceLoc &initLocation,
2718 TIntermTyped *initializer,
2719 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002720{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002721 // If the declaration starting this declarator list was empty (example: int,), some checks were
2722 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002723 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002724 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002725 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2726 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002727 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002728
Olli Etuaho856c4972016-08-08 11:38:39 +03002729 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002730
Olli Etuaho13389b62016-10-16 11:48:18 +01002731 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002732 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002733 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002734 {
2735 //
2736 // build the intermediate representation
2737 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002738 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002739 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002741 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002742 }
2743}
2744
Olli Etuaho55bde912017-10-25 13:41:13 +03002745void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002746 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002747 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002749 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002750 const TSourceLoc &initLocation,
2751 TIntermTyped *initializer,
2752 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002753{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754 // If the declaration starting this declarator list was empty (example: int,), some checks were
2755 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002756 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002758 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002759 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002760 }
2761
Olli Etuaho55bde912017-10-25 13:41:13 +03002762 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763
Olli Etuaho55bde912017-10-25 13:41:13 +03002764 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765
Olli Etuahob60d30f2018-01-16 12:31:06 +02002766 TType *arrayType = new TType(elementType);
2767 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002768
2769 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002770 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002771 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002772 {
2773 if (initNode)
2774 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002775 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002776 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002777 }
2778}
2779
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002780TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2781{
2782 // It's simpler to parse an empty statement as a constant expression rather than having a
2783 // different type of node just for empty statements, that will be pruned from the AST anyway.
2784 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2785 node->setLine(location);
2786 return node;
2787}
2788
jchen104cdac9e2017-05-08 11:01:20 +08002789void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2790 const TSourceLoc &location)
2791{
2792 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2793 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2794 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2795 {
2796 error(location, "Requires both binding and offset", "layout");
2797 return;
2798 }
2799 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2800}
2801
Olli Etuahocce89652017-06-19 16:04:09 +03002802void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2803 const TPublicType &type,
2804 const TSourceLoc &loc)
2805{
2806 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2807 !getFragmentPrecisionHigh())
2808 {
2809 error(loc, "precision is not supported in fragment shader", "highp");
2810 }
2811
2812 if (!CanSetDefaultPrecisionOnType(type))
2813 {
2814 error(loc, "illegal type argument for default precision qualifier",
2815 getBasicString(type.getBasicType()));
2816 return;
2817 }
2818 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2819}
2820
Shaob5cc1192017-07-06 10:47:20 +08002821bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2822{
2823 switch (typeQualifier.layoutQualifier.primitiveType)
2824 {
2825 case EptLines:
2826 case EptLinesAdjacency:
2827 case EptTriangles:
2828 case EptTrianglesAdjacency:
2829 return typeQualifier.qualifier == EvqGeometryIn;
2830
2831 case EptLineStrip:
2832 case EptTriangleStrip:
2833 return typeQualifier.qualifier == EvqGeometryOut;
2834
2835 case EptPoints:
2836 return true;
2837
2838 default:
2839 UNREACHABLE();
2840 return false;
2841 }
2842}
2843
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002844void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2845 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002846{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002847 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002848 {
2849 error(line,
2850 "Array size or input primitive declaration doesn't match the size of earlier sized "
2851 "array inputs.",
2852 "layout");
2853 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002854}
2855
Shaob5cc1192017-07-06 10:47:20 +08002856bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2857{
2858 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2859
2860 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2861
2862 if (layoutQualifier.maxVertices != -1)
2863 {
2864 error(typeQualifier.line,
2865 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2866 return false;
2867 }
2868
2869 // Set mGeometryInputPrimitiveType if exists
2870 if (layoutQualifier.primitiveType != EptUndefined)
2871 {
2872 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2873 {
2874 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2875 return false;
2876 }
2877
2878 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2879 {
2880 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002881 setGeometryShaderInputArraySize(
2882 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2883 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002884 }
2885 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2886 {
2887 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2888 "layout");
2889 return false;
2890 }
2891 }
2892
2893 // Set mGeometryInvocations if exists
2894 if (layoutQualifier.invocations > 0)
2895 {
2896 if (mGeometryShaderInvocations == 0)
2897 {
2898 mGeometryShaderInvocations = layoutQualifier.invocations;
2899 }
2900 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2901 {
2902 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2903 "layout");
2904 return false;
2905 }
2906 }
2907
2908 return true;
2909}
2910
2911bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2912{
2913 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2914
2915 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2916
2917 if (layoutQualifier.invocations > 0)
2918 {
2919 error(typeQualifier.line,
2920 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2921 return false;
2922 }
2923
2924 // Set mGeometryOutputPrimitiveType if exists
2925 if (layoutQualifier.primitiveType != EptUndefined)
2926 {
2927 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2928 {
2929 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2930 return false;
2931 }
2932
2933 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2934 {
2935 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2936 }
2937 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2938 {
2939 error(typeQualifier.line,
2940 "primitive doesn't match earlier output primitive declaration", "layout");
2941 return false;
2942 }
2943 }
2944
2945 // Set mGeometryMaxVertices if exists
2946 if (layoutQualifier.maxVertices > -1)
2947 {
2948 if (mGeometryShaderMaxVertices == -1)
2949 {
2950 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2951 }
2952 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2953 {
2954 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2955 "layout");
2956 return false;
2957 }
2958 }
2959
2960 return true;
2961}
2962
Martin Radev70866b82016-07-22 15:27:42 +03002963void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002964{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002965 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002966 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002967
Martin Radev70866b82016-07-22 15:27:42 +03002968 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2969 typeQualifier.line);
2970
Jamie Madillc2128ff2016-07-04 10:26:17 -04002971 // It should never be the case, but some strange parser errors can send us here.
2972 if (layoutQualifier.isEmpty())
2973 {
2974 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002975 return;
2976 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002977
Martin Radev802abe02016-08-04 17:48:32 +03002978 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 {
Olli Etuaho43364892017-02-13 16:00:12 +00002980 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002981 return;
2982 }
2983
Olli Etuaho43364892017-02-13 16:00:12 +00002984 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2985
2986 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002987
2988 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2989
Andrei Volykhina5527072017-03-22 16:46:30 +03002990 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2991
jchen104cdac9e2017-05-08 11:01:20 +08002992 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2993
Qin Jiajiaca68d982017-09-18 16:41:56 +08002994 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2995 typeQualifier.qualifier);
2996
Martin Radev802abe02016-08-04 17:48:32 +03002997 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002998 {
Martin Radev802abe02016-08-04 17:48:32 +03002999 if (mComputeShaderLocalSizeDeclared &&
3000 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3001 {
3002 error(typeQualifier.line, "Work group size does not match the previous declaration",
3003 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003004 return;
3005 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003006
Martin Radev802abe02016-08-04 17:48:32 +03003007 if (mShaderVersion < 310)
3008 {
3009 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003010 return;
3011 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003012
Martin Radev4c4c8e72016-08-04 12:25:34 +03003013 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003014 {
3015 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003016 return;
3017 }
3018
3019 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003020 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003021
3022 const TConstantUnion *maxComputeWorkGroupSizeData =
3023 maxComputeWorkGroupSize->getConstPointer();
3024
3025 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3026 {
3027 if (layoutQualifier.localSize[i] != -1)
3028 {
3029 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3030 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3031 if (mComputeShaderLocalSize[i] < 1 ||
3032 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3033 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003034 std::stringstream reasonStream;
3035 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3036 << maxComputeWorkGroupSizeValue;
3037 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003038
Olli Etuaho4de340a2016-12-16 09:32:03 +00003039 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003040 return;
3041 }
3042 }
3043 }
3044
3045 mComputeShaderLocalSizeDeclared = true;
3046 }
Shaob5cc1192017-07-06 10:47:20 +08003047 else if (typeQualifier.qualifier == EvqGeometryIn)
3048 {
3049 if (mShaderVersion < 310)
3050 {
3051 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3052 return;
3053 }
3054
3055 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3056 {
3057 return;
3058 }
3059 }
3060 else if (typeQualifier.qualifier == EvqGeometryOut)
3061 {
3062 if (mShaderVersion < 310)
3063 {
3064 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3065 "layout");
3066 return;
3067 }
3068
3069 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3070 {
3071 return;
3072 }
3073 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003074 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3075 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003076 {
3077 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3078 // specification.
3079 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3080 {
3081 error(typeQualifier.line, "Number of views does not match the previous declaration",
3082 "layout");
3083 return;
3084 }
3085
3086 if (layoutQualifier.numViews == -1)
3087 {
3088 error(typeQualifier.line, "No num_views specified", "layout");
3089 return;
3090 }
3091
3092 if (layoutQualifier.numViews > mMaxNumViews)
3093 {
3094 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3095 "layout");
3096 return;
3097 }
3098
3099 mNumViews = layoutQualifier.numViews;
3100 }
Martin Radev802abe02016-08-04 17:48:32 +03003101 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003102 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003103 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003104 {
Martin Radev802abe02016-08-04 17:48:32 +03003105 return;
3106 }
3107
Jiajia Qinbc585152017-06-23 15:42:17 +08003108 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003109 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003110 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003111 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003112 return;
3113 }
3114
3115 if (mShaderVersion < 300)
3116 {
3117 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3118 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003119 return;
3120 }
3121
Olli Etuaho09b04a22016-12-15 13:30:26 +00003122 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003123
3124 if (layoutQualifier.matrixPacking != EmpUnspecified)
3125 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003126 if (typeQualifier.qualifier == EvqUniform)
3127 {
3128 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3129 }
3130 else if (typeQualifier.qualifier == EvqBuffer)
3131 {
3132 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3133 }
Martin Radev802abe02016-08-04 17:48:32 +03003134 }
3135
3136 if (layoutQualifier.blockStorage != EbsUnspecified)
3137 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003138 if (typeQualifier.qualifier == EvqUniform)
3139 {
3140 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3141 }
3142 else if (typeQualifier.qualifier == EvqBuffer)
3143 {
3144 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3145 }
Martin Radev802abe02016-08-04 17:48:32 +03003146 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003147 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003148}
3149
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003150TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3151 const TFunction &function,
3152 const TSourceLoc &location,
3153 bool insertParametersToSymbolTable)
3154{
Olli Etuahobed35d72017-12-20 16:36:26 +02003155 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003156
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003157 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003158 prototype->setLine(location);
3159
3160 for (size_t i = 0; i < function.getParamCount(); i++)
3161 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003162 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003163
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003164 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3165 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003166 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003167 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003168 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003170 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003171 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003172 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003173 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003174 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003175 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003176 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003177 }
3178 else
3179 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003180 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003181 {
3182 error(location, "function parameter array must be sized at compile time", "[]");
3183 // We don't need to size the arrays since the parameter is unnamed and hence
3184 // inaccessible.
3185 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187 }
3188 return prototype;
3189}
3190
Olli Etuaho16c745a2017-01-16 17:02:27 +00003191TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3192 const TFunction &parsedFunction,
3193 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003194{
Olli Etuaho476197f2016-10-11 13:59:08 +01003195 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3196 // first declaration. Either way the instance in the symbol table is used to track whether the
3197 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003198 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003199 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003200 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3201
3202 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003203 {
3204 // ESSL 1.00.17 section 4.2.7.
3205 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3206 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003207 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003208
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003209 TIntermFunctionPrototype *prototype =
3210 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003211
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003212 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003213
3214 if (!symbolTable.atGlobalLevel())
3215 {
3216 // ESSL 3.00.4 section 4.2.4.
3217 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003218 }
3219
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003220 return prototype;
3221}
3222
Olli Etuaho336b1472016-10-05 16:37:55 +01003223TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003224 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003225 TIntermBlock *functionBody,
3226 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003227{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003228 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3230 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003231 error(location,
3232 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003233 }
3234
Olli Etuahof51fdd22016-10-03 10:03:40 +01003235 if (functionBody == nullptr)
3236 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003237 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003238 functionBody->setLine(location);
3239 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003240 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003241 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003242 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003243
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003245 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246}
3247
Olli Etuaho476197f2016-10-11 13:59:08 +01003248void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003249 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003250 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003251{
Olli Etuaho476197f2016-10-11 13:59:08 +01003252 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003253
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003254 bool wasDefined = false;
3255 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3256 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003257 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003258 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003259 }
Jamie Madill185fb402015-06-12 15:48:48 -04003260
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003261 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003262 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003263 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003264
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003265 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003266 setLoopNestingLevel(0);
3267}
3268
Jamie Madillb98c3a82015-07-23 14:26:04 -04003269TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003270{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003271 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003272 // We don't know at this point whether this is a function definition or a prototype.
3273 // The definition production code will check for redefinitions.
3274 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003275 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303276
Olli Etuahod80f2942017-11-06 12:44:45 +02003277 for (size_t i = 0u; i < function->getParamCount(); ++i)
3278 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003279 const TVariable *param = function->getParam(i);
3280 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003281 {
3282 // ESSL 3.00.6 section 12.10.
3283 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003284 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003285 }
3286 }
3287
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003288 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303289 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003290 const UnmangledBuiltIn *builtIn =
3291 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3292 if (builtIn &&
3293 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3294 {
3295 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3296 // functions. Therefore overloading or redefining builtin functions is an error.
3297 error(location, "Name of a built-in function cannot be redeclared as function",
3298 function->name());
3299 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303300 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003301 else
3302 {
3303 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3304 // this applies to redeclarations as well.
3305 const TSymbol *builtIn =
3306 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3307 if (builtIn)
3308 {
3309 error(location, "built-in functions cannot be redefined", function->name());
3310 }
3311 }
3312
3313 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3314 // here.
3315 const TFunction *prevDec =
3316 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3317 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003318 {
3319 if (prevDec->getReturnType() != function->getReturnType())
3320 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003321 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003322 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003323 }
3324 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3325 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003326 if (prevDec->getParam(i)->getType().getQualifier() !=
3327 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003328 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003329 error(location,
3330 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003331 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003332 }
3333 }
3334 }
3335
Jamie Madill185fb402015-06-12 15:48:48 -04003336 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003337 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3338 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003339 if (prevSym)
3340 {
3341 if (!prevSym->isFunction())
3342 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003343 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003344 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003345 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003346 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003347 // Parsing is at the inner scope level of the function's arguments and body statement at this
3348 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3349 // scope.
3350 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003351
Olli Etuaho78d13742017-01-18 13:06:10 +00003352 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003353 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003354 {
3355 if (function->getParamCount() > 0)
3356 {
3357 error(location, "function cannot take any parameter(s)", "main");
3358 }
3359 if (function->getReturnType().getBasicType() != EbtVoid)
3360 {
3361 error(location, "main function cannot return a value",
3362 function->getReturnType().getBasicString());
3363 }
3364 }
3365
Jamie Madill185fb402015-06-12 15:48:48 -04003366 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003367 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3368 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003369 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3370 //
3371 return function;
3372}
3373
Olli Etuaho9de84a52016-06-14 17:36:01 +03003374TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003375 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003376 const TSourceLoc &location)
3377{
3378 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3379 {
3380 error(location, "no qualifiers allowed for function return",
3381 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003382 }
3383 if (!type.layoutQualifier.isEmpty())
3384 {
3385 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003386 }
jchen10cc2a10e2017-05-03 14:05:12 +08003387 // make sure an opaque type is not involved as well...
3388 std::string reason(getBasicString(type.getBasicType()));
3389 reason += "s can't be function return values";
3390 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003391 if (mShaderVersion < 300)
3392 {
3393 // Array return values are forbidden, but there's also no valid syntax for declaring array
3394 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003395 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003396
3397 if (type.isStructureContainingArrays())
3398 {
3399 // ESSL 1.00.17 section 6.1 Function Definitions
Olli Etuaho72e35892018-06-20 11:43:08 +03003400 TInfoSinkBase typeString;
3401 typeString << TType(type);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003402 error(location, "structures containing arrays can't be function return values",
Olli Etuaho72e35892018-06-20 11:43:08 +03003403 typeString.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003404 }
3405 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003406
3407 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003408 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003409}
3410
Olli Etuaho697bf652018-02-16 11:50:54 +02003411TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3412 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003413{
Olli Etuaho697bf652018-02-16 11:50:54 +02003414 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003415}
3416
Olli Etuaho95ed1942018-02-01 14:01:19 +02003417TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003418{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003419 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003420 {
3421 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3422 "[]");
3423 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003424 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003425 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003426 error(publicType.getLine(), "constructor can't be a structure definition",
3427 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003428 }
3429
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003430 TType *type = new TType(publicType);
3431 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003432 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003433 error(publicType.getLine(), "cannot construct this type",
3434 getBasicString(publicType.getBasicType()));
3435 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003436 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003437 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003438}
3439
Olli Etuaho55bde912017-10-25 13:41:13 +03003440void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3441 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003442 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003443 TType *arrayType)
3444{
3445 if (arrayType->isUnsizedArray())
3446 {
3447 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003448 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003449 }
3450}
3451
3452TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003453 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003454 const TSourceLoc &nameLoc)
3455{
Olli Etuaho55bde912017-10-25 13:41:13 +03003456 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003457 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003458 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003459 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003460 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003461 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003462 checkIsNotReserved(nameLoc, name);
3463 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003464 return param;
3465}
3466
Olli Etuaho55bde912017-10-25 13:41:13 +03003467TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003468 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003469 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003470{
Olli Etuaho55bde912017-10-25 13:41:13 +03003471 TType *type = new TType(publicType);
3472 return parseParameterDeclarator(type, name, nameLoc);
3473}
3474
Olli Etuahofbb1c792018-01-19 16:26:59 +02003475TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003476 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003477 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003478 const TSourceLoc &arrayLoc,
3479 TPublicType *elementType)
3480{
3481 checkArrayElementIsNotArray(arrayLoc, *elementType);
3482 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003483 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003484 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003485}
3486
Olli Etuaho95ed1942018-02-01 14:01:19 +02003487bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3488 const TIntermSequence &arguments,
3489 TType type,
3490 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003491{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003492 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003493 {
3494 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3495 return false;
3496 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003497 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003498 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003499 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003500 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003501 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3502 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003503 {
3504 error(line, "constructing from a non-dereferenced array", "constructor");
3505 return false;
3506 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003507 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003508 {
3509 if (dimensionalityFromElement == 1u)
3510 {
3511 error(line, "implicitly sized array of arrays constructor argument is not an array",
3512 "constructor");
3513 }
3514 else
3515 {
3516 error(line,
3517 "implicitly sized array of arrays constructor argument dimensionality is too "
3518 "low",
3519 "constructor");
3520 }
3521 return false;
3522 }
3523 }
3524 return true;
3525}
3526
Jamie Madillb98c3a82015-07-23 14:26:04 -04003527// This function is used to test for the correctness of the parameters passed to various constructor
3528// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003529//
Olli Etuaho856c4972016-08-08 11:38:39 +03003530// 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 +00003531//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003532TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003533{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003534 TType type = fnCall->constructorType();
3535 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003536 if (type.isUnsizedArray())
3537 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003539 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003540 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003541 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003542 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003543 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003544 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003545 if (type.getOutermostArraySize() == 0u)
3546 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003547 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003548 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003549 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003550 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003551 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003552 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003553 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 }
3555 }
3556 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003557 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003558
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003559 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003560 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003561 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003562 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003563
Olli Etuaho95ed1942018-02-01 14:01:19 +02003564 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003565 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566
Olli Etuaho765924f2018-01-04 12:48:36 +02003567 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003568}
3569
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003570//
3571// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003572// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003573//
Olli Etuaho13389b62016-10-16 11:48:18 +01003574TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003575 const TTypeQualifierBuilder &typeQualifierBuilder,
3576 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003577 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003578 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003579 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003580 const TSourceLoc &instanceLine,
3581 TIntermTyped *arrayIndex,
3582 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583{
Olli Etuaho856c4972016-08-08 11:38:39 +03003584 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003585
Olli Etuaho77ba4082016-12-16 12:01:18 +00003586 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003587
Jiajia Qinbc585152017-06-23 15:42:17 +08003588 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003589 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003590 error(typeQualifier.line,
3591 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3592 "3.10",
3593 getQualifierString(typeQualifier.qualifier));
3594 }
3595 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3596 {
3597 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003598 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599 }
3600
Martin Radev70866b82016-07-22 15:27:42 +03003601 if (typeQualifier.invariant)
3602 {
3603 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3604 }
3605
Jiajia Qinbc585152017-06-23 15:42:17 +08003606 if (typeQualifier.qualifier != EvqBuffer)
3607 {
3608 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3609 }
Olli Etuaho43364892017-02-13 16:00:12 +00003610
jchen10af713a22017-04-19 09:10:56 +08003611 // add array index
3612 unsigned int arraySize = 0;
3613 if (arrayIndex != nullptr)
3614 {
3615 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3616 }
3617
3618 if (mShaderVersion < 310)
3619 {
3620 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3621 }
3622 else
3623 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003624 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3625 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003626 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003627
Andrei Volykhina5527072017-03-22 16:46:30 +03003628 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3629
Jamie Madill099c0f32013-06-20 11:55:52 -04003630 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003631 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003632 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3633 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003634
Jamie Madill099c0f32013-06-20 11:55:52 -04003635 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3636 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003637 if (typeQualifier.qualifier == EvqUniform)
3638 {
3639 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3640 }
3641 else if (typeQualifier.qualifier == EvqBuffer)
3642 {
3643 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3644 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003645 }
3646
Jamie Madill1566ef72013-06-20 11:55:54 -04003647 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3648 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003649 if (typeQualifier.qualifier == EvqUniform)
3650 {
3651 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3652 }
3653 else if (typeQualifier.qualifier == EvqBuffer)
3654 {
3655 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3656 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003657 }
3658
Olli Etuaho856c4972016-08-08 11:38:39 +03003659 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003660
Martin Radev2cc85b32016-08-05 16:22:53 +03003661 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3662
Jamie Madill98493dd2013-07-08 14:39:03 -04003663 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303664 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3665 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003666 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303667 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003668 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303669 {
jchen10cc2a10e2017-05-03 14:05:12 +08003670 std::string reason("unsupported type - ");
3671 reason += fieldType->getBasicString();
3672 reason += " types are not allowed in interface blocks";
3673 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003674 }
3675
Jamie Madill98493dd2013-07-08 14:39:03 -04003676 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003677 switch (qualifier)
3678 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003680 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003681 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003682 if (typeQualifier.qualifier == EvqBuffer)
3683 {
3684 error(field->line(), "invalid qualifier on shader storage block member",
3685 getQualifierString(qualifier));
3686 }
3687 break;
3688 case EvqBuffer:
3689 if (typeQualifier.qualifier == EvqUniform)
3690 {
3691 error(field->line(), "invalid qualifier on uniform block member",
3692 getQualifierString(qualifier));
3693 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694 break;
3695 default:
3696 error(field->line(), "invalid qualifier on interface block member",
3697 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003698 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003699 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003700
Martin Radev70866b82016-07-22 15:27:42 +03003701 if (fieldType->isInvariant())
3702 {
3703 error(field->line(), "invalid qualifier on interface block member", "invariant");
3704 }
3705
Jamie Madilla5efff92013-06-06 11:56:47 -04003706 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003707 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003708 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003709 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003710
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003712 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003713 error(field->line(), "invalid layout qualifier: cannot be used here",
3714 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003715 }
3716
Jamie Madill98493dd2013-07-08 14:39:03 -04003717 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003718 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003719 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003720 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003721 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003722 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003723 warning(field->line(),
3724 "extraneous layout qualifier: only has an effect on matrix types",
3725 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003726 }
3727
Jamie Madill98493dd2013-07-08 14:39:03 -04003728 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003729
Olli Etuahoebee5b32017-11-23 12:56:32 +02003730 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3731 typeQualifier.qualifier != EvqBuffer)
3732 {
3733 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3734 checkIsNotUnsizedArray(field->line(),
3735 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003736 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003737 }
3738
Jiajia Qinbc585152017-06-23 15:42:17 +08003739 if (typeQualifier.qualifier == EvqBuffer)
3740 {
3741 // set memory qualifiers
3742 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3743 // qualified with a memory qualifier, it is as if all of its members were declared with
3744 // the same memory qualifier.
3745 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3746 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3747 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3748 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3749 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3750 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3751 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3752 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3753 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3754 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3755 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003756 }
3757
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003758 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003759 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003760 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003761 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003762 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003763 }
3764
Olli Etuahob60d30f2018-01-16 12:31:06 +02003765 TType *interfaceBlockType =
3766 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003767 if (arrayIndex != nullptr)
3768 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003769 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003770 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003771
Olli Etuaho195be942017-12-04 23:40:14 +02003772 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003773 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3774 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003775 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003776 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003777 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003778
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003779 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003780 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003781 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003782 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3783 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003784 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003785 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003786
3787 // set parent pointer of the field variable
3788 fieldType->setInterfaceBlock(interfaceBlock);
3789
Olli Etuahob60d30f2018-01-16 12:31:06 +02003790 fieldType->setQualifier(typeQualifier.qualifier);
3791
Olli Etuaho195be942017-12-04 23:40:14 +02003792 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003793 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003794 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303795 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003796 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003797 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003798 }
3799 }
3800 }
3801 else
3802 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003803 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003804
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003805 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003806 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303807 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003808 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003809 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003810 }
3811
Olli Etuaho195be942017-12-04 23:40:14 +02003812 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3813 blockSymbol->setLine(typeQualifier.line);
3814 TIntermDeclaration *declaration = new TIntermDeclaration();
3815 declaration->appendDeclarator(blockSymbol);
3816 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003817
3818 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003819 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820}
3821
Olli Etuahofbb1c792018-01-19 16:26:59 +02003822void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3823 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003824{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003825 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003826
3827 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003828 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303829 if (mStructNestingLevel > 1)
3830 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003831 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003832 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003833}
3834
3835void TParseContext::exitStructDeclaration()
3836{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003837 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003838}
3839
Olli Etuaho8a176262016-08-16 14:23:01 +03003840void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003841{
Jamie Madillacb4b812016-11-07 13:50:29 -05003842 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303843 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003844 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003845 }
3846
Arun Patole7e7e68d2015-05-22 12:02:25 +05303847 if (field.type()->getBasicType() != EbtStruct)
3848 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003849 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003850 }
3851
3852 // We're already inside a structure definition at this point, so add
3853 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303854 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3855 {
Jamie Madill41a49272014-03-18 16:10:13 -04003856 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003857 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3858 {
3859 // This may happen in case there are nested struct definitions. While they are also
3860 // invalid GLSL, they don't cause a syntax error.
3861 reasonStream << "Struct nesting";
3862 }
3863 else
3864 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003865 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003866 }
3867 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003868 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003869 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003870 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003871 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872}
3873
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003874//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875// Parse an array index expression
3876//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003877TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3878 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303879 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003880{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003881 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3882 {
3883 if (baseExpression->getAsSymbolNode())
3884 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303885 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003886 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003887 }
3888 else
3889 {
3890 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3891 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003892
Olli Etuaho3ec75682017-07-05 17:02:55 +03003893 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003894 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003895
Jiawei Shaod8105a02017-08-08 09:54:36 +08003896 if (baseExpression->getQualifier() == EvqPerVertexIn)
3897 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003898 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003899 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3900 {
3901 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3902 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3903 }
3904 }
3905
Jamie Madill21c1e452014-12-29 11:33:41 -05003906 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3907
Olli Etuaho36b05142015-11-12 13:10:42 +02003908 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3909 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3910 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3911 // index is a constant expression.
3912 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3913 {
3914 if (baseExpression->isInterfaceBlock())
3915 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003916 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003917 switch (baseExpression->getQualifier())
3918 {
3919 case EvqPerVertexIn:
3920 break;
3921 case EvqUniform:
3922 case EvqBuffer:
3923 error(location,
3924 "array indexes for uniform block arrays and shader storage block arrays "
3925 "must be constant integral expressions",
3926 "[");
3927 break;
3928 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003929 // We can reach here only in error cases.
3930 ASSERT(mDiagnostics->numErrors() > 0);
3931 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003932 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003933 }
3934 else if (baseExpression->getQualifier() == EvqFragmentOut)
3935 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003936 error(location,
3937 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003938 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003939 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3940 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003941 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003942 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003943 }
3944
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003945 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003946 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003947 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3948 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3949 // constant fold expressions that are not constant expressions). The most compatible way to
3950 // handle this case is to report a warning instead of an error and force the index to be in
3951 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003952 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003953 int index = 0;
3954 if (indexConstantUnion->getBasicType() == EbtInt)
3955 {
3956 index = indexConstantUnion->getIConst(0);
3957 }
3958 else if (indexConstantUnion->getBasicType() == EbtUInt)
3959 {
3960 index = static_cast<int>(indexConstantUnion->getUConst(0));
3961 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003962
3963 int safeIndex = -1;
3964
Olli Etuahoebee5b32017-11-23 12:56:32 +02003965 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003966 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003967 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3968 safeIndex = 0;
3969 }
3970
3971 if (!baseExpression->getType().isUnsizedArray())
3972 {
3973 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003974 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003975 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003976 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003977 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3978 {
3979 outOfRangeError(outOfRangeIndexIsError, location,
3980 "array index for gl_FragData must be zero when "
3981 "GL_EXT_draw_buffers is disabled",
3982 "[]");
3983 safeIndex = 0;
3984 }
3985 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003986 }
3987 // Only do generic out-of-range check if similar error hasn't already been reported.
3988 if (safeIndex < 0)
3989 {
3990 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02003991 {
3992 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3993 baseExpression->getOutermostArraySize(),
3994 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003995 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003996 else if (baseExpression->isMatrix())
3997 {
3998 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3999 baseExpression->getType().getCols(),
4000 "matrix field selection out of range");
4001 }
4002 else
4003 {
4004 ASSERT(baseExpression->isVector());
4005 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4006 baseExpression->getType().getNominalSize(),
4007 "vector field selection out of range");
4008 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004009 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004010
Olli Etuahoebee5b32017-11-23 12:56:32 +02004011 ASSERT(safeIndex >= 0);
4012 // Data of constant unions can't be changed, because it may be shared with other
4013 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4014 // sanitized object.
4015 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4016 {
4017 TConstantUnion *safeConstantUnion = new TConstantUnion();
4018 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004019 indexExpression = new TIntermConstantUnion(
4020 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4021 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004023
Olli Etuahoebee5b32017-11-23 12:56:32 +02004024 TIntermBinary *node =
4025 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4026 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004027 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004028 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004030
Olli Etuaho94bbed12018-03-20 14:44:53 +02004031 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004032 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4033 node->setLine(location);
4034 // Indirect indexing can never be constant folded.
4035 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004036}
4037
Olli Etuahoebee5b32017-11-23 12:56:32 +02004038int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4039 const TSourceLoc &location,
4040 int index,
4041 int arraySize,
4042 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004043{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004044 // Should not reach here with an unsized / runtime-sized array.
4045 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004046 // A negative index should already have been checked.
4047 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004049 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004050 std::stringstream reasonStream;
4051 reasonStream << reason << " '" << index << "'";
4052 std::string token = reasonStream.str();
4053 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004054 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004055 }
4056 return index;
4057}
4058
Jamie Madillb98c3a82015-07-23 14:26:04 -04004059TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4060 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004061 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004062 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004063{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004064 if (baseExpression->isArray())
4065 {
4066 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004067 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004068 }
4069
4070 if (baseExpression->isVector())
4071 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004072 TVector<int> fieldOffsets;
4073 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4074 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004075 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004076 fieldOffsets.resize(1);
4077 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004079 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4080 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081
Olli Etuaho765924f2018-01-04 12:48:36 +02004082 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004083 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084 else if (baseExpression->getBasicType() == EbtStruct)
4085 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304086 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004087 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 {
4089 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004090 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004091 }
4092 else
4093 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004094 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004095 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004096 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004098 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004099 {
4100 fieldFound = true;
4101 break;
4102 }
4103 }
4104 if (fieldFound)
4105 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004106 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004108 TIntermBinary *node =
4109 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4110 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004111 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112 }
4113 else
4114 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004115 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004116 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 }
4118 }
4119 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004120 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004121 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304122 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004123 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004124 {
4125 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004126 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004127 }
4128 else
4129 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004130 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004131 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004132 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004133 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004134 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004135 {
4136 fieldFound = true;
4137 break;
4138 }
4139 }
4140 if (fieldFound)
4141 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004142 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004143 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004144 TIntermBinary *node =
4145 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4146 node->setLine(dotLocation);
4147 // Indexing interface blocks can never be constant folded.
4148 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004149 }
4150 else
4151 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004152 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004153 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004154 }
4155 }
4156 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004157 else
4158 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004159 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004160 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004161 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004162 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004163 }
4164 else
4165 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304166 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004167 " field selection requires structure, vector, or interface block on left hand "
4168 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004169 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004171 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004172 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004173}
4174
Olli Etuahofbb1c792018-01-19 16:26:59 +02004175TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004176 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004177{
Jamie Madill2f294c92017-11-20 14:47:26 -05004178 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004179
4180 if (qualifierType == "shared")
4181 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004182 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004183 {
4184 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4185 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004186 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004187 }
4188 else if (qualifierType == "packed")
4189 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004190 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004191 {
4192 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4193 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004194 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004195 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004196 else if (qualifierType == "std430")
4197 {
4198 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4199 qualifier.blockStorage = EbsStd430;
4200 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004201 else if (qualifierType == "std140")
4202 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004203 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004204 }
4205 else if (qualifierType == "row_major")
4206 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004207 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004208 }
4209 else if (qualifierType == "column_major")
4210 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004211 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004212 }
4213 else if (qualifierType == "location")
4214 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004215 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004216 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004218 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004219 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004220 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4221 {
4222 qualifier.yuv = true;
4223 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004224 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004225 else if (qualifierType == "rgba32f")
4226 {
4227 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4228 qualifier.imageInternalFormat = EiifRGBA32F;
4229 }
4230 else if (qualifierType == "rgba16f")
4231 {
4232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4233 qualifier.imageInternalFormat = EiifRGBA16F;
4234 }
4235 else if (qualifierType == "r32f")
4236 {
4237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4238 qualifier.imageInternalFormat = EiifR32F;
4239 }
4240 else if (qualifierType == "rgba8")
4241 {
4242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4243 qualifier.imageInternalFormat = EiifRGBA8;
4244 }
4245 else if (qualifierType == "rgba8_snorm")
4246 {
4247 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4248 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4249 }
4250 else if (qualifierType == "rgba32i")
4251 {
4252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4253 qualifier.imageInternalFormat = EiifRGBA32I;
4254 }
4255 else if (qualifierType == "rgba16i")
4256 {
4257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4258 qualifier.imageInternalFormat = EiifRGBA16I;
4259 }
4260 else if (qualifierType == "rgba8i")
4261 {
4262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4263 qualifier.imageInternalFormat = EiifRGBA8I;
4264 }
4265 else if (qualifierType == "r32i")
4266 {
4267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4268 qualifier.imageInternalFormat = EiifR32I;
4269 }
4270 else if (qualifierType == "rgba32ui")
4271 {
4272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4273 qualifier.imageInternalFormat = EiifRGBA32UI;
4274 }
4275 else if (qualifierType == "rgba16ui")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifRGBA16UI;
4279 }
4280 else if (qualifierType == "rgba8ui")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA8UI;
4284 }
4285 else if (qualifierType == "r32ui")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifR32UI;
4289 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004290 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4291 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.primitiveType = EptPoints;
4295 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004296 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4297 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004298 {
4299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4300 qualifier.primitiveType = EptLines;
4301 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004302 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4303 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 qualifier.primitiveType = EptLinesAdjacency;
4307 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004308 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4309 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004310 {
4311 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4312 qualifier.primitiveType = EptTriangles;
4313 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004314 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4315 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004316 {
4317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4318 qualifier.primitiveType = EptTrianglesAdjacency;
4319 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004320 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4321 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.primitiveType = EptLineStrip;
4325 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004326 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4327 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004328 {
4329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4330 qualifier.primitiveType = EptTriangleStrip;
4331 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004332
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004333 else
4334 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004335 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004336 }
4337
Jamie Madilla5efff92013-06-06 11:56:47 -04004338 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004339}
4340
Olli Etuahofbb1c792018-01-19 16:26:59 +02004341void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004342 const TSourceLoc &qualifierTypeLine,
4343 int intValue,
4344 const TSourceLoc &intValueLine,
4345 const std::string &intValueString,
4346 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004347 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004348{
Olli Etuaho856c4972016-08-08 11:38:39 +03004349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004350 if (intValue < 1)
4351 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004352 std::stringstream reasonStream;
4353 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4354 std::string reason = reasonStream.str();
4355 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004356 }
4357 (*localSize)[index] = intValue;
4358}
4359
Olli Etuaho09b04a22016-12-15 13:30:26 +00004360void TParseContext::parseNumViews(int intValue,
4361 const TSourceLoc &intValueLine,
4362 const std::string &intValueString,
4363 int *numViews)
4364{
4365 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4366 // specification.
4367 if (intValue < 1)
4368 {
4369 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4370 }
4371 *numViews = intValue;
4372}
4373
Shaob5cc1192017-07-06 10:47:20 +08004374void TParseContext::parseInvocations(int intValue,
4375 const TSourceLoc &intValueLine,
4376 const std::string &intValueString,
4377 int *numInvocations)
4378{
4379 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4380 // it doesn't make sense to accept invocations <= 0.
4381 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4382 {
4383 error(intValueLine,
4384 "out of range: invocations must be in the range of [1, "
4385 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4386 intValueString.c_str());
4387 }
4388 else
4389 {
4390 *numInvocations = intValue;
4391 }
4392}
4393
4394void TParseContext::parseMaxVertices(int intValue,
4395 const TSourceLoc &intValueLine,
4396 const std::string &intValueString,
4397 int *maxVertices)
4398{
4399 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4400 // it doesn't make sense to accept max_vertices < 0.
4401 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4402 {
4403 error(
4404 intValueLine,
4405 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4406 intValueString.c_str());
4407 }
4408 else
4409 {
4410 *maxVertices = intValue;
4411 }
4412}
4413
Olli Etuahofbb1c792018-01-19 16:26:59 +02004414TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004415 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004416 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304417 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004418{
Jamie Madill2f294c92017-11-20 14:47:26 -05004419 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004420
Martin Radev802abe02016-08-04 17:48:32 +03004421 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004422
Martin Radev802abe02016-08-04 17:48:32 +03004423 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004424 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004425 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004426 if (intValue < 0)
4427 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004428 error(intValueLine, "out of range: location must be non-negative",
4429 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004430 }
4431 else
4432 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004433 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004434 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004435 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004436 }
Olli Etuaho43364892017-02-13 16:00:12 +00004437 else if (qualifierType == "binding")
4438 {
4439 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4440 if (intValue < 0)
4441 {
4442 error(intValueLine, "out of range: binding must be non-negative",
4443 intValueString.c_str());
4444 }
4445 else
4446 {
4447 qualifier.binding = intValue;
4448 }
4449 }
jchen104cdac9e2017-05-08 11:01:20 +08004450 else if (qualifierType == "offset")
4451 {
4452 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4453 if (intValue < 0)
4454 {
4455 error(intValueLine, "out of range: offset must be non-negative",
4456 intValueString.c_str());
4457 }
4458 else
4459 {
4460 qualifier.offset = intValue;
4461 }
4462 }
Martin Radev802abe02016-08-04 17:48:32 +03004463 else if (qualifierType == "local_size_x")
4464 {
4465 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4466 &qualifier.localSize);
4467 }
4468 else if (qualifierType == "local_size_y")
4469 {
4470 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4471 &qualifier.localSize);
4472 }
4473 else if (qualifierType == "local_size_z")
4474 {
4475 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4476 &qualifier.localSize);
4477 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004478 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004479 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004480 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4481 {
4482 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4483 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004484 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004485 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4486 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004487 {
4488 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4489 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004490 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4491 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004492 {
4493 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4494 }
4495
Martin Radev802abe02016-08-04 17:48:32 +03004496 else
4497 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004498 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004499 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004500
Jamie Madilla5efff92013-06-06 11:56:47 -04004501 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004502}
4503
Olli Etuaho613b9592016-09-05 12:05:53 +03004504TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4505{
4506 return new TTypeQualifierBuilder(
4507 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4508 mShaderVersion);
4509}
4510
Olli Etuahocce89652017-06-19 16:04:09 +03004511TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4512 const TSourceLoc &loc)
4513{
4514 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4515 return new TStorageQualifierWrapper(qualifier, loc);
4516}
4517
4518TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4519{
4520 if (getShaderType() == GL_VERTEX_SHADER)
4521 {
4522 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4523 }
4524 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4525}
4526
4527TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4528{
4529 if (declaringFunction())
4530 {
4531 return new TStorageQualifierWrapper(EvqIn, loc);
4532 }
Shaob5cc1192017-07-06 10:47:20 +08004533
4534 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004535 {
Shaob5cc1192017-07-06 10:47:20 +08004536 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004537 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004538 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004539 {
4540 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4541 }
4542 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004543 }
Shaob5cc1192017-07-06 10:47:20 +08004544 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004545 {
Shaob5cc1192017-07-06 10:47:20 +08004546 if (mShaderVersion < 300)
4547 {
4548 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4549 }
4550 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004551 }
Shaob5cc1192017-07-06 10:47:20 +08004552 case GL_COMPUTE_SHADER:
4553 {
4554 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4555 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004556 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004557 {
4558 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4559 }
4560 default:
4561 {
4562 UNREACHABLE();
4563 return new TStorageQualifierWrapper(EvqLast, loc);
4564 }
Olli Etuahocce89652017-06-19 16:04:09 +03004565 }
Olli Etuahocce89652017-06-19 16:04:09 +03004566}
4567
4568TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4569{
4570 if (declaringFunction())
4571 {
4572 return new TStorageQualifierWrapper(EvqOut, loc);
4573 }
Shaob5cc1192017-07-06 10:47:20 +08004574 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004575 {
Shaob5cc1192017-07-06 10:47:20 +08004576 case GL_VERTEX_SHADER:
4577 {
4578 if (mShaderVersion < 300)
4579 {
4580 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4581 }
4582 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4583 }
4584 case GL_FRAGMENT_SHADER:
4585 {
4586 if (mShaderVersion < 300)
4587 {
4588 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4589 }
4590 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4591 }
4592 case GL_COMPUTE_SHADER:
4593 {
4594 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4595 return new TStorageQualifierWrapper(EvqLast, loc);
4596 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004597 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004598 {
4599 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4600 }
4601 default:
4602 {
4603 UNREACHABLE();
4604 return new TStorageQualifierWrapper(EvqLast, loc);
4605 }
Olli Etuahocce89652017-06-19 16:04:09 +03004606 }
Olli Etuahocce89652017-06-19 16:04:09 +03004607}
4608
4609TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4610{
4611 if (!declaringFunction())
4612 {
4613 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4614 }
4615 return new TStorageQualifierWrapper(EvqInOut, loc);
4616}
4617
Jamie Madillb98c3a82015-07-23 14:26:04 -04004618TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004619 TLayoutQualifier rightQualifier,
4620 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004621{
Martin Radevc28888b2016-07-22 15:27:42 +03004622 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004623 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004624}
4625
Olli Etuahofbb1c792018-01-19 16:26:59 +02004626TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4627 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004628{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004629 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004630 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004631}
4632
Olli Etuahofbb1c792018-01-19 16:26:59 +02004633TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004634 const TSourceLoc &loc,
4635 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004636{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004637 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004638 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004639}
4640
Olli Etuaho722bfb52017-10-26 17:00:11 +03004641void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4642 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004643 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004644 const TSourceLoc &location)
4645{
4646 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4647 {
4648 if ((*fieldIter)->name() == name)
4649 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004650 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004651 }
4652 }
4653}
4654
4655TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4656{
4657 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4658 ++fieldIter)
4659 {
4660 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4661 location);
4662 }
4663 return fields;
4664}
4665
Olli Etuaho4de340a2016-12-16 09:32:03 +00004666TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4667 const TFieldList *newlyAddedFields,
4668 const TSourceLoc &location)
4669{
4670 for (TField *field : *newlyAddedFields)
4671 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004672 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4673 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004674 processedFields->push_back(field);
4675 }
4676 return processedFields;
4677}
4678
Martin Radev70866b82016-07-22 15:27:42 +03004679TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4680 const TTypeQualifierBuilder &typeQualifierBuilder,
4681 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004682 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004683{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004684 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004685
Martin Radev70866b82016-07-22 15:27:42 +03004686 typeSpecifier->qualifier = typeQualifier.qualifier;
4687 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004688 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004689 typeSpecifier->invariant = typeQualifier.invariant;
4690 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304691 {
Martin Radev70866b82016-07-22 15:27:42 +03004692 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004693 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004694 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004695}
4696
Jamie Madillb98c3a82015-07-23 14:26:04 -04004697TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004698 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004699{
Martin Radev4a9cd802016-09-01 16:51:51 +03004700 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4701 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004702
Olli Etuahofbb1c792018-01-19 16:26:59 +02004703 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004704 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004705
Martin Radev4a9cd802016-09-01 16:51:51 +03004706 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004707
Olli Etuahod5f44c92017-11-29 17:15:40 +02004708 TFieldList *fieldList = new TFieldList();
4709
4710 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304711 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004712 TType *type = new TType(typeSpecifier);
4713 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304714 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004715 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004716 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004717 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004718 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004719
Jamie Madillf5557ac2018-06-15 09:46:58 -04004720 TField *field =
4721 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004722 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4723 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004724 }
4725
Olli Etuahod5f44c92017-11-29 17:15:40 +02004726 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727}
4728
Martin Radev4a9cd802016-09-01 16:51:51 +03004729TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4730 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004731 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004732 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004733{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004734 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004735 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004736 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004737 structSymbolType = SymbolType::Empty;
4738 }
4739 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004740
Jamie Madill9b820842015-02-12 10:40:10 -05004741 // Store a bool in the struct if we're at global scope, to allow us to
4742 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004743 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004744
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004745 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004746 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004747 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004748 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304749 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004750 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004751 }
4752 }
4753
4754 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004755 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004756 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004757 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004758 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004759 switch (qualifier)
4760 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004761 case EvqGlobal:
4762 case EvqTemporary:
4763 break;
4764 default:
4765 error(field.line(), "invalid qualifier on struct member",
4766 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004767 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004768 }
Martin Radev70866b82016-07-22 15:27:42 +03004769 if (field.type()->isInvariant())
4770 {
4771 error(field.line(), "invalid qualifier on struct member", "invariant");
4772 }
jchen104cdac9e2017-05-08 11:01:20 +08004773 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4774 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004775 {
4776 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4777 }
4778
Olli Etuahoebee5b32017-11-23 12:56:32 +02004779 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004780 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004781
Olli Etuaho43364892017-02-13 16:00:12 +00004782 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4783
4784 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004785
4786 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004787 }
4788
Martin Radev4a9cd802016-09-01 16:51:51 +03004789 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004790 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004791 exitStructDeclaration();
4792
Martin Radev4a9cd802016-09-01 16:51:51 +03004793 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004794}
4795
Jamie Madillb98c3a82015-07-23 14:26:04 -04004796TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004797 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004798 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004799{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004800 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004801 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004802 init->isVector())
4803 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4805 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004806 return nullptr;
4807 }
4808
Olli Etuaho923ecef2017-10-11 12:01:38 +03004809 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004810 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004811 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004812 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004813 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004814 }
4815
Olli Etuaho94bbed12018-03-20 14:44:53 +02004816 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004817 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4818 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004819 return node;
4820}
4821
4822TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4823{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004824 if (mSwitchNestingLevel == 0)
4825 {
4826 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004827 return nullptr;
4828 }
4829 if (condition == nullptr)
4830 {
4831 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004832 return nullptr;
4833 }
4834 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004835 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 {
4837 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004838 }
4839 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004840 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4841 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4842 // fold in case labels.
4843 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 {
4845 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004846 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004847 TIntermCase *node = new TIntermCase(condition);
4848 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004849 return node;
4850}
4851
4852TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4853{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004854 if (mSwitchNestingLevel == 0)
4855 {
4856 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 return nullptr;
4858 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004859 TIntermCase *node = new TIntermCase(nullptr);
4860 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004861 return node;
4862}
4863
Jamie Madillb98c3a82015-07-23 14:26:04 -04004864TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4865 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004866 const TSourceLoc &loc,
4867 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004868{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004869 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004870
4871 switch (op)
4872 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004873 case EOpLogicalNot:
4874 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4875 child->isVector())
4876 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004877 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004878 return nullptr;
4879 }
4880 break;
4881 case EOpBitwiseNot:
4882 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4883 child->isMatrix() || child->isArray())
4884 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004885 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004886 return nullptr;
4887 }
4888 break;
4889 case EOpPostIncrement:
4890 case EOpPreIncrement:
4891 case EOpPostDecrement:
4892 case EOpPreDecrement:
4893 case EOpNegative:
4894 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004895 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4896 child->getBasicType() == EbtBool || child->isArray() ||
4897 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004898 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004899 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004900 return nullptr;
4901 }
Nico Weber41b072b2018-02-09 10:01:32 -05004902 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004903 // Operators for built-ins are already type checked against their prototype.
4904 default:
4905 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004906 }
4907
Jiajia Qinbc585152017-06-23 15:42:17 +08004908 if (child->getMemoryQualifier().writeonly)
4909 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004910 unaryOpError(loc, GetOperatorString(op), child->getType());
Jiajia Qinbc585152017-06-23 15:42:17 +08004911 return nullptr;
4912 }
4913
Olli Etuaho94bbed12018-03-20 14:44:53 +02004914 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004915 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03004916 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004917
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004918 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004919}
4920
Olli Etuaho09b22472015-02-11 11:47:26 +02004921TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4922{
Olli Etuahocce89652017-06-19 16:04:09 +03004923 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004924 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004925 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004926 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004927 return child;
4928 }
4929 return node;
4930}
4931
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4933 TIntermTyped *child,
4934 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004935{
Olli Etuaho856c4972016-08-08 11:38:39 +03004936 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004937 return addUnaryMath(op, child, loc);
4938}
4939
Olli Etuaho765924f2018-01-04 12:48:36 +02004940TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4941{
4942 // If we can, we should return the folded version of the expression for subsequent parsing. This
4943 // enables folding the containing expression during parsing as well, instead of the separate
4944 // FoldExpressions() step where folding nested expressions requires multiple full AST
4945 // traversals.
4946
4947 // Even if folding fails the fold() functions return some node representing the expression,
4948 // typically the original node. So "folded" can be assumed to be non-null.
4949 TIntermTyped *folded = expression->fold(mDiagnostics);
4950 ASSERT(folded != nullptr);
4951 if (folded->getQualifier() == expression->getQualifier())
4952 {
4953 // We need this expression to have the correct qualifier when validating the consuming
4954 // expression. So we can only return the folded node from here in case it has the same
4955 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4956 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4957 // 1. (true ? 1.0 : non_constant)
4958 // 2. (non_constant, 1.0)
4959 return folded;
4960 }
4961 return expression;
4962}
4963
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964bool TParseContext::binaryOpCommonCheck(TOperator op,
4965 TIntermTyped *left,
4966 TIntermTyped *right,
4967 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004968{
jchen10b4cf5652017-05-05 18:51:17 +08004969 // Check opaque types are not allowed to be operands in expressions other than array indexing
4970 // and structure member selection.
4971 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4972 {
4973 switch (op)
4974 {
4975 case EOpIndexDirect:
4976 case EOpIndexIndirect:
4977 break;
jchen10b4cf5652017-05-05 18:51:17 +08004978
4979 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004980 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08004981 error(loc, "Invalid operation for variables with an opaque type",
4982 GetOperatorString(op));
4983 return false;
4984 }
4985 }
jchen10cc2a10e2017-05-03 14:05:12 +08004986
Jiajia Qinbc585152017-06-23 15:42:17 +08004987 if (right->getMemoryQualifier().writeonly)
4988 {
4989 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4990 return false;
4991 }
4992
4993 if (left->getMemoryQualifier().writeonly)
4994 {
4995 switch (op)
4996 {
4997 case EOpAssign:
4998 case EOpInitialize:
4999 case EOpIndexDirect:
5000 case EOpIndexIndirect:
5001 case EOpIndexDirectStruct:
5002 case EOpIndexDirectInterfaceBlock:
5003 break;
5004 default:
5005 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5006 return false;
5007 }
5008 }
5009
Olli Etuaho244be012016-08-18 15:26:02 +03005010 if (left->getType().getStruct() || right->getType().getStruct())
5011 {
5012 switch (op)
5013 {
5014 case EOpIndexDirectStruct:
5015 ASSERT(left->getType().getStruct());
5016 break;
5017 case EOpEqual:
5018 case EOpNotEqual:
5019 case EOpAssign:
5020 case EOpInitialize:
5021 if (left->getType() != right->getType())
5022 {
5023 return false;
5024 }
5025 break;
5026 default:
5027 error(loc, "Invalid operation for structs", GetOperatorString(op));
5028 return false;
5029 }
5030 }
5031
Olli Etuaho94050052017-05-08 14:17:44 +03005032 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5033 {
5034 switch (op)
5035 {
5036 case EOpIndexDirectInterfaceBlock:
5037 ASSERT(left->getType().getInterfaceBlock());
5038 break;
5039 default:
5040 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5041 return false;
5042 }
5043 }
5044
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005045 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005046 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005047 error(loc, "array / non-array mismatch", GetOperatorString(op));
5048 return false;
5049 }
5050
5051 if (left->isArray())
5052 {
5053 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005054 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005055 {
5056 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5057 return false;
5058 }
5059
Olli Etuahoe79904c2015-03-18 16:56:42 +02005060 switch (op)
5061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005062 case EOpEqual:
5063 case EOpNotEqual:
5064 case EOpAssign:
5065 case EOpInitialize:
5066 break;
5067 default:
5068 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5069 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005070 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005071 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005072 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005073 {
5074 error(loc, "array size mismatch", GetOperatorString(op));
5075 return false;
5076 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005077 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005078
5079 // Check ops which require integer / ivec parameters
5080 bool isBitShift = false;
5081 switch (op)
5082 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005083 case EOpBitShiftLeft:
5084 case EOpBitShiftRight:
5085 case EOpBitShiftLeftAssign:
5086 case EOpBitShiftRightAssign:
5087 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5088 // check that the basic type is an integer type.
5089 isBitShift = true;
5090 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5091 {
5092 return false;
5093 }
5094 break;
5095 case EOpBitwiseAnd:
5096 case EOpBitwiseXor:
5097 case EOpBitwiseOr:
5098 case EOpBitwiseAndAssign:
5099 case EOpBitwiseXorAssign:
5100 case EOpBitwiseOrAssign:
5101 // It is enough to check the type of only one operand, since later it
5102 // is checked that the operand types match.
5103 if (!IsInteger(left->getBasicType()))
5104 {
5105 return false;
5106 }
5107 break;
5108 default:
5109 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005110 }
5111
5112 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5113 // So the basic type should usually match.
5114 if (!isBitShift && left->getBasicType() != right->getBasicType())
5115 {
5116 return false;
5117 }
5118
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005119 // Check that:
5120 // 1. Type sizes match exactly on ops that require that.
5121 // 2. Restrictions for structs that contain arrays or samplers are respected.
5122 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005123 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005124 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005125 case EOpAssign:
5126 case EOpInitialize:
5127 case EOpEqual:
5128 case EOpNotEqual:
5129 // ESSL 1.00 sections 5.7, 5.8, 5.9
5130 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5131 {
5132 error(loc, "undefined operation for structs containing arrays",
5133 GetOperatorString(op));
5134 return false;
5135 }
5136 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5137 // we interpret the spec so that this extends to structs containing samplers,
5138 // similarly to ESSL 1.00 spec.
5139 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5140 left->getType().isStructureContainingSamplers())
5141 {
5142 error(loc, "undefined operation for structs containing samplers",
5143 GetOperatorString(op));
5144 return false;
5145 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005146
Olli Etuahoe1805592017-01-02 16:41:20 +00005147 if ((left->getNominalSize() != right->getNominalSize()) ||
5148 (left->getSecondarySize() != right->getSecondarySize()))
5149 {
5150 error(loc, "dimension mismatch", GetOperatorString(op));
5151 return false;
5152 }
5153 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 case EOpLessThan:
5155 case EOpGreaterThan:
5156 case EOpLessThanEqual:
5157 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005158 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005159 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005160 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005161 return false;
5162 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005163 break;
5164 case EOpAdd:
5165 case EOpSub:
5166 case EOpDiv:
5167 case EOpIMod:
5168 case EOpBitShiftLeft:
5169 case EOpBitShiftRight:
5170 case EOpBitwiseAnd:
5171 case EOpBitwiseXor:
5172 case EOpBitwiseOr:
5173 case EOpAddAssign:
5174 case EOpSubAssign:
5175 case EOpDivAssign:
5176 case EOpIModAssign:
5177 case EOpBitShiftLeftAssign:
5178 case EOpBitShiftRightAssign:
5179 case EOpBitwiseAndAssign:
5180 case EOpBitwiseXorAssign:
5181 case EOpBitwiseOrAssign:
5182 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5183 {
5184 return false;
5185 }
5186
5187 // Are the sizes compatible?
5188 if (left->getNominalSize() != right->getNominalSize() ||
5189 left->getSecondarySize() != right->getSecondarySize())
5190 {
5191 // If the nominal sizes of operands do not match:
5192 // One of them must be a scalar.
5193 if (!left->isScalar() && !right->isScalar())
5194 return false;
5195
5196 // In the case of compound assignment other than multiply-assign,
5197 // the right side needs to be a scalar. Otherwise a vector/matrix
5198 // would be assigned to a scalar. A scalar can't be shifted by a
5199 // vector either.
5200 if (!right->isScalar() &&
5201 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5202 return false;
5203 }
5204 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005205 default:
5206 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005207 }
5208
Olli Etuahod6b14282015-03-17 14:31:35 +02005209 return true;
5210}
5211
Olli Etuaho1dded802016-08-18 18:13:13 +03005212bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5213 const TType &left,
5214 const TType &right)
5215{
5216 switch (op)
5217 {
5218 case EOpMul:
5219 case EOpMulAssign:
5220 return left.getNominalSize() == right.getNominalSize() &&
5221 left.getSecondarySize() == right.getSecondarySize();
5222 case EOpVectorTimesScalar:
5223 return true;
5224 case EOpVectorTimesScalarAssign:
5225 ASSERT(!left.isMatrix() && !right.isMatrix());
5226 return left.isVector() && !right.isVector();
5227 case EOpVectorTimesMatrix:
5228 return left.getNominalSize() == right.getRows();
5229 case EOpVectorTimesMatrixAssign:
5230 ASSERT(!left.isMatrix() && right.isMatrix());
5231 return left.isVector() && left.getNominalSize() == right.getRows() &&
5232 left.getNominalSize() == right.getCols();
5233 case EOpMatrixTimesVector:
5234 return left.getCols() == right.getNominalSize();
5235 case EOpMatrixTimesScalar:
5236 return true;
5237 case EOpMatrixTimesScalarAssign:
5238 ASSERT(left.isMatrix() && !right.isMatrix());
5239 return !right.isVector();
5240 case EOpMatrixTimesMatrix:
5241 return left.getCols() == right.getRows();
5242 case EOpMatrixTimesMatrixAssign:
5243 ASSERT(left.isMatrix() && right.isMatrix());
5244 // We need to check two things:
5245 // 1. The matrix multiplication step is valid.
5246 // 2. The result will have the same number of columns as the lvalue.
5247 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5248
5249 default:
5250 UNREACHABLE();
5251 return false;
5252 }
5253}
5254
Jamie Madillb98c3a82015-07-23 14:26:04 -04005255TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5256 TIntermTyped *left,
5257 TIntermTyped *right,
5258 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005259{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005260 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005261 return nullptr;
5262
Olli Etuahofc1806e2015-03-17 13:03:11 +02005263 switch (op)
5264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005265 case EOpEqual:
5266 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005267 case EOpLessThan:
5268 case EOpGreaterThan:
5269 case EOpLessThanEqual:
5270 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005271 break;
5272 case EOpLogicalOr:
5273 case EOpLogicalXor:
5274 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005275 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5276 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005277 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 {
5279 return nullptr;
5280 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005281 // Basic types matching should have been already checked.
5282 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 break;
5284 case EOpAdd:
5285 case EOpSub:
5286 case EOpDiv:
5287 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005288 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5289 !right->getType().getStruct());
5290 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005291 {
5292 return nullptr;
5293 }
5294 break;
5295 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005296 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5297 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005298 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005299 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 {
5301 return nullptr;
5302 }
5303 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005304 default:
5305 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005306 }
5307
Olli Etuaho1dded802016-08-18 18:13:13 +03005308 if (op == EOpMul)
5309 {
5310 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5311 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5312 {
5313 return nullptr;
5314 }
5315 }
5316
Olli Etuaho3fdec912016-08-18 15:08:06 +03005317 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005318 ASSERT(op != EOpAssign);
5319 markStaticReadIfSymbol(left);
5320 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005321 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005322 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005323}
5324
Jamie Madillb98c3a82015-07-23 14:26:04 -04005325TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5326 TIntermTyped *left,
5327 TIntermTyped *right,
5328 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005329{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005330 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005331 if (node == 0)
5332 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005333 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho09b22472015-02-11 11:47:26 +02005334 return left;
5335 }
5336 return node;
5337}
5338
Jamie Madillb98c3a82015-07-23 14:26:04 -04005339TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5340 TIntermTyped *left,
5341 TIntermTyped *right,
5342 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005343{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005344 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005345 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005346 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005347 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005348 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005349 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005350 }
5351 return node;
5352}
5353
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354TIntermTyped *TParseContext::addAssign(TOperator op,
5355 TIntermTyped *left,
5356 TIntermTyped *right,
5357 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005358{
Olli Etuahocce89652017-06-19 16:04:09 +03005359 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005360 TIntermBinary *node = nullptr;
5361 if (binaryOpCommonCheck(op, left, right, loc))
5362 {
5363 if (op == EOpMulAssign)
5364 {
5365 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5366 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5367 {
5368 node = new TIntermBinary(op, left, right);
5369 }
5370 }
5371 else
5372 {
5373 node = new TIntermBinary(op, left, right);
5374 }
5375 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005376 if (node == nullptr)
5377 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005378 assignError(loc, "assign", left->getType(), right->getType());
Olli Etuahod6b14282015-03-17 14:31:35 +02005379 return left;
5380 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005381 if (op != EOpAssign)
5382 {
5383 markStaticReadIfSymbol(left);
5384 }
5385 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005386 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005387 return node;
5388}
5389
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005390TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5391 TIntermTyped *right,
5392 const TSourceLoc &loc)
5393{
Corentin Wallez0d959252016-07-12 17:26:32 -04005394 // WebGL2 section 5.26, the following results in an error:
5395 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005396 if (mShaderSpec == SH_WEBGL2_SPEC &&
5397 (left->isArray() || left->getBasicType() == EbtVoid ||
5398 left->getType().isStructureContainingArrays() || right->isArray() ||
5399 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005400 {
5401 error(loc,
5402 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5403 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005404 }
5405
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005406 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005407 markStaticReadIfSymbol(left);
5408 markStaticReadIfSymbol(right);
5409 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005410
5411 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005412}
5413
Olli Etuaho49300862015-02-20 14:54:49 +02005414TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5415{
5416 switch (op)
5417 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005418 case EOpContinue:
5419 if (mLoopNestingLevel <= 0)
5420 {
5421 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005422 }
5423 break;
5424 case EOpBreak:
5425 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5426 {
5427 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005428 }
5429 break;
5430 case EOpReturn:
5431 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5432 {
5433 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005434 }
5435 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005436 case EOpKill:
5437 if (mShaderType != GL_FRAGMENT_SHADER)
5438 {
5439 error(loc, "discard supported in fragment shaders only", "discard");
5440 }
5441 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005442 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005443 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005444 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005445 }
Olli Etuahocce89652017-06-19 16:04:09 +03005446 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005447}
5448
Jamie Madillb98c3a82015-07-23 14:26:04 -04005449TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005450 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005451 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005452{
Olli Etuahocce89652017-06-19 16:04:09 +03005453 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005454 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005455 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005456 ASSERT(op == EOpReturn);
5457 mFunctionReturnsValue = true;
5458 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5459 {
5460 error(loc, "void function cannot return a value", "return");
5461 }
5462 else if (*mCurrentFunctionType != expression->getType())
5463 {
5464 error(loc, "function return is not matching type:", "return");
5465 }
Olli Etuaho49300862015-02-20 14:54:49 +02005466 }
Olli Etuahocce89652017-06-19 16:04:09 +03005467 TIntermBranch *node = new TIntermBranch(op, expression);
5468 node->setLine(loc);
5469 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005470}
5471
Olli Etuaho94bbed12018-03-20 14:44:53 +02005472void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5473{
5474 if (statement != nullptr)
5475 {
5476 markStaticReadIfSymbol(statement);
5477 block->appendStatement(statement);
5478 }
5479}
5480
Martin Radev84aa2dc2017-09-11 15:51:02 +03005481void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5482{
5483 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005484 const TFunction *func = functionCall->getFunction();
5485 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005486 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005487 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005488 TIntermNode *componentNode = nullptr;
5489 TIntermSequence *arguments = functionCall->getSequence();
5490 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5491 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5492 ASSERT(sampler != nullptr);
5493 switch (sampler->getBasicType())
5494 {
5495 case EbtSampler2D:
5496 case EbtISampler2D:
5497 case EbtUSampler2D:
5498 case EbtSampler2DArray:
5499 case EbtISampler2DArray:
5500 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005501 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005502 (isTextureGatherOffset && arguments->size() == 4u))
5503 {
5504 componentNode = arguments->back();
5505 }
5506 break;
5507 case EbtSamplerCube:
5508 case EbtISamplerCube:
5509 case EbtUSamplerCube:
5510 ASSERT(!isTextureGatherOffset);
5511 if (arguments->size() == 3u)
5512 {
5513 componentNode = arguments->back();
5514 }
5515 break;
5516 case EbtSampler2DShadow:
5517 case EbtSampler2DArrayShadow:
5518 case EbtSamplerCubeShadow:
5519 break;
5520 default:
5521 UNREACHABLE();
5522 break;
5523 }
5524 if (componentNode)
5525 {
5526 const TIntermConstantUnion *componentConstantUnion =
5527 componentNode->getAsConstantUnion();
5528 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5529 {
5530 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005531 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005532 }
5533 else
5534 {
5535 int component = componentConstantUnion->getIConst(0);
5536 if (component < 0 || component > 3)
5537 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005538 error(functionCall->getLine(), "Component must be in the range [0;3]",
5539 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005540 }
5541 }
5542 }
5543 }
5544}
5545
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005546void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5547{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005548 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005549 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005550 TIntermNode *offset = nullptr;
5551 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005552 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005553 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005554 {
5555 offset = arguments->back();
5556 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005557 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005558 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005559 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005560 ASSERT(arguments->size() >= 3);
5561 offset = (*arguments)[2];
5562 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005563 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005564 {
5565 ASSERT(arguments->size() >= 3u);
5566 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5567 ASSERT(sampler != nullptr);
5568 switch (sampler->getBasicType())
5569 {
5570 case EbtSampler2D:
5571 case EbtISampler2D:
5572 case EbtUSampler2D:
5573 case EbtSampler2DArray:
5574 case EbtISampler2DArray:
5575 case EbtUSampler2DArray:
5576 offset = (*arguments)[2];
5577 break;
5578 case EbtSampler2DShadow:
5579 case EbtSampler2DArrayShadow:
5580 offset = (*arguments)[3];
5581 break;
5582 default:
5583 UNREACHABLE();
5584 break;
5585 }
5586 useTextureGatherOffsetConstraints = true;
5587 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005588 if (offset != nullptr)
5589 {
5590 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5591 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5592 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005593 error(functionCall->getLine(), "Texture offset must be a constant expression",
5594 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005595 }
5596 else
5597 {
5598 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5599 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005600 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005601 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5602 : mMinProgramTexelOffset;
5603 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5604 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005605 for (size_t i = 0u; i < size; ++i)
5606 {
5607 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005608 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005609 {
5610 std::stringstream tokenStream;
5611 tokenStream << offsetValue;
5612 std::string token = tokenStream.str();
5613 error(offset->getLine(), "Texture offset value out of valid range",
5614 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005615 }
5616 }
5617 }
5618 }
5619}
5620
Jiajia Qina3106c52017-11-03 09:39:39 +08005621void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5622{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005623 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005624 const TFunction *func = functionCall->getFunction();
5625 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005626 {
5627 TIntermSequence *arguments = functionCall->getSequence();
5628 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5629
5630 if (IsBufferOrSharedVariable(memNode))
5631 {
5632 return;
5633 }
5634
5635 while (memNode->getAsBinaryNode())
5636 {
5637 memNode = memNode->getAsBinaryNode()->getLeft();
5638 if (IsBufferOrSharedVariable(memNode))
5639 {
5640 return;
5641 }
5642 }
5643
5644 error(memNode->getLine(),
5645 "The value passed to the mem argument of an atomic memory function does not "
5646 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005647 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005648 }
5649}
5650
Martin Radev2cc85b32016-08-05 16:22:53 +03005651// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5652void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5653{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005654 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005655
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005656 const TFunction *func = functionCall->getFunction();
5657
5658 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005659 {
5660 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005661 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005662
Olli Etuaho485eefd2017-02-14 17:40:06 +00005663 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005664
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005665 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005666 {
5667 if (memoryQualifier.readonly)
5668 {
5669 error(imageNode->getLine(),
5670 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005671 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005672 }
5673 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005674 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005675 {
5676 if (memoryQualifier.writeonly)
5677 {
5678 error(imageNode->getLine(),
5679 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005680 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005681 }
5682 }
5683 }
5684}
5685
5686// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5687void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5688 const TFunction *functionDefinition,
5689 const TIntermAggregate *functionCall)
5690{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005691 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005692
5693 const TIntermSequence &arguments = *functionCall->getSequence();
5694
5695 ASSERT(functionDefinition->getParamCount() == arguments.size());
5696
5697 for (size_t i = 0; i < arguments.size(); ++i)
5698 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005699 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5700 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005701 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005702 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5703
5704 if (IsImage(functionArgumentType.getBasicType()))
5705 {
5706 const TMemoryQualifier &functionArgumentMemoryQualifier =
5707 functionArgumentType.getMemoryQualifier();
5708 const TMemoryQualifier &functionParameterMemoryQualifier =
5709 functionParameterType.getMemoryQualifier();
5710 if (functionArgumentMemoryQualifier.readonly &&
5711 !functionParameterMemoryQualifier.readonly)
5712 {
5713 error(functionCall->getLine(),
5714 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005715 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005716 }
5717
5718 if (functionArgumentMemoryQualifier.writeonly &&
5719 !functionParameterMemoryQualifier.writeonly)
5720 {
5721 error(functionCall->getLine(),
5722 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005723 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005724 }
Martin Radev049edfa2016-11-11 14:35:37 +02005725
5726 if (functionArgumentMemoryQualifier.coherent &&
5727 !functionParameterMemoryQualifier.coherent)
5728 {
5729 error(functionCall->getLine(),
5730 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005731 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005732 }
5733
5734 if (functionArgumentMemoryQualifier.volatileQualifier &&
5735 !functionParameterMemoryQualifier.volatileQualifier)
5736 {
5737 error(functionCall->getLine(),
5738 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005739 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005740 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005741 }
5742 }
5743}
5744
Olli Etuaho95ed1942018-02-01 14:01:19 +02005745TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005746{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005747 if (fnCall->thisNode() != nullptr)
5748 {
5749 return addMethod(fnCall, loc);
5750 }
5751 if (fnCall->isConstructor())
5752 {
5753 return addConstructor(fnCall, loc);
5754 }
5755 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005756}
5757
Olli Etuaho95ed1942018-02-01 14:01:19 +02005758TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005759{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005760 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5762 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5763 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005764 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005765 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005766 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005767 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005768 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005769 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005770 {
5771 error(loc, "method takes no parameters", "length");
5772 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005773 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005774 {
5775 error(loc, "length can only be called on arrays", "length");
5776 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005777 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005778 mGeometryShaderInputPrimitiveType == EptUndefined)
5779 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005780 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005781 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5782 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 else
5784 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005785 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005786 node->setLine(loc);
5787 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005788 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005789 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005790}
5791
Olli Etuaho95ed1942018-02-01 14:01:19 +02005792TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005793 const TSourceLoc &loc)
5794{
Olli Etuaho697bf652018-02-16 11:50:54 +02005795 // First check whether the function has been hidden by a variable name or struct typename by
5796 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5797 // with a matching argument list.
5798 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005800 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 }
5802 else
5803 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005804 // There are no inner functions, so it's enough to look for user-defined functions in the
5805 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005806 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005807 if (symbol != nullptr)
5808 {
5809 // A user-defined function - could be an overloaded built-in as well.
5810 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5811 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5812 TIntermAggregate *callNode =
5813 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5814 callNode->setLine(loc);
5815 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5816 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5817 return callNode;
5818 }
5819
5820 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005821 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005822 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005823 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005824 }
5825 else
5826 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005827 // A built-in function.
5828 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005830
Olli Etuaho37b697e2018-01-29 12:19:27 +02005831 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005832 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005833 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005836 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 if (fnCandidate->getParamCount() == 1)
5840 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005842 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005843 TIntermTyped *callNode =
5844 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005845 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005846 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005848 TIntermAggregate *callNode =
5849 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005850 callNode->setLine(loc);
5851
Olli Etuahoe80825e2018-02-16 10:24:53 +02005852 // Some built-in functions have out parameters too.
5853 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5854
5855 // See if we can constant fold a built-in. Note that this may be possible
5856 // even if it is not const-qualified.
5857 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005859
5860 // This is a built-in function with no op associated with it.
5861 TIntermAggregate *callNode =
5862 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5863 callNode->setLine(loc);
5864 checkTextureOffsetConst(callNode);
5865 checkTextureGather(callNode);
5866 checkImageMemoryAccessForBuiltinFunctions(callNode);
5867 checkAtomicMemoryBuiltinFunctions(callNode);
5868 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5869 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005870 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005871 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005872
5873 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005874 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005875}
5876
Jamie Madillb98c3a82015-07-23 14:26:04 -04005877TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005878 TIntermTyped *trueExpression,
5879 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005880 const TSourceLoc &loc)
5881{
Olli Etuaho56229f12017-07-10 14:16:33 +03005882 if (!checkIsScalarBool(loc, cond))
5883 {
5884 return falseExpression;
5885 }
Olli Etuaho52901742015-04-15 13:42:45 +03005886
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005887 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005888 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005889 TInfoSinkBase reasonStream;
5890 reasonStream << "mismatching ternary operator operand types '" << trueExpression->getType()
5891 << " and '" << falseExpression->getType() << "'";
5892 error(loc, reasonStream.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005893 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005894 }
Olli Etuahode318b22016-10-25 16:18:25 +01005895 if (IsOpaqueType(trueExpression->getBasicType()))
5896 {
5897 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005898 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005899 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5900 // Note that structs containing opaque types don't need to be checked as structs are
5901 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005902 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005903 return falseExpression;
5904 }
5905
Jiajia Qinbc585152017-06-23 15:42:17 +08005906 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5907 falseExpression->getMemoryQualifier().writeonly)
5908 {
5909 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5910 return falseExpression;
5911 }
5912
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005913 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005914 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005915 // ESSL 3.00.6 section 5.7:
5916 // Ternary operator support is optional for arrays. No certainty that it works across all
5917 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5918 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005919 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005920 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005921 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005922 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005923 }
Olli Etuaho94050052017-05-08 14:17:44 +03005924 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5925 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005926 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005927 return falseExpression;
5928 }
5929
Corentin Wallez0d959252016-07-12 17:26:32 -04005930 // WebGL2 section 5.26, the following results in an error:
5931 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005932 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005933 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005934 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005935 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005936 }
5937
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005938 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005939 markStaticReadIfSymbol(cond);
5940 markStaticReadIfSymbol(trueExpression);
5941 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005942 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005943 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005944}
Olli Etuaho49300862015-02-20 14:54:49 +02005945
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005946//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005947// Parse an array of strings using yyparse.
5948//
5949// Returns 0 for success.
5950//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005951int PaParseStrings(size_t count,
5952 const char *const string[],
5953 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305954 TParseContext *context)
5955{
Yunchao He4f285442017-04-21 12:15:49 +08005956 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005957 return 1;
5958
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005959 if (glslang_initialize(context))
5960 return 1;
5961
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005962 int error = glslang_scan(count, string, length, context);
5963 if (!error)
5964 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005965
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005966 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005967
alokp@chromium.org6b495712012-06-29 00:06:58 +00005968 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005969}
Jamie Madill45bcc782016-11-07 13:58:48 -05005970
5971} // namespace sh