blob: c3393ee9356e362551321f59c3edc5481d9cd4ad [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 Etuahoa78092c2018-09-26 14:16:13 +03001133 ASSERT(type->getLayoutQualifier().index == -1 ||
1134 (isExtensionEnabled(TExtension::EXT_blend_func_extended) &&
1135 mShaderType == GL_FRAGMENT_SHADER && mShaderVersion >= 300));
1136 if (type->getQualifier() == EvqFragmentOut)
1137 {
1138 if (type->getLayoutQualifier().index != -1 && type->getLayoutQualifier().location == -1)
1139 {
1140 error(line,
1141 "If index layout qualifier is specified for a fragment output, location must "
1142 "also be specified.",
1143 "index");
1144 return false;
1145 }
1146 }
1147 else
1148 {
1149 checkIndexIsNotSpecified(line, type->getLayoutQualifier().index);
1150 }
1151
Olli Etuahob60d30f2018-01-16 12:31:06 +02001152 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001153
Olli Etuaho856c4972016-08-08 11:38:39 +03001154 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001157 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001158 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001159 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001160 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001161 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001162 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001163 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001164 return false;
1165 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001166 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001167 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001168 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001169 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001170 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001171 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001172 }
1173 }
1174 else
1175 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001176 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001177 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001178 return false;
1179 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181
Olli Etuaho8a176262016-08-16 14:23:01 +03001182 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001183 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184
Olli Etuaho437664b2018-02-28 15:38:14 +02001185 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001186 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001187 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001188 return false;
1189 }
1190
Olli Etuahob60d30f2018-01-16 12:31:06 +02001191 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001192 return false;
1193
1194 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001195}
1196
Martin Radev70866b82016-07-22 15:27:42 +03001197void TParseContext::checkIsParameterQualifierValid(
1198 const TSourceLoc &line,
1199 const TTypeQualifierBuilder &typeQualifierBuilder,
1200 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301201{
Olli Etuahocce89652017-06-19 16:04:09 +03001202 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001203 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001204
1205 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301206 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001207 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1208 }
1209
1210 if (!IsImage(type->getBasicType()))
1211 {
Olli Etuaho43364892017-02-13 16:00:12 +00001212 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001213 }
1214 else
1215 {
1216 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001217 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001218
Martin Radev70866b82016-07-22 15:27:42 +03001219 type->setQualifier(typeQualifier.qualifier);
1220
1221 if (typeQualifier.precision != EbpUndefined)
1222 {
1223 type->setPrecision(typeQualifier.precision);
1224 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225}
1226
Olli Etuaho703671e2017-11-08 17:47:18 +02001227template <size_t size>
1228bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1229 const std::array<TExtension, size> &extensions)
1230{
1231 ASSERT(!extensions.empty());
1232 const TExtensionBehavior &extBehavior = extensionBehavior();
1233
1234 bool canUseWithWarning = false;
1235 bool canUseWithoutWarning = false;
1236
1237 const char *errorMsgString = "";
1238 TExtension errorMsgExtension = TExtension::UNDEFINED;
1239
1240 for (TExtension extension : extensions)
1241 {
1242 auto extIter = extBehavior.find(extension);
1243 if (canUseWithWarning)
1244 {
1245 // We already have an extension that we can use, but with a warning.
1246 // See if we can use the alternative extension without a warning.
1247 if (extIter == extBehavior.end())
1248 {
1249 continue;
1250 }
1251 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1252 {
1253 canUseWithoutWarning = true;
1254 break;
1255 }
1256 continue;
1257 }
1258 if (extIter == extBehavior.end())
1259 {
1260 errorMsgString = "extension is not supported";
1261 errorMsgExtension = extension;
1262 }
1263 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1264 {
1265 errorMsgString = "extension is disabled";
1266 errorMsgExtension = extension;
1267 }
1268 else if (extIter->second == EBhWarn)
1269 {
1270 errorMsgExtension = extension;
1271 canUseWithWarning = true;
1272 }
1273 else
1274 {
1275 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1276 canUseWithoutWarning = true;
1277 break;
1278 }
1279 }
1280
1281 if (canUseWithoutWarning)
1282 {
1283 return true;
1284 }
1285 if (canUseWithWarning)
1286 {
1287 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1288 return true;
1289 }
1290 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1291 return false;
1292}
1293
1294template bool TParseContext::checkCanUseOneOfExtensions(
1295 const TSourceLoc &line,
1296 const std::array<TExtension, 1> &extensions);
1297template bool TParseContext::checkCanUseOneOfExtensions(
1298 const TSourceLoc &line,
1299 const std::array<TExtension, 2> &extensions);
1300template bool TParseContext::checkCanUseOneOfExtensions(
1301 const TSourceLoc &line,
1302 const std::array<TExtension, 3> &extensions);
1303
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001304bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001305{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001306 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001307 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308}
1309
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001310// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1311// compile-time or link-time errors are the same whether or not the declaration is empty".
1312// This function implements all the checks that are done on qualifiers regardless of if the
1313// declaration is empty.
1314void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1315 const sh::TLayoutQualifier &layoutQualifier,
1316 const TSourceLoc &location)
1317{
1318 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1319 {
1320 error(location, "Shared memory declarations cannot have layout specified", "layout");
1321 }
1322
1323 if (layoutQualifier.matrixPacking != EmpUnspecified)
1324 {
1325 error(location, "layout qualifier only valid for interface blocks",
1326 getMatrixPackingString(layoutQualifier.matrixPacking));
1327 return;
1328 }
1329
1330 if (layoutQualifier.blockStorage != EbsUnspecified)
1331 {
1332 error(location, "layout qualifier only valid for interface blocks",
1333 getBlockStorageString(layoutQualifier.blockStorage));
1334 return;
1335 }
1336
1337 if (qualifier == EvqFragmentOut)
1338 {
1339 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1340 {
1341 error(location, "invalid layout qualifier combination", "yuv");
1342 return;
1343 }
1344 }
1345 else
1346 {
1347 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1348 }
1349
Olli Etuaho95468d12017-05-04 11:14:34 +03001350 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1351 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001352 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1353 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001354 {
1355 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1356 }
1357
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001358 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001359 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001360 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001361 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001362 // We're not checking whether the uniform location is in range here since that depends on
1363 // the type of the variable.
1364 // The type can only be fully determined for non-empty declarations.
1365 }
1366 if (!canHaveLocation)
1367 {
1368 checkLocationIsNotSpecified(location, layoutQualifier);
1369 }
1370}
1371
jchen104cdac9e2017-05-08 11:01:20 +08001372void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1373 const TSourceLoc &location)
1374{
1375 if (publicType.precision != EbpHigh)
1376 {
1377 error(location, "Can only be highp", "atomic counter");
1378 }
1379 // dEQP enforces compile error if location is specified. See uniform_location.test.
1380 if (publicType.layoutQualifier.location != -1)
1381 {
1382 error(location, "location must not be set for atomic_uint", "layout");
1383 }
1384 if (publicType.layoutQualifier.binding == -1)
1385 {
1386 error(location, "no binding specified", "atomic counter");
1387 }
1388}
1389
Olli Etuaho55bde912017-10-25 13:41:13 +03001390void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001391{
Olli Etuaho55bde912017-10-25 13:41:13 +03001392 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001393 {
1394 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1395 // error. It is assumed that this applies to empty declarations as well.
1396 error(location, "empty array declaration needs to specify a size", "");
1397 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03001398
1399 if (type.getQualifier() != EvqFragmentOut)
1400 {
1401 checkIndexIsNotSpecified(location, type.getLayoutQualifier().index);
1402 }
Martin Radevb8b01222016-11-20 23:25:53 +02001403}
1404
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001405// These checks are done for all declarations that are non-empty. They're done for non-empty
1406// declarations starting a declarator list, and declarators that follow an empty declaration.
1407void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1408 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001409{
Olli Etuahofa33d582015-04-09 14:33:12 +03001410 switch (publicType.qualifier)
1411 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001412 case EvqVaryingIn:
1413 case EvqVaryingOut:
1414 case EvqAttribute:
1415 case EvqVertexIn:
1416 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001417 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001418 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001419 {
1420 error(identifierLocation, "cannot be used with a structure",
1421 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001422 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001423 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001424 break;
1425 case EvqBuffer:
1426 if (publicType.getBasicType() != EbtInterfaceBlock)
1427 {
1428 error(identifierLocation,
1429 "cannot declare buffer variables at global scope(outside a block)",
1430 getQualifierString(publicType.qualifier));
1431 return;
1432 }
1433 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001434 default:
1435 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001436 }
jchen10cc2a10e2017-05-03 14:05:12 +08001437 std::string reason(getBasicString(publicType.getBasicType()));
1438 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001439 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001440 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001441 {
1442 return;
1443 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001444
Andrei Volykhina5527072017-03-22 16:46:30 +03001445 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1446 publicType.qualifier != EvqConst) &&
1447 publicType.getBasicType() == EbtYuvCscStandardEXT)
1448 {
1449 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1450 getQualifierString(publicType.qualifier));
1451 return;
1452 }
1453
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001454 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1455 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001456 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1457 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001458 TType type(publicType);
1459 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001460 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001461 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1462 publicType.layoutQualifier);
1463 }
1464 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001465
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001466 // check for layout qualifier issues
1467 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001468
Martin Radev2cc85b32016-08-05 16:22:53 +03001469 if (IsImage(publicType.getBasicType()))
1470 {
1471
1472 switch (layoutQualifier.imageInternalFormat)
1473 {
1474 case EiifRGBA32F:
1475 case EiifRGBA16F:
1476 case EiifR32F:
1477 case EiifRGBA8:
1478 case EiifRGBA8_SNORM:
1479 if (!IsFloatImage(publicType.getBasicType()))
1480 {
1481 error(identifierLocation,
1482 "internal image format requires a floating image type",
1483 getBasicString(publicType.getBasicType()));
1484 return;
1485 }
1486 break;
1487 case EiifRGBA32I:
1488 case EiifRGBA16I:
1489 case EiifRGBA8I:
1490 case EiifR32I:
1491 if (!IsIntegerImage(publicType.getBasicType()))
1492 {
1493 error(identifierLocation,
1494 "internal image format requires an integer image type",
1495 getBasicString(publicType.getBasicType()));
1496 return;
1497 }
1498 break;
1499 case EiifRGBA32UI:
1500 case EiifRGBA16UI:
1501 case EiifRGBA8UI:
1502 case EiifR32UI:
1503 if (!IsUnsignedImage(publicType.getBasicType()))
1504 {
1505 error(identifierLocation,
1506 "internal image format requires an unsigned image type",
1507 getBasicString(publicType.getBasicType()));
1508 return;
1509 }
1510 break;
1511 case EiifUnspecified:
1512 error(identifierLocation, "layout qualifier", "No image internal format specified");
1513 return;
1514 default:
1515 error(identifierLocation, "layout qualifier", "unrecognized token");
1516 return;
1517 }
1518
1519 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1520 switch (layoutQualifier.imageInternalFormat)
1521 {
1522 case EiifR32F:
1523 case EiifR32I:
1524 case EiifR32UI:
1525 break;
1526 default:
1527 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1528 {
1529 error(identifierLocation, "layout qualifier",
1530 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1531 "image variables must be qualified readonly and/or writeonly");
1532 return;
1533 }
1534 break;
1535 }
1536 }
1537 else
1538 {
Olli Etuaho43364892017-02-13 16:00:12 +00001539 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001540 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1541 }
jchen104cdac9e2017-05-08 11:01:20 +08001542
1543 if (IsAtomicCounter(publicType.getBasicType()))
1544 {
1545 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1546 }
1547 else
1548 {
1549 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1550 }
Olli Etuaho43364892017-02-13 16:00:12 +00001551}
Martin Radev2cc85b32016-08-05 16:22:53 +03001552
Olli Etuaho43364892017-02-13 16:00:12 +00001553void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1554{
1555 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001556 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1557 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1558 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1559 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1560 // when it comes to which shaders are accepted by the compiler.
1561 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001562 if (IsImage(type.getBasicType()))
1563 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001564 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1565 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001566 }
1567 else if (IsSampler(type.getBasicType()))
1568 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001569 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1570 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001571 }
jchen104cdac9e2017-05-08 11:01:20 +08001572 else if (IsAtomicCounter(type.getBasicType()))
1573 {
1574 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1575 }
Olli Etuaho43364892017-02-13 16:00:12 +00001576 else
1577 {
1578 ASSERT(!IsOpaqueType(type.getBasicType()));
1579 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001580 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001581}
1582
Olli Etuaho856c4972016-08-08 11:38:39 +03001583void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001584 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001585 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001586{
1587
1588 if (mShaderVersion < versionRequired)
1589 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001590 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001591 }
1592}
1593
Olli Etuaho856c4972016-08-08 11:38:39 +03001594bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1595 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001596{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001597 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001598 for (size_t i = 0u; i < localSize.size(); ++i)
1599 {
1600 if (localSize[i] != -1)
1601 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001602 error(location,
1603 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1604 "global layout declaration",
1605 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001606 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001607 }
1608 }
1609
Olli Etuaho8a176262016-08-16 14:23:01 +03001610 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001611}
1612
Olli Etuaho43364892017-02-13 16:00:12 +00001613void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001614 TLayoutImageInternalFormat internalFormat)
1615{
1616 if (internalFormat != EiifUnspecified)
1617 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001618 error(location, "invalid layout qualifier: only valid when used with images",
1619 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001620 }
Olli Etuaho43364892017-02-13 16:00:12 +00001621}
1622
Olli Etuahoa78092c2018-09-26 14:16:13 +03001623void TParseContext::checkIndexIsNotSpecified(const TSourceLoc &location, int index)
1624{
1625 if (index != -1)
1626 {
1627 error(location,
1628 "invalid layout qualifier: only valid when used with a fragment shader output in "
1629 "ESSL version >= 3.00 and EXT_blend_func_extended is enabled",
1630 "index");
1631 }
1632}
1633
Olli Etuaho43364892017-02-13 16:00:12 +00001634void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1635{
1636 if (binding != -1)
1637 {
1638 error(location,
1639 "invalid layout qualifier: only valid when used with opaque types or blocks",
1640 "binding");
1641 }
1642}
1643
jchen104cdac9e2017-05-08 11:01:20 +08001644void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1645{
1646 if (offset != -1)
1647 {
1648 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1649 "offset");
1650 }
1651}
1652
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001653void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1654 int binding,
1655 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001656{
1657 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001658 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001659 {
1660 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1661 }
1662}
1663
1664void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1665 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001666 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001667{
1668 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001669 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001670 {
1671 error(location, "sampler binding greater than maximum texture units", "binding");
1672 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001673}
1674
Jiajia Qinbc585152017-06-23 15:42:17 +08001675void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1676 const TQualifier &qualifier,
1677 int binding,
1678 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001679{
1680 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001681 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001682 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001683 if (binding + size > mMaxUniformBufferBindings)
1684 {
1685 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1686 "binding");
1687 }
1688 }
1689 else if (qualifier == EvqBuffer)
1690 {
1691 if (binding + size > mMaxShaderStorageBufferBindings)
1692 {
1693 error(location,
1694 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1695 "binding");
1696 }
jchen10af713a22017-04-19 09:10:56 +08001697 }
1698}
jchen104cdac9e2017-05-08 11:01:20 +08001699void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1700{
1701 if (binding >= mMaxAtomicCounterBindings)
1702 {
1703 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1704 "binding");
1705 }
1706}
jchen10af713a22017-04-19 09:10:56 +08001707
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001708void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1709 int objectLocationCount,
1710 const TLayoutQualifier &layoutQualifier)
1711{
1712 int loc = layoutQualifier.location;
1713 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1714 {
1715 error(location, "Uniform location out of range", "location");
1716 }
1717}
1718
Andrei Volykhina5527072017-03-22 16:46:30 +03001719void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1720{
1721 if (yuv != false)
1722 {
1723 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1724 }
1725}
1726
Jiajia Qinbc585152017-06-23 15:42:17 +08001727void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1728 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001729{
1730 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1731 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001732 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001733 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001734 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1735 qual == EvqConstReadOnly);
1736 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001737 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001738 markStaticReadIfSymbol(argument);
1739 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001740 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001741 if (argument->getMemoryQualifier().writeonly)
1742 {
1743 error(argument->getLine(),
1744 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1745 fnCall->functionName());
1746 return;
1747 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001748 }
1749 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001750 if (qual == EvqOut || qual == EvqInOut)
1751 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001752 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001753 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001754 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001755 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001756 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001757 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001758 }
1759 }
1760 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001761}
1762
Martin Radev70866b82016-07-22 15:27:42 +03001763void TParseContext::checkInvariantVariableQualifier(bool invariant,
1764 const TQualifier qualifier,
1765 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001766{
Martin Radev70866b82016-07-22 15:27:42 +03001767 if (!invariant)
1768 return;
1769
1770 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001771 {
Martin Radev70866b82016-07-22 15:27:42 +03001772 // input variables in the fragment shader can be also qualified as invariant
1773 if (!sh::CanBeInvariantESSL1(qualifier))
1774 {
1775 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1776 }
1777 }
1778 else
1779 {
1780 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1781 {
1782 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1783 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001784 }
1785}
1786
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001787bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001788{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001789 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001790}
1791
Jamie Madillb98c3a82015-07-23 14:26:04 -04001792void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1793 const char *extName,
1794 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001795{
Geoff Lang197d5292018-04-25 14:29:00 -04001796 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001797 srcLoc.file = loc.first_file;
1798 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001799 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001800}
1801
Jamie Madillb98c3a82015-07-23 14:26:04 -04001802void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1803 const char *name,
1804 const char *value,
1805 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001806{
Geoff Lang197d5292018-04-25 14:29:00 -04001807 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001808 srcLoc.file = loc.first_file;
1809 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001810 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001811}
1812
Martin Radev4c4c8e72016-08-04 12:25:34 +03001813sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001814{
Jamie Madill2f294c92017-11-20 14:47:26 -05001815 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001816 for (size_t i = 0u; i < result.size(); ++i)
1817 {
1818 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1819 {
1820 result[i] = 1;
1821 }
1822 else
1823 {
1824 result[i] = mComputeShaderLocalSize[i];
1825 }
1826 }
1827 return result;
1828}
1829
Olli Etuaho56229f12017-07-10 14:16:33 +03001830TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1831 const TSourceLoc &line)
1832{
1833 TIntermConstantUnion *node = new TIntermConstantUnion(
1834 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1835 node->setLine(line);
1836 return node;
1837}
1838
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839/////////////////////////////////////////////////////////////////////////////////
1840//
1841// Non-Errors.
1842//
1843/////////////////////////////////////////////////////////////////////////////////
1844
Jamie Madill5c097022014-08-20 16:38:32 -04001845const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001846 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001847 const TSymbol *symbol)
1848{
Jamie Madill5c097022014-08-20 16:38:32 -04001849 if (!symbol)
1850 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001851 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001852 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001853 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001854
1855 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001856 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001857 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001858 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001859 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001860
1861 const TVariable *variable = static_cast<const TVariable *>(symbol);
1862
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001863 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001864 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001865 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001866 }
1867
Olli Etuaho0f684632017-07-13 12:42:15 +03001868 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1869 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
Olli Etuaho59c5b892018-04-03 11:44:50 +03001870 variable->getType().getQualifier() == EvqWorkGroupSize)
Olli Etuaho0f684632017-07-13 12:42:15 +03001871 {
1872 error(location,
1873 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1874 "gl_WorkGroupSize");
1875 }
Jamie Madill5c097022014-08-20 16:38:32 -04001876 return variable;
1877}
1878
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001879TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001880 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001881 const TSymbol *symbol)
1882{
1883 const TVariable *variable = getNamedVariable(location, name, symbol);
1884
Olli Etuaho0f684632017-07-13 12:42:15 +03001885 if (!variable)
1886 {
1887 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1888 node->setLine(location);
1889 return node;
1890 }
1891
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 const TType &variableType = variable->getType();
Jamie Madill50cf2be2018-06-15 09:46:57 -04001893 TIntermTyped *node = nullptr;
Olli Etuaho56229f12017-07-10 14:16:33 +03001894
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001895 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001897 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001898 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001899 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001900 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001901 {
1902 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1903 // needs to be added to the AST as a constant and not as a symbol.
1904 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1905 TConstantUnion *constArray = new TConstantUnion[3];
1906 for (size_t i = 0; i < 3; ++i)
1907 {
1908 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1909 }
1910
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001911 ASSERT(variableType.getBasicType() == EbtUInt);
1912 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001913
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001914 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001915 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001916 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001917 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001918 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1919 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001920 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001921 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1922 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001923 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001924 else
1925 {
Olli Etuaho195be942017-12-04 23:40:14 +02001926 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001927 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001928 ASSERT(node != nullptr);
1929 node->setLine(location);
1930 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001931}
1932
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933// Initializers show up in several places in the grammar. Have one set of
1934// code to handle them here.
1935//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001936// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001937bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001938 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001939 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001941 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942{
Olli Etuaho13389b62016-10-16 11:48:18 +01001943 ASSERT(initNode != nullptr);
1944 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945
Olli Etuahob60d30f2018-01-16 12:31:06 +02001946 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001947 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001948 // In case initializer is not an array or type has more dimensions than initializer, this
1949 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1950 // actually is an array or not. Having a non-array initializer for an unsized array will
1951 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001952 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001953 type->sizeUnsizedArrays(arraySizes);
1954 }
1955
1956 const TQualifier qualifier = type->getQualifier();
1957
1958 bool constError = false;
1959 if (qualifier == EvqConst)
1960 {
1961 if (EvqConst != initializer->getType().getQualifier())
1962 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001963 TInfoSinkBase reasonStream;
1964 reasonStream << "assigning non-constant to '" << *type << "'";
1965 error(line, reasonStream.c_str(), "=");
Olli Etuahob60d30f2018-01-16 12:31:06 +02001966
1967 // We're still going to declare the variable to avoid extra error messages.
1968 type->setQualifier(EvqTemporary);
1969 constError = true;
1970 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001971 }
Olli Etuaho195be942017-12-04 23:40:14 +02001972
1973 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001974 if (!declareVariable(line, identifier, type, &variable))
1975 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001976 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
Olli Etuahob60d30f2018-01-16 12:31:06 +02001979 if (constError)
1980 {
1981 return false;
1982 }
1983
Olli Etuahob0c645e2015-05-12 14:25:36 +03001984 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001985 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001986 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001987 {
1988 // Error message does not completely match behavior with ESSL 1.00, but
1989 // we want to steer developers towards only using constant expressions.
1990 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001991 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001992 }
1993 if (globalInitWarning)
1994 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001995 warning(
1996 line,
1997 "global variable initializers should be constant expressions "
1998 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1999 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03002000 }
2001
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002002 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05302003 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
2004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002005 error(line, " cannot initialize this type of qualifier ",
2006 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002007 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002008 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02002009
2010 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
2011 intermSymbol->setLine(line);
2012
2013 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
2014 {
Olli Etuaho72e35892018-06-20 11:43:08 +03002015 assignError(line, "=", variable->getType(), initializer->getType());
Olli Etuahob60d30f2018-01-16 12:31:06 +02002016 return false;
2017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018
Arun Patole7e7e68d2015-05-22 12:02:25 +05302019 if (qualifier == EvqConst)
2020 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002021 // Save the constant folded value to the variable if possible.
2022 const TConstantUnion *constArray = initializer->getConstantValue();
2023 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302024 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002025 variable->shareConstPointer(constArray);
2026 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002027 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002028 ASSERT(*initNode == nullptr);
2029 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002030 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002031 }
2032 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002033
Olli Etuahob60d30f2018-01-16 12:31:06 +02002034 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002035 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002036 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002037 return true;
2038}
2039
2040TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002041 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002042 TIntermTyped *initializer,
2043 const TSourceLoc &loc)
2044{
2045 checkIsScalarBool(loc, pType);
2046 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002047 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002048 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002049 {
2050 // The initializer is valid. The init condition needs to have a node - either the
2051 // initializer node, or a constant node in case the initialized variable is const and won't
2052 // be recorded in the AST.
2053 if (initNode == nullptr)
2054 {
2055 return initializer;
2056 }
2057 else
2058 {
2059 TIntermDeclaration *declaration = new TIntermDeclaration();
2060 declaration->appendDeclarator(initNode);
2061 return declaration;
2062 }
2063 }
2064 return nullptr;
2065}
2066
2067TIntermNode *TParseContext::addLoop(TLoopType type,
2068 TIntermNode *init,
2069 TIntermNode *cond,
2070 TIntermTyped *expr,
2071 TIntermNode *body,
2072 const TSourceLoc &line)
2073{
2074 TIntermNode *node = nullptr;
2075 TIntermTyped *typedCond = nullptr;
2076 if (cond)
2077 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002078 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002079 typedCond = cond->getAsTyped();
2080 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002081 if (expr)
2082 {
2083 markStaticReadIfSymbol(expr);
2084 }
2085 // In case the loop body was not parsed as a block and contains a statement that simply refers
2086 // to a variable, we need to mark it as statically used.
2087 if (body)
2088 {
2089 markStaticReadIfSymbol(body);
2090 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002091 if (cond == nullptr || typedCond)
2092 {
Olli Etuahocce89652017-06-19 16:04:09 +03002093 if (type == ELoopDoWhile)
2094 {
2095 checkIsScalarBool(line, typedCond);
2096 }
2097 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2098 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2099 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2100 !typedCond->isVector()));
2101
Olli Etuaho3ec75682017-07-05 17:02:55 +03002102 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002103 node->setLine(line);
2104 return node;
2105 }
2106
Olli Etuahocce89652017-06-19 16:04:09 +03002107 ASSERT(type != ELoopDoWhile);
2108
Olli Etuaho914b79a2017-06-19 16:03:19 +03002109 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2110 ASSERT(declaration);
2111 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2112 ASSERT(declarator->getLeft()->getAsSymbolNode());
2113
2114 // The condition is a declaration. In the AST representation we don't support declarations as
2115 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2116 // the loop.
2117 TIntermBlock *block = new TIntermBlock();
2118
2119 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2120 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2121 block->appendStatement(declareCondition);
2122
2123 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2124 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002125 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002126 block->appendStatement(loop);
2127 loop->setLine(line);
2128 block->setLine(line);
2129 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130}
2131
Olli Etuahocce89652017-06-19 16:04:09 +03002132TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2133 TIntermNodePair code,
2134 const TSourceLoc &loc)
2135{
Olli Etuaho56229f12017-07-10 14:16:33 +03002136 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002137 // In case the conditional statements were not parsed as blocks and contain a statement that
2138 // simply refers to a variable, we need to mark them as statically used.
2139 if (code.node1)
2140 {
2141 markStaticReadIfSymbol(code.node1);
2142 }
2143 if (code.node2)
2144 {
2145 markStaticReadIfSymbol(code.node2);
2146 }
Olli Etuahocce89652017-06-19 16:04:09 +03002147
2148 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002149 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002150 {
2151 if (cond->getAsConstantUnion()->getBConst(0) == true)
2152 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002153 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002154 }
2155 else
2156 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002157 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002158 }
2159 }
2160
Olli Etuaho3ec75682017-07-05 17:02:55 +03002161 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002162 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002163 node->setLine(loc);
2164
2165 return node;
2166}
2167
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002168void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2169{
2170 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2171 typeSpecifier->getBasicType());
2172
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002173 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002174 {
2175 error(typeSpecifier->getLine(), "not supported", "first-class array");
2176 typeSpecifier->clearArrayness();
2177 }
2178}
2179
Martin Radev70866b82016-07-22 15:27:42 +03002180TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302181 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002183 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002184
Martin Radev70866b82016-07-22 15:27:42 +03002185 TPublicType returnType = typeSpecifier;
2186 returnType.qualifier = typeQualifier.qualifier;
2187 returnType.invariant = typeQualifier.invariant;
2188 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002189 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002190 returnType.precision = typeSpecifier.precision;
2191
2192 if (typeQualifier.precision != EbpUndefined)
2193 {
2194 returnType.precision = typeQualifier.precision;
2195 }
2196
Martin Radev4a9cd802016-09-01 16:51:51 +03002197 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2198 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002199
Martin Radev4a9cd802016-09-01 16:51:51 +03002200 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2201 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002202
Martin Radev4a9cd802016-09-01 16:51:51 +03002203 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002204
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002205 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002206 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002207 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002208 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002210 returnType.clearArrayness();
2211 }
2212
Martin Radev70866b82016-07-22 15:27:42 +03002213 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002214 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002215 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002216 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002217 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002218 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002219
Martin Radev70866b82016-07-22 15:27:42 +03002220 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002221 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002222 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002223 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002224 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002225 }
2226 }
2227 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002228 {
Martin Radev70866b82016-07-22 15:27:42 +03002229 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002230 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002231 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002232 }
Martin Radev70866b82016-07-22 15:27:42 +03002233 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2234 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002235 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002236 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2237 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002238 }
Martin Radev70866b82016-07-22 15:27:42 +03002239 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002240 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002241 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002242 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002243 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002244 }
2245
2246 return returnType;
2247}
2248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2250 const TPublicType &type,
2251 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002252{
2253 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002254 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002255 {
2256 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 }
2258
2259 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2260 switch (qualifier)
2261 {
2262 case EvqVertexIn:
2263 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002264 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 {
2266 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002268 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 return;
2270 case EvqFragmentOut:
2271 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002272 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002273 {
2274 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002276 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002277 return;
2278 default:
2279 break;
2280 }
2281
2282 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2283 // restrictions.
2284 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002285 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2286 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002287 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2288 {
2289 error(qualifierLocation, "must use 'flat' interpolation here",
2290 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002291 }
2292
Martin Radev4a9cd802016-09-01 16:51:51 +03002293 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002294 {
2295 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2296 // These restrictions are only implied by the ESSL 3.00 spec, but
2297 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002298 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002299 {
2300 error(qualifierLocation, "cannot be an array of structures",
2301 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002302 }
2303 if (type.isStructureContainingArrays())
2304 {
2305 error(qualifierLocation, "cannot be a structure containing an array",
2306 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002307 }
2308 if (type.isStructureContainingType(EbtStruct))
2309 {
2310 error(qualifierLocation, "cannot be a structure containing a structure",
2311 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002312 }
2313 if (type.isStructureContainingType(EbtBool))
2314 {
2315 error(qualifierLocation, "cannot be a structure containing a bool",
2316 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002317 }
2318 }
2319}
2320
Martin Radev2cc85b32016-08-05 16:22:53 +03002321void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2322{
2323 if (qualifier.getType() == QtStorage)
2324 {
2325 const TStorageQualifierWrapper &storageQualifier =
2326 static_cast<const TStorageQualifierWrapper &>(qualifier);
2327 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2328 !symbolTable.atGlobalLevel())
2329 {
2330 error(storageQualifier.getLine(),
2331 "Local variables can only use the const storage qualifier.",
Olli Etuaho1a3bbaa2018-01-25 11:41:31 +02002332 storageQualifier.getQualifierString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002333 }
2334 }
2335}
2336
Olli Etuaho43364892017-02-13 16:00:12 +00002337void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002338 const TSourceLoc &location)
2339{
Jiajia Qinbc585152017-06-23 15:42:17 +08002340 const std::string reason(
2341 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2342 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002343 if (memoryQualifier.readonly)
2344 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002345 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002346 }
2347 if (memoryQualifier.writeonly)
2348 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002349 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002350 }
Martin Radev049edfa2016-11-11 14:35:37 +02002351 if (memoryQualifier.coherent)
2352 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002353 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002354 }
2355 if (memoryQualifier.restrictQualifier)
2356 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002357 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002358 }
2359 if (memoryQualifier.volatileQualifier)
2360 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002361 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002362 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002363}
2364
jchen104cdac9e2017-05-08 11:01:20 +08002365// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2366// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002367void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2368 const TSourceLoc &loc,
2369 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002370{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002371 if (!IsAtomicCounter(type->getBasicType()))
2372 {
2373 return;
2374 }
2375
2376 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2377 : kAtomicCounterSize;
2378 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2379 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002380 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002381 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002382 {
2383 offset = bindingState.appendSpan(size);
2384 }
2385 else
2386 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002387 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002388 }
2389 if (offset == -1)
2390 {
2391 error(loc, "Offset overlapping", "atomic counter");
2392 return;
2393 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002394 layoutQualifier.offset = offset;
2395 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002396}
2397
Olli Etuaho454c34c2017-10-25 16:35:56 +03002398void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002399 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002400 TType *type)
2401{
2402 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2403 {
2404 if (type->isArray() && type->getOutermostArraySize() == 0u)
2405 {
2406 // Set size for the unsized geometry shader inputs if they are declared after a valid
2407 // input primitive declaration.
2408 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2409 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002410 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002411 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002412 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002413 }
2414 else
2415 {
2416 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2417 // An input can be declared without an array size if there is a previous layout
2418 // which specifies the size.
2419 error(location,
2420 "Missing a valid input primitive declaration before declaring an unsized "
2421 "array input",
2422 token);
2423 }
2424 }
2425 else if (type->isArray())
2426 {
2427 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2428 }
2429 else
2430 {
2431 error(location, "Geometry shader input variable must be declared as an array", token);
2432 }
2433 }
2434}
2435
Olli Etuaho13389b62016-10-16 11:48:18 +01002436TIntermDeclaration *TParseContext::parseSingleDeclaration(
2437 TPublicType &publicType,
2438 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002439 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002440{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002441 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002442 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2443 mDirectiveHandler.pragma().stdgl.invariantAll)
2444 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002445 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002446
2447 // The directive handler has already taken care of rejecting invalid uses of this pragma
2448 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2449 // affected variable declarations:
2450 //
2451 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2452 // elsewhere, in TranslatorGLSL.)
2453 //
2454 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2455 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2456 // the way this is currently implemented we have to enable this compiler option before
2457 // parsing the shader and determining the shading language version it uses. If this were
2458 // implemented as a post-pass, the workaround could be more targeted.
2459 //
2460 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2461 // the specification, but there are desktop OpenGL drivers that expect that this is the
2462 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2463 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2464 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002465 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002466 }
2467 }
2468
Olli Etuahofbb1c792018-01-19 16:26:59 +02002469 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002470
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2472 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002473
Jamie Madill50cf2be2018-06-15 09:46:57 -04002474 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002476
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002477 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002478 if (emptyDeclaration)
2479 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002480 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 // In most cases we don't need to create a symbol node for an empty declaration.
2482 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002483 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002484 {
Olli Etuaho195be942017-12-04 23:40:14 +02002485 TVariable *emptyVariable =
Jamie Madillb779b122018-06-20 11:46:43 -04002486 new TVariable(&symbolTable, kEmptyImmutableString, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002487 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 }
jchen104cdac9e2017-05-08 11:01:20 +08002489 else if (IsAtomicCounter(publicType.getBasicType()))
2490 {
2491 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2492 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002493 }
2494 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002495 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002496 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002497
Olli Etuahob60d30f2018-01-16 12:31:06 +02002498 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuahob60d30f2018-01-16 12:31:06 +02002500 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002501
Olli Etuaho2935c582015-04-08 14:32:06 +03002502 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002503 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 {
Olli Etuaho195be942017-12-04 23:40:14 +02002505 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002507 }
2508
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002509 TIntermDeclaration *declaration = new TIntermDeclaration();
2510 declaration->setLine(identifierOrTypeLocation);
2511 if (symbol)
2512 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002513 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002514 declaration->appendDeclarator(symbol);
2515 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002517}
2518
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002519TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2520 TPublicType &elementType,
2521 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002522 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002523 const TSourceLoc &indexLocation,
2524 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002525{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002526 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002527
Olli Etuaho55bde912017-10-25 13:41:13 +03002528 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002529 identifierLocation);
2530
Olli Etuaho55bde912017-10-25 13:41:13 +03002531 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002532
Olli Etuaho55bde912017-10-25 13:41:13 +03002533 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002534
Olli Etuahob60d30f2018-01-16 12:31:06 +02002535 TType *arrayType = new TType(elementType);
2536 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002537
Olli Etuahofbb1c792018-01-19 16:26:59 +02002538 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002539
Olli Etuahob60d30f2018-01-16 12:31:06 +02002540 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002541
Olli Etuahob60d30f2018-01-16 12:31:06 +02002542 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002543
Olli Etuaho13389b62016-10-16 11:48:18 +01002544 TIntermDeclaration *declaration = new TIntermDeclaration();
2545 declaration->setLine(identifierLocation);
2546
Olli Etuaho195be942017-12-04 23:40:14 +02002547 TVariable *variable = nullptr;
2548 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 {
Olli Etuaho195be942017-12-04 23:40:14 +02002550 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002551 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002552 declaration->appendDeclarator(symbol);
2553 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002554
Olli Etuaho13389b62016-10-16 11:48:18 +01002555 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002556}
2557
Olli Etuaho13389b62016-10-16 11:48:18 +01002558TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2559 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002560 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002561 const TSourceLoc &initLocation,
2562 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002563{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002564 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002565
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002566 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2567 identifierLocation);
2568
2569 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002570
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 TIntermDeclaration *declaration = new TIntermDeclaration();
2572 declaration->setLine(identifierLocation);
2573
2574 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002575 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002576 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002577 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002578 if (initNode)
2579 {
2580 declaration->appendDeclarator(initNode);
2581 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002582 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002583 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002584}
2585
Olli Etuaho13389b62016-10-16 11:48:18 +01002586TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002587 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002588 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002589 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002591 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002592 const TSourceLoc &initLocation,
2593 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002594{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002595 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002596
Olli Etuaho55bde912017-10-25 13:41:13 +03002597 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002598 identifierLocation);
2599
Olli Etuaho55bde912017-10-25 13:41:13 +03002600 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002601
Olli Etuaho55bde912017-10-25 13:41:13 +03002602 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002603
Olli Etuahob60d30f2018-01-16 12:31:06 +02002604 TType *arrayType = new TType(elementType);
2605 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002606
Olli Etuaho13389b62016-10-16 11:48:18 +01002607 TIntermDeclaration *declaration = new TIntermDeclaration();
2608 declaration->setLine(identifierLocation);
2609
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002610 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002611 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002612 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002613 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002614 if (initNode)
2615 {
2616 declaration->appendDeclarator(initNode);
2617 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002618 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002619
2620 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002621}
2622
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002623TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002624 const TTypeQualifierBuilder &typeQualifierBuilder,
2625 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002626 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002627 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002628{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002629 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002630
Martin Radev70866b82016-07-22 15:27:42 +03002631 if (!typeQualifier.invariant)
2632 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002633 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002634 return nullptr;
2635 }
2636 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2637 {
2638 return nullptr;
2639 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002640 if (!symbol)
2641 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002642 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002643 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002644 }
Martin Radev70866b82016-07-22 15:27:42 +03002645 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002646 {
Martin Radev70866b82016-07-22 15:27:42 +03002647 error(identifierLoc, "invariant declaration specifies qualifier",
2648 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002649 }
Martin Radev70866b82016-07-22 15:27:42 +03002650 if (typeQualifier.precision != EbpUndefined)
2651 {
2652 error(identifierLoc, "invariant declaration specifies precision",
2653 getPrecisionString(typeQualifier.precision));
2654 }
2655 if (!typeQualifier.layoutQualifier.isEmpty())
2656 {
2657 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2658 }
2659
2660 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002661 if (!variable)
2662 {
2663 return nullptr;
2664 }
Martin Radev70866b82016-07-22 15:27:42 +03002665 const TType &type = variable->getType();
2666
2667 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2668 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002669 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002670
Olli Etuaho76b2c382018-03-19 15:51:29 +02002671 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002672
Olli Etuaho195be942017-12-04 23:40:14 +02002673 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002674 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002675
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002676 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002677}
2678
Olli Etuaho13389b62016-10-16 11:48:18 +01002679void TParseContext::parseDeclarator(TPublicType &publicType,
2680 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002681 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002682 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002683{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002684 // If the declaration starting this declarator list was empty (example: int,), some checks were
2685 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002686 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002687 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002688 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2689 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002690 }
2691
Olli Etuaho856c4972016-08-08 11:38:39 +03002692 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002693
Olli Etuahob60d30f2018-01-16 12:31:06 +02002694 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002695
Olli Etuahofbb1c792018-01-19 16:26:59 +02002696 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002697
Olli Etuahob60d30f2018-01-16 12:31:06 +02002698 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002699
Olli Etuahob60d30f2018-01-16 12:31:06 +02002700 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002701
Olli Etuaho195be942017-12-04 23:40:14 +02002702 TVariable *variable = nullptr;
2703 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002704 {
Olli Etuaho195be942017-12-04 23:40:14 +02002705 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002706 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002707 declarationOut->appendDeclarator(symbol);
2708 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002709}
2710
Olli Etuaho55bde912017-10-25 13:41:13 +03002711void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002712 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002713 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002714 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002715 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002716 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002717{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002718 // If the declaration starting this declarator list was empty (example: int,), some checks were
2719 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002720 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002721 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002722 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002723 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002724 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002725
Olli Etuaho55bde912017-10-25 13:41:13 +03002726 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002727
Olli Etuaho55bde912017-10-25 13:41:13 +03002728 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002729 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002730 TType *arrayType = new TType(elementType);
2731 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002732
Olli Etuahofbb1c792018-01-19 16:26:59 +02002733 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002734
Olli Etuahob60d30f2018-01-16 12:31:06 +02002735 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002736
Olli Etuahob60d30f2018-01-16 12:31:06 +02002737 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002738
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002739 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002740 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002741 {
Olli Etuaho195be942017-12-04 23:40:14 +02002742 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002743 symbol->setLine(identifierLocation);
2744 declarationOut->appendDeclarator(symbol);
2745 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002746 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002747}
2748
Olli Etuaho13389b62016-10-16 11:48:18 +01002749void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2750 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002751 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002752 const TSourceLoc &initLocation,
2753 TIntermTyped *initializer,
2754 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002755{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002756 // If the declaration starting this declarator list was empty (example: int,), some checks were
2757 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002759 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002760 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2761 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002762 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002763
Olli Etuaho856c4972016-08-08 11:38:39 +03002764 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002765
Olli Etuaho13389b62016-10-16 11:48:18 +01002766 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002767 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002769 {
2770 //
2771 // build the intermediate representation
2772 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002773 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002774 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002775 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002776 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002777 }
2778}
2779
Olli Etuaho55bde912017-10-25 13:41:13 +03002780void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002781 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002782 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002783 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002784 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002785 const TSourceLoc &initLocation,
2786 TIntermTyped *initializer,
2787 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002788{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002789 // If the declaration starting this declarator list was empty (example: int,), some checks were
2790 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002791 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002792 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002793 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002794 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002795 }
2796
Olli Etuaho55bde912017-10-25 13:41:13 +03002797 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002798
Olli Etuaho55bde912017-10-25 13:41:13 +03002799 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002800
Olli Etuahob60d30f2018-01-16 12:31:06 +02002801 TType *arrayType = new TType(elementType);
2802 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002803
2804 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002805 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002806 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002807 {
2808 if (initNode)
2809 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002810 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002811 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002812 }
2813}
2814
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002815TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2816{
2817 // It's simpler to parse an empty statement as a constant expression rather than having a
2818 // different type of node just for empty statements, that will be pruned from the AST anyway.
2819 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2820 node->setLine(location);
2821 return node;
2822}
2823
jchen104cdac9e2017-05-08 11:01:20 +08002824void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2825 const TSourceLoc &location)
2826{
2827 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2828 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2829 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2830 {
2831 error(location, "Requires both binding and offset", "layout");
2832 return;
2833 }
2834 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2835}
2836
Olli Etuahocce89652017-06-19 16:04:09 +03002837void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2838 const TPublicType &type,
2839 const TSourceLoc &loc)
2840{
2841 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2842 !getFragmentPrecisionHigh())
2843 {
2844 error(loc, "precision is not supported in fragment shader", "highp");
2845 }
2846
2847 if (!CanSetDefaultPrecisionOnType(type))
2848 {
2849 error(loc, "illegal type argument for default precision qualifier",
2850 getBasicString(type.getBasicType()));
2851 return;
2852 }
2853 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2854}
2855
Shaob5cc1192017-07-06 10:47:20 +08002856bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2857{
2858 switch (typeQualifier.layoutQualifier.primitiveType)
2859 {
2860 case EptLines:
2861 case EptLinesAdjacency:
2862 case EptTriangles:
2863 case EptTrianglesAdjacency:
2864 return typeQualifier.qualifier == EvqGeometryIn;
2865
2866 case EptLineStrip:
2867 case EptTriangleStrip:
2868 return typeQualifier.qualifier == EvqGeometryOut;
2869
2870 case EptPoints:
2871 return true;
2872
2873 default:
2874 UNREACHABLE();
2875 return false;
2876 }
2877}
2878
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002879void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2880 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002881{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002882 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002883 {
2884 error(line,
2885 "Array size or input primitive declaration doesn't match the size of earlier sized "
2886 "array inputs.",
2887 "layout");
2888 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002889}
2890
Shaob5cc1192017-07-06 10:47:20 +08002891bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2892{
2893 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2894
2895 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2896
2897 if (layoutQualifier.maxVertices != -1)
2898 {
2899 error(typeQualifier.line,
2900 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2901 return false;
2902 }
2903
2904 // Set mGeometryInputPrimitiveType if exists
2905 if (layoutQualifier.primitiveType != EptUndefined)
2906 {
2907 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2908 {
2909 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2910 return false;
2911 }
2912
2913 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2914 {
2915 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002916 setGeometryShaderInputArraySize(
2917 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2918 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002919 }
2920 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2921 {
2922 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2923 "layout");
2924 return false;
2925 }
2926 }
2927
2928 // Set mGeometryInvocations if exists
2929 if (layoutQualifier.invocations > 0)
2930 {
2931 if (mGeometryShaderInvocations == 0)
2932 {
2933 mGeometryShaderInvocations = layoutQualifier.invocations;
2934 }
2935 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2936 {
2937 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2938 "layout");
2939 return false;
2940 }
2941 }
2942
2943 return true;
2944}
2945
2946bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2947{
2948 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2949
2950 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2951
2952 if (layoutQualifier.invocations > 0)
2953 {
2954 error(typeQualifier.line,
2955 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2956 return false;
2957 }
2958
2959 // Set mGeometryOutputPrimitiveType if exists
2960 if (layoutQualifier.primitiveType != EptUndefined)
2961 {
2962 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2963 {
2964 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2965 return false;
2966 }
2967
2968 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2969 {
2970 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2971 }
2972 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2973 {
2974 error(typeQualifier.line,
2975 "primitive doesn't match earlier output primitive declaration", "layout");
2976 return false;
2977 }
2978 }
2979
2980 // Set mGeometryMaxVertices if exists
2981 if (layoutQualifier.maxVertices > -1)
2982 {
2983 if (mGeometryShaderMaxVertices == -1)
2984 {
2985 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2986 }
2987 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2988 {
2989 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2990 "layout");
2991 return false;
2992 }
2993 }
2994
2995 return true;
2996}
2997
Martin Radev70866b82016-07-22 15:27:42 +03002998void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002999{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003000 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04003001 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04003002
Martin Radev70866b82016-07-22 15:27:42 +03003003 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
3004 typeQualifier.line);
3005
Jamie Madillc2128ff2016-07-04 10:26:17 -04003006 // It should never be the case, but some strange parser errors can send us here.
3007 if (layoutQualifier.isEmpty())
3008 {
3009 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04003010 return;
3011 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003012
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003014 {
Olli Etuaho43364892017-02-13 16:00:12 +00003015 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003016 return;
3017 }
3018
Olli Etuahoa78092c2018-09-26 14:16:13 +03003019 checkIndexIsNotSpecified(typeQualifier.line, layoutQualifier.index);
3020
Olli Etuaho43364892017-02-13 16:00:12 +00003021 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3022
3023 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003024
3025 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3026
Andrei Volykhina5527072017-03-22 16:46:30 +03003027 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3028
jchen104cdac9e2017-05-08 11:01:20 +08003029 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3030
Qin Jiajiaca68d982017-09-18 16:41:56 +08003031 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3032 typeQualifier.qualifier);
3033
Martin Radev802abe02016-08-04 17:48:32 +03003034 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003035 {
Martin Radev802abe02016-08-04 17:48:32 +03003036 if (mComputeShaderLocalSizeDeclared &&
3037 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3038 {
3039 error(typeQualifier.line, "Work group size does not match the previous declaration",
3040 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003041 return;
3042 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003043
Martin Radev802abe02016-08-04 17:48:32 +03003044 if (mShaderVersion < 310)
3045 {
3046 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003047 return;
3048 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003049
Martin Radev4c4c8e72016-08-04 12:25:34 +03003050 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003051 {
3052 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003053 return;
3054 }
3055
3056 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003057 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003058
3059 const TConstantUnion *maxComputeWorkGroupSizeData =
3060 maxComputeWorkGroupSize->getConstPointer();
3061
3062 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3063 {
3064 if (layoutQualifier.localSize[i] != -1)
3065 {
3066 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3067 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3068 if (mComputeShaderLocalSize[i] < 1 ||
3069 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3070 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003071 std::stringstream reasonStream;
3072 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3073 << maxComputeWorkGroupSizeValue;
3074 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003075
Olli Etuaho4de340a2016-12-16 09:32:03 +00003076 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003077 return;
3078 }
3079 }
3080 }
3081
3082 mComputeShaderLocalSizeDeclared = true;
3083 }
Shaob5cc1192017-07-06 10:47:20 +08003084 else if (typeQualifier.qualifier == EvqGeometryIn)
3085 {
3086 if (mShaderVersion < 310)
3087 {
3088 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3089 return;
3090 }
3091
3092 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3093 {
3094 return;
3095 }
3096 }
3097 else if (typeQualifier.qualifier == EvqGeometryOut)
3098 {
3099 if (mShaderVersion < 310)
3100 {
3101 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3102 "layout");
3103 return;
3104 }
3105
3106 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3107 {
3108 return;
3109 }
3110 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003111 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3112 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003113 {
3114 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3115 // specification.
3116 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3117 {
3118 error(typeQualifier.line, "Number of views does not match the previous declaration",
3119 "layout");
3120 return;
3121 }
3122
3123 if (layoutQualifier.numViews == -1)
3124 {
3125 error(typeQualifier.line, "No num_views specified", "layout");
3126 return;
3127 }
3128
3129 if (layoutQualifier.numViews > mMaxNumViews)
3130 {
3131 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3132 "layout");
3133 return;
3134 }
3135
3136 mNumViews = layoutQualifier.numViews;
3137 }
Martin Radev802abe02016-08-04 17:48:32 +03003138 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003139 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003140 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003141 {
Martin Radev802abe02016-08-04 17:48:32 +03003142 return;
3143 }
3144
Jiajia Qinbc585152017-06-23 15:42:17 +08003145 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003146 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003147 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003148 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003149 return;
3150 }
3151
3152 if (mShaderVersion < 300)
3153 {
3154 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3155 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003156 return;
3157 }
3158
Olli Etuaho09b04a22016-12-15 13:30:26 +00003159 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003160
3161 if (layoutQualifier.matrixPacking != EmpUnspecified)
3162 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003163 if (typeQualifier.qualifier == EvqUniform)
3164 {
3165 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3166 }
3167 else if (typeQualifier.qualifier == EvqBuffer)
3168 {
3169 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3170 }
Martin Radev802abe02016-08-04 17:48:32 +03003171 }
3172
3173 if (layoutQualifier.blockStorage != EbsUnspecified)
3174 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003175 if (typeQualifier.qualifier == EvqUniform)
3176 {
3177 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3178 }
3179 else if (typeQualifier.qualifier == EvqBuffer)
3180 {
3181 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3182 }
Martin Radev802abe02016-08-04 17:48:32 +03003183 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003184 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003185}
3186
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003187TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3188 const TFunction &function,
3189 const TSourceLoc &location,
3190 bool insertParametersToSymbolTable)
3191{
Olli Etuahobed35d72017-12-20 16:36:26 +02003192 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003193
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003194 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003195 prototype->setLine(location);
3196
3197 for (size_t i = 0; i < function.getParamCount(); i++)
3198 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003199 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003200
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003201 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3202 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003203 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003204 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003205 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003206 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003207 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003208 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003209 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003210 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003211 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003212 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003213 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003214 }
3215 else
3216 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003217 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003218 {
3219 error(location, "function parameter array must be sized at compile time", "[]");
3220 // We don't need to size the arrays since the parameter is unnamed and hence
3221 // inaccessible.
3222 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003223 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003224 }
3225 return prototype;
3226}
3227
Olli Etuaho16c745a2017-01-16 17:02:27 +00003228TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3229 const TFunction &parsedFunction,
3230 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003231{
Olli Etuaho476197f2016-10-11 13:59:08 +01003232 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3233 // first declaration. Either way the instance in the symbol table is used to track whether the
3234 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003235 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003236 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003237 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3238
3239 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003240 {
3241 // ESSL 1.00.17 section 4.2.7.
3242 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3243 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003244 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003245
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003246 TIntermFunctionPrototype *prototype =
3247 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003248
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003249 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003250
3251 if (!symbolTable.atGlobalLevel())
3252 {
3253 // ESSL 3.00.4 section 4.2.4.
3254 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003255 }
3256
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003257 return prototype;
3258}
3259
Olli Etuaho336b1472016-10-05 16:37:55 +01003260TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003261 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003262 TIntermBlock *functionBody,
3263 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003265 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003266 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3267 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003268 error(location,
3269 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003270 }
3271
Olli Etuahof51fdd22016-10-03 10:03:40 +01003272 if (functionBody == nullptr)
3273 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003274 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003275 functionBody->setLine(location);
3276 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003277 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003278 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003279 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003280
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003281 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003282 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003283}
3284
Olli Etuaho476197f2016-10-11 13:59:08 +01003285void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003286 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003287 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003288{
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003290
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003291 bool wasDefined = false;
3292 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3293 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003294 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003295 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003296 }
Jamie Madill185fb402015-06-12 15:48:48 -04003297
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003298 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003299 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003300 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003301
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003302 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003303 setLoopNestingLevel(0);
3304}
3305
Jamie Madillb98c3a82015-07-23 14:26:04 -04003306TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003307{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003308 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003309 // We don't know at this point whether this is a function definition or a prototype.
3310 // The definition production code will check for redefinitions.
3311 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003312 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303313
Olli Etuahod80f2942017-11-06 12:44:45 +02003314 for (size_t i = 0u; i < function->getParamCount(); ++i)
3315 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003316 const TVariable *param = function->getParam(i);
3317 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003318 {
3319 // ESSL 3.00.6 section 12.10.
3320 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003321 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003322 }
3323 }
3324
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003325 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303326 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003327 const UnmangledBuiltIn *builtIn =
3328 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3329 if (builtIn &&
3330 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3331 {
3332 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3333 // functions. Therefore overloading or redefining builtin functions is an error.
3334 error(location, "Name of a built-in function cannot be redeclared as function",
3335 function->name());
3336 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303337 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003338 else
3339 {
3340 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3341 // this applies to redeclarations as well.
3342 const TSymbol *builtIn =
3343 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3344 if (builtIn)
3345 {
3346 error(location, "built-in functions cannot be redefined", function->name());
3347 }
3348 }
3349
3350 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3351 // here.
3352 const TFunction *prevDec =
3353 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3354 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003355 {
3356 if (prevDec->getReturnType() != function->getReturnType())
3357 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003358 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003359 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003360 }
3361 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3362 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003363 if (prevDec->getParam(i)->getType().getQualifier() !=
3364 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003365 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003366 error(location,
3367 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003368 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003369 }
3370 }
3371 }
3372
Jamie Madill185fb402015-06-12 15:48:48 -04003373 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003374 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3375 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003376 if (prevSym)
3377 {
3378 if (!prevSym->isFunction())
3379 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003380 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003382 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003383 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003384 // Parsing is at the inner scope level of the function's arguments and body statement at this
3385 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3386 // scope.
3387 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003388
Olli Etuaho78d13742017-01-18 13:06:10 +00003389 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003390 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003391 {
3392 if (function->getParamCount() > 0)
3393 {
3394 error(location, "function cannot take any parameter(s)", "main");
3395 }
3396 if (function->getReturnType().getBasicType() != EbtVoid)
3397 {
3398 error(location, "main function cannot return a value",
3399 function->getReturnType().getBasicString());
3400 }
3401 }
3402
Jamie Madill185fb402015-06-12 15:48:48 -04003403 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003404 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3405 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003406 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3407 //
3408 return function;
3409}
3410
Olli Etuaho9de84a52016-06-14 17:36:01 +03003411TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003412 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003413 const TSourceLoc &location)
3414{
3415 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3416 {
3417 error(location, "no qualifiers allowed for function return",
3418 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419 }
3420 if (!type.layoutQualifier.isEmpty())
3421 {
3422 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423 }
jchen10cc2a10e2017-05-03 14:05:12 +08003424 // make sure an opaque type is not involved as well...
3425 std::string reason(getBasicString(type.getBasicType()));
3426 reason += "s can't be function return values";
3427 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003428 if (mShaderVersion < 300)
3429 {
3430 // Array return values are forbidden, but there's also no valid syntax for declaring array
3431 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003432 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003433
3434 if (type.isStructureContainingArrays())
3435 {
3436 // ESSL 1.00.17 section 6.1 Function Definitions
Olli Etuaho72e35892018-06-20 11:43:08 +03003437 TInfoSinkBase typeString;
3438 typeString << TType(type);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003439 error(location, "structures containing arrays can't be function return values",
Olli Etuaho72e35892018-06-20 11:43:08 +03003440 typeString.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003441 }
3442 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003443
3444 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003445 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003446}
3447
Olli Etuaho697bf652018-02-16 11:50:54 +02003448TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3449 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003450{
Olli Etuaho697bf652018-02-16 11:50:54 +02003451 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003452}
3453
Olli Etuaho95ed1942018-02-01 14:01:19 +02003454TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003455{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003456 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003457 {
3458 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3459 "[]");
3460 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003461 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003462 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003463 error(publicType.getLine(), "constructor can't be a structure definition",
3464 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003465 }
3466
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003467 TType *type = new TType(publicType);
3468 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003469 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003470 error(publicType.getLine(), "cannot construct this type",
3471 getBasicString(publicType.getBasicType()));
3472 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003473 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003474 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003475}
3476
Olli Etuaho55bde912017-10-25 13:41:13 +03003477void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3478 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003479 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003480 TType *arrayType)
3481{
3482 if (arrayType->isUnsizedArray())
3483 {
3484 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003485 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003486 }
3487}
3488
3489TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003490 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003491 const TSourceLoc &nameLoc)
3492{
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003494 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003495 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003496 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003497 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003498 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003499 checkIsNotReserved(nameLoc, name);
3500 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003501 return param;
3502}
3503
Olli Etuaho55bde912017-10-25 13:41:13 +03003504TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003505 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003506 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003507{
Olli Etuaho55bde912017-10-25 13:41:13 +03003508 TType *type = new TType(publicType);
3509 return parseParameterDeclarator(type, name, nameLoc);
3510}
3511
Olli Etuahofbb1c792018-01-19 16:26:59 +02003512TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003513 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003514 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003515 const TSourceLoc &arrayLoc,
3516 TPublicType *elementType)
3517{
3518 checkArrayElementIsNotArray(arrayLoc, *elementType);
3519 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003520 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003521 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003522}
3523
Olli Etuaho95ed1942018-02-01 14:01:19 +02003524bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3525 const TIntermSequence &arguments,
3526 TType type,
3527 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003529 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003530 {
3531 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3532 return false;
3533 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003534 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003535 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003536 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003537 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003538 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3539 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003540 {
3541 error(line, "constructing from a non-dereferenced array", "constructor");
3542 return false;
3543 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003544 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003545 {
3546 if (dimensionalityFromElement == 1u)
3547 {
3548 error(line, "implicitly sized array of arrays constructor argument is not an array",
3549 "constructor");
3550 }
3551 else
3552 {
3553 error(line,
3554 "implicitly sized array of arrays constructor argument dimensionality is too "
3555 "low",
3556 "constructor");
3557 }
3558 return false;
3559 }
3560 }
3561 return true;
3562}
3563
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564// This function is used to test for the correctness of the parameters passed to various constructor
3565// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566//
Olli Etuaho856c4972016-08-08 11:38:39 +03003567// 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 +00003568//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003569TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003570{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003571 TType type = fnCall->constructorType();
3572 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003573 if (type.isUnsizedArray())
3574 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003575 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003576 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003577 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003578 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003579 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003580 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003581 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003582 if (type.getOutermostArraySize() == 0u)
3583 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003584 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003585 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003590 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003591 }
3592 }
3593 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003594 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003595
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003596 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003598 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003599 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003600
Olli Etuaho95ed1942018-02-01 14:01:19 +02003601 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003602 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003603
Olli Etuaho765924f2018-01-04 12:48:36 +02003604 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003605}
3606
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003607//
3608// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003609// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003610//
Olli Etuaho13389b62016-10-16 11:48:18 +01003611TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003612 const TTypeQualifierBuilder &typeQualifierBuilder,
3613 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003614 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003615 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003616 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003617 const TSourceLoc &instanceLine,
3618 TIntermTyped *arrayIndex,
3619 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003620{
Olli Etuaho856c4972016-08-08 11:38:39 +03003621 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003622
Olli Etuaho77ba4082016-12-16 12:01:18 +00003623 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003624
Jiajia Qinbc585152017-06-23 15:42:17 +08003625 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003627 error(typeQualifier.line,
3628 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3629 "3.10",
3630 getQualifierString(typeQualifier.qualifier));
3631 }
3632 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3633 {
3634 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003635 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636 }
3637
Martin Radev70866b82016-07-22 15:27:42 +03003638 if (typeQualifier.invariant)
3639 {
3640 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3641 }
3642
Jiajia Qinbc585152017-06-23 15:42:17 +08003643 if (typeQualifier.qualifier != EvqBuffer)
3644 {
3645 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3646 }
Olli Etuaho43364892017-02-13 16:00:12 +00003647
jchen10af713a22017-04-19 09:10:56 +08003648 // add array index
3649 unsigned int arraySize = 0;
3650 if (arrayIndex != nullptr)
3651 {
3652 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3653 }
3654
Olli Etuahoa78092c2018-09-26 14:16:13 +03003655 checkIndexIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.index);
3656
jchen10af713a22017-04-19 09:10:56 +08003657 if (mShaderVersion < 310)
3658 {
3659 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3660 }
3661 else
3662 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003663 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3664 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003665 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003666
Andrei Volykhina5527072017-03-22 16:46:30 +03003667 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3668
Jamie Madill099c0f32013-06-20 11:55:52 -04003669 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003670 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003671 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3672 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003673
Jamie Madill099c0f32013-06-20 11:55:52 -04003674 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3675 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003676 if (typeQualifier.qualifier == EvqUniform)
3677 {
3678 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3679 }
3680 else if (typeQualifier.qualifier == EvqBuffer)
3681 {
3682 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3683 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003684 }
3685
Jamie Madill1566ef72013-06-20 11:55:54 -04003686 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3687 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003688 if (typeQualifier.qualifier == EvqUniform)
3689 {
3690 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3691 }
3692 else if (typeQualifier.qualifier == EvqBuffer)
3693 {
3694 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3695 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003696 }
3697
Olli Etuaho856c4972016-08-08 11:38:39 +03003698 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003699
Martin Radev2cc85b32016-08-05 16:22:53 +03003700 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3701
Jamie Madill98493dd2013-07-08 14:39:03 -04003702 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303703 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3704 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003705 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303706 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003707 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303708 {
jchen10cc2a10e2017-05-03 14:05:12 +08003709 std::string reason("unsupported type - ");
3710 reason += fieldType->getBasicString();
3711 reason += " types are not allowed in interface blocks";
3712 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003713 }
3714
Jamie Madill98493dd2013-07-08 14:39:03 -04003715 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003716 switch (qualifier)
3717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003719 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003720 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003721 if (typeQualifier.qualifier == EvqBuffer)
3722 {
3723 error(field->line(), "invalid qualifier on shader storage block member",
3724 getQualifierString(qualifier));
3725 }
3726 break;
3727 case EvqBuffer:
3728 if (typeQualifier.qualifier == EvqUniform)
3729 {
3730 error(field->line(), "invalid qualifier on uniform block member",
3731 getQualifierString(qualifier));
3732 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003733 break;
3734 default:
3735 error(field->line(), "invalid qualifier on interface block member",
3736 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003738 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003739
Martin Radev70866b82016-07-22 15:27:42 +03003740 if (fieldType->isInvariant())
3741 {
3742 error(field->line(), "invalid qualifier on interface block member", "invariant");
3743 }
3744
Jamie Madilla5efff92013-06-06 11:56:47 -04003745 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003746 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003747 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Olli Etuahoa78092c2018-09-26 14:16:13 +03003748 checkIndexIsNotSpecified(field->line(), fieldLayoutQualifier.index);
jchen10af713a22017-04-19 09:10:56 +08003749 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003750
Jamie Madill98493dd2013-07-08 14:39:03 -04003751 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003752 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003753 error(field->line(), "invalid layout qualifier: cannot be used here",
3754 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003755 }
3756
Jamie Madill98493dd2013-07-08 14:39:03 -04003757 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003758 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003759 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003760 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003761 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003762 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003763 warning(field->line(),
3764 "extraneous layout qualifier: only has an effect on matrix types",
3765 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003766 }
3767
Jamie Madill98493dd2013-07-08 14:39:03 -04003768 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003769
Olli Etuahoebee5b32017-11-23 12:56:32 +02003770 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3771 typeQualifier.qualifier != EvqBuffer)
3772 {
3773 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3774 checkIsNotUnsizedArray(field->line(),
3775 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003776 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003777 }
3778
Jiajia Qinbc585152017-06-23 15:42:17 +08003779 if (typeQualifier.qualifier == EvqBuffer)
3780 {
3781 // set memory qualifiers
3782 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3783 // qualified with a memory qualifier, it is as if all of its members were declared with
3784 // the same memory qualifier.
3785 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3786 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3787 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3788 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3789 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3790 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3791 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3792 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3793 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3794 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3795 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003796 }
3797
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003798 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003799 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003800 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003801 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003802 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003803 }
3804
Olli Etuahob60d30f2018-01-16 12:31:06 +02003805 TType *interfaceBlockType =
3806 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003807 if (arrayIndex != nullptr)
3808 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003809 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003810 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003811
Olli Etuaho195be942017-12-04 23:40:14 +02003812 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003813 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3814 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003815 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003816 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003817 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003818
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003819 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003820 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003821 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003822 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003824 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003825 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003826
3827 // set parent pointer of the field variable
3828 fieldType->setInterfaceBlock(interfaceBlock);
3829
Olli Etuahob60d30f2018-01-16 12:31:06 +02003830 fieldType->setQualifier(typeQualifier.qualifier);
3831
Olli Etuaho195be942017-12-04 23:40:14 +02003832 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003833 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003834 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303835 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003836 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003837 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003838 }
3839 }
3840 }
3841 else
3842 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003843 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003844
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003845 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003846 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303847 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003848 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003849 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003850 }
3851
Olli Etuaho195be942017-12-04 23:40:14 +02003852 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3853 blockSymbol->setLine(typeQualifier.line);
3854 TIntermDeclaration *declaration = new TIntermDeclaration();
3855 declaration->appendDeclarator(blockSymbol);
3856 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003857
3858 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003859 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003860}
3861
Olli Etuahofbb1c792018-01-19 16:26:59 +02003862void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3863 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003864{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003865 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003866
3867 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003868 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303869 if (mStructNestingLevel > 1)
3870 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003871 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873}
3874
3875void TParseContext::exitStructDeclaration()
3876{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003877 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003878}
3879
Olli Etuaho8a176262016-08-16 14:23:01 +03003880void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881{
Jamie Madillacb4b812016-11-07 13:50:29 -05003882 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303883 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003884 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885 }
3886
Arun Patole7e7e68d2015-05-22 12:02:25 +05303887 if (field.type()->getBasicType() != EbtStruct)
3888 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003889 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890 }
3891
3892 // We're already inside a structure definition at this point, so add
3893 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303894 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3895 {
Jamie Madill41a49272014-03-18 16:10:13 -04003896 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003897 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3898 {
3899 // This may happen in case there are nested struct definitions. While they are also
3900 // invalid GLSL, they don't cause a syntax error.
3901 reasonStream << "Struct nesting";
3902 }
3903 else
3904 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003905 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003906 }
3907 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003908 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003909 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003910 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003911 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003912}
3913
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003914//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003915// Parse an array index expression
3916//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3918 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303919 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003921 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3922 {
3923 if (baseExpression->getAsSymbolNode())
3924 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303925 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003926 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 }
3928 else
3929 {
3930 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3931 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003932
Olli Etuaho3ec75682017-07-05 17:02:55 +03003933 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003935
Jiawei Shaod8105a02017-08-08 09:54:36 +08003936 if (baseExpression->getQualifier() == EvqPerVertexIn)
3937 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003938 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003939 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3940 {
3941 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3942 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3943 }
3944 }
3945
Jamie Madill21c1e452014-12-29 11:33:41 -05003946 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3947
Olli Etuaho36b05142015-11-12 13:10:42 +02003948 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3949 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3950 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3951 // index is a constant expression.
3952 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3953 {
3954 if (baseExpression->isInterfaceBlock())
3955 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003956 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003957 switch (baseExpression->getQualifier())
3958 {
3959 case EvqPerVertexIn:
3960 break;
3961 case EvqUniform:
3962 case EvqBuffer:
3963 error(location,
3964 "array indexes for uniform block arrays and shader storage block arrays "
3965 "must be constant integral expressions",
3966 "[");
3967 break;
3968 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003969 // We can reach here only in error cases.
3970 ASSERT(mDiagnostics->numErrors() > 0);
3971 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003972 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003973 }
3974 else if (baseExpression->getQualifier() == EvqFragmentOut)
3975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003976 error(location,
3977 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003978 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003979 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3980 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003981 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003982 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003983 }
3984
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003985 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003986 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003987 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3988 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3989 // constant fold expressions that are not constant expressions). The most compatible way to
3990 // handle this case is to report a warning instead of an error and force the index to be in
3991 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003992 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003993 int index = 0;
3994 if (indexConstantUnion->getBasicType() == EbtInt)
3995 {
3996 index = indexConstantUnion->getIConst(0);
3997 }
3998 else if (indexConstantUnion->getBasicType() == EbtUInt)
3999 {
4000 index = static_cast<int>(indexConstantUnion->getUConst(0));
4001 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002
4003 int safeIndex = -1;
4004
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004006 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004007 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4008 safeIndex = 0;
4009 }
4010
4011 if (!baseExpression->getType().isUnsizedArray())
4012 {
4013 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004017 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4018 {
4019 outOfRangeError(outOfRangeIndexIsError, location,
4020 "array index for gl_FragData must be zero when "
4021 "GL_EXT_draw_buffers is disabled",
4022 "[]");
4023 safeIndex = 0;
4024 }
4025 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004026 }
4027 // Only do generic out-of-range check if similar error hasn't already been reported.
4028 if (safeIndex < 0)
4029 {
4030 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031 {
4032 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4033 baseExpression->getOutermostArraySize(),
4034 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004035 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004036 else if (baseExpression->isMatrix())
4037 {
4038 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4039 baseExpression->getType().getCols(),
4040 "matrix field selection out of range");
4041 }
4042 else
4043 {
4044 ASSERT(baseExpression->isVector());
4045 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4046 baseExpression->getType().getNominalSize(),
4047 "vector field selection out of range");
4048 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004049 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004050
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 ASSERT(safeIndex >= 0);
4052 // Data of constant unions can't be changed, because it may be shared with other
4053 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4054 // sanitized object.
4055 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4056 {
4057 TConstantUnion *safeConstantUnion = new TConstantUnion();
4058 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004059 indexExpression = new TIntermConstantUnion(
4060 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4061 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004062 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004063
Olli Etuahoebee5b32017-11-23 12:56:32 +02004064 TIntermBinary *node =
4065 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4066 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004067 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004068 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004069 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004070
Olli Etuaho94bbed12018-03-20 14:44:53 +02004071 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004072 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4073 node->setLine(location);
4074 // Indirect indexing can never be constant folded.
4075 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076}
4077
Olli Etuahoebee5b32017-11-23 12:56:32 +02004078int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4079 const TSourceLoc &location,
4080 int index,
4081 int arraySize,
4082 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004083{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084 // Should not reach here with an unsized / runtime-sized array.
4085 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004086 // A negative index should already have been checked.
4087 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004088 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004089 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004090 std::stringstream reasonStream;
4091 reasonStream << reason << " '" << index << "'";
4092 std::string token = reasonStream.str();
4093 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095 }
4096 return index;
4097}
4098
Jamie Madillb98c3a82015-07-23 14:26:04 -04004099TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4100 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004101 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004102 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004103{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 if (baseExpression->isArray())
4105 {
4106 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 }
4109
4110 if (baseExpression->isVector())
4111 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004112 TVector<int> fieldOffsets;
4113 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4114 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004116 fieldOffsets.resize(1);
4117 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004119 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4120 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121
Olli Etuaho765924f2018-01-04 12:48:36 +02004122 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004123 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 else if (baseExpression->getBasicType() == EbtStruct)
4125 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304126 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 {
4129 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004130 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 }
4132 else
4133 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004134 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004136 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 {
4140 fieldFound = true;
4141 break;
4142 }
4143 }
4144 if (fieldFound)
4145 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004146 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004147 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004148 TIntermBinary *node =
4149 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4150 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004151 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004152 }
4153 else
4154 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004155 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004156 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004157 }
4158 }
4159 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004160 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304162 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004163 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 {
4165 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004166 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 }
4168 else
4169 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004170 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004172 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004174 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004175 {
4176 fieldFound = true;
4177 break;
4178 }
4179 }
4180 if (fieldFound)
4181 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004182 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004183 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004184 TIntermBinary *node =
4185 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4186 node->setLine(dotLocation);
4187 // Indexing interface blocks can never be constant folded.
4188 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004189 }
4190 else
4191 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004192 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004193 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004194 }
4195 }
4196 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004197 else
4198 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004199 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004201 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004202 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004203 }
4204 else
4205 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304206 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004207 " field selection requires structure, vector, or interface block on left hand "
4208 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004209 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004210 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004211 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004212 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004213}
4214
Olli Etuahofbb1c792018-01-19 16:26:59 +02004215TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004216 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217{
Jamie Madill2f294c92017-11-20 14:47:26 -05004218 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004219
4220 if (qualifierType == "shared")
4221 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004222 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004223 {
4224 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4225 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004226 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004227 }
4228 else if (qualifierType == "packed")
4229 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004230 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004231 {
4232 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4233 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004234 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004236 else if (qualifierType == "std430")
4237 {
4238 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4239 qualifier.blockStorage = EbsStd430;
4240 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 else if (qualifierType == "std140")
4242 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004243 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004244 }
4245 else if (qualifierType == "row_major")
4246 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004247 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004248 }
4249 else if (qualifierType == "column_major")
4250 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004251 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004252 }
4253 else if (qualifierType == "location")
4254 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004255 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004256 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004257 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004258 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004259 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004260 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4261 {
4262 qualifier.yuv = true;
4263 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004264 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004265 else if (qualifierType == "rgba32f")
4266 {
4267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4268 qualifier.imageInternalFormat = EiifRGBA32F;
4269 }
4270 else if (qualifierType == "rgba16f")
4271 {
4272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4273 qualifier.imageInternalFormat = EiifRGBA16F;
4274 }
4275 else if (qualifierType == "r32f")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifR32F;
4279 }
4280 else if (qualifierType == "rgba8")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA8;
4284 }
4285 else if (qualifierType == "rgba8_snorm")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4289 }
4290 else if (qualifierType == "rgba32i")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.imageInternalFormat = EiifRGBA32I;
4294 }
4295 else if (qualifierType == "rgba16i")
4296 {
4297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4298 qualifier.imageInternalFormat = EiifRGBA16I;
4299 }
4300 else if (qualifierType == "rgba8i")
4301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.imageInternalFormat = EiifRGBA8I;
4304 }
4305 else if (qualifierType == "r32i")
4306 {
4307 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4308 qualifier.imageInternalFormat = EiifR32I;
4309 }
4310 else if (qualifierType == "rgba32ui")
4311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.imageInternalFormat = EiifRGBA32UI;
4314 }
4315 else if (qualifierType == "rgba16ui")
4316 {
4317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4318 qualifier.imageInternalFormat = EiifRGBA16UI;
4319 }
4320 else if (qualifierType == "rgba8ui")
4321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.imageInternalFormat = EiifRGBA8UI;
4324 }
4325 else if (qualifierType == "r32ui")
4326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.imageInternalFormat = EiifR32UI;
4329 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004330 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptPoints;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptLines;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLinesAdjacency;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptTriangles;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptTrianglesAdjacency;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptLineStrip;
4365 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004366 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptTriangleStrip;
4371 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004372
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004373 else
4374 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004375 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004376 }
4377
Jamie Madilla5efff92013-06-06 11:56:47 -04004378 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379}
4380
Olli Etuahofbb1c792018-01-19 16:26:59 +02004381void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004382 const TSourceLoc &qualifierTypeLine,
4383 int intValue,
4384 const TSourceLoc &intValueLine,
4385 const std::string &intValueString,
4386 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004387 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004388{
Olli Etuaho856c4972016-08-08 11:38:39 +03004389 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004390 if (intValue < 1)
4391 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004392 std::stringstream reasonStream;
4393 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4394 std::string reason = reasonStream.str();
4395 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004396 }
4397 (*localSize)[index] = intValue;
4398}
4399
Olli Etuaho09b04a22016-12-15 13:30:26 +00004400void TParseContext::parseNumViews(int intValue,
4401 const TSourceLoc &intValueLine,
4402 const std::string &intValueString,
4403 int *numViews)
4404{
4405 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4406 // specification.
4407 if (intValue < 1)
4408 {
4409 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4410 }
4411 *numViews = intValue;
4412}
4413
Shaob5cc1192017-07-06 10:47:20 +08004414void TParseContext::parseInvocations(int intValue,
4415 const TSourceLoc &intValueLine,
4416 const std::string &intValueString,
4417 int *numInvocations)
4418{
4419 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4420 // it doesn't make sense to accept invocations <= 0.
4421 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4422 {
4423 error(intValueLine,
4424 "out of range: invocations must be in the range of [1, "
4425 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4426 intValueString.c_str());
4427 }
4428 else
4429 {
4430 *numInvocations = intValue;
4431 }
4432}
4433
4434void TParseContext::parseMaxVertices(int intValue,
4435 const TSourceLoc &intValueLine,
4436 const std::string &intValueString,
4437 int *maxVertices)
4438{
4439 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4440 // it doesn't make sense to accept max_vertices < 0.
4441 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4442 {
4443 error(
4444 intValueLine,
4445 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4446 intValueString.c_str());
4447 }
4448 else
4449 {
4450 *maxVertices = intValue;
4451 }
4452}
4453
Olli Etuahoa78092c2018-09-26 14:16:13 +03004454void TParseContext::parseIndexLayoutQualifier(int intValue,
4455 const TSourceLoc &intValueLine,
4456 const std::string &intValueString,
4457 int *index)
4458{
4459 // EXT_blend_func_extended specifies that most validation should happen at link time, but since
4460 // we're validating output variable locations at compile time, it makes sense to validate that
4461 // index is 0 or 1 also at compile time. Also since we use "-1" as a placeholder for unspecified
4462 // index, we can't accept it here.
4463 if (intValue < 0 || intValue > 1)
4464 {
4465 error(intValueLine, "out of range: index layout qualifier can only be 0 or 1",
4466 intValueString.c_str());
4467 }
4468 else
4469 {
4470 *index = intValue;
4471 }
4472}
4473
Olli Etuahofbb1c792018-01-19 16:26:59 +02004474TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004475 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004476 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304477 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478{
Jamie Madill2f294c92017-11-20 14:47:26 -05004479 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004480
Martin Radev802abe02016-08-04 17:48:32 +03004481 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004482
Martin Radev802abe02016-08-04 17:48:32 +03004483 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004484 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004485 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004486 if (intValue < 0)
4487 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004488 error(intValueLine, "out of range: location must be non-negative",
4489 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004490 }
4491 else
4492 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004493 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004494 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004495 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004496 }
Olli Etuaho43364892017-02-13 16:00:12 +00004497 else if (qualifierType == "binding")
4498 {
4499 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4500 if (intValue < 0)
4501 {
4502 error(intValueLine, "out of range: binding must be non-negative",
4503 intValueString.c_str());
4504 }
4505 else
4506 {
4507 qualifier.binding = intValue;
4508 }
4509 }
jchen104cdac9e2017-05-08 11:01:20 +08004510 else if (qualifierType == "offset")
4511 {
4512 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4513 if (intValue < 0)
4514 {
4515 error(intValueLine, "out of range: offset must be non-negative",
4516 intValueString.c_str());
4517 }
4518 else
4519 {
4520 qualifier.offset = intValue;
4521 }
4522 }
Martin Radev802abe02016-08-04 17:48:32 +03004523 else if (qualifierType == "local_size_x")
4524 {
4525 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4526 &qualifier.localSize);
4527 }
4528 else if (qualifierType == "local_size_y")
4529 {
4530 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4531 &qualifier.localSize);
4532 }
4533 else if (qualifierType == "local_size_z")
4534 {
4535 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4536 &qualifier.localSize);
4537 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004538 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004539 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004540 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4541 {
4542 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4543 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004544 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004545 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4546 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004547 {
4548 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4549 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004550 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4551 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004552 {
4553 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4554 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03004555 else if (qualifierType == "index" && mShaderType == GL_FRAGMENT_SHADER &&
4556 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_blend_func_extended))
4557 {
4558 parseIndexLayoutQualifier(intValue, intValueLine, intValueString, &qualifier.index);
4559 }
Martin Radev802abe02016-08-04 17:48:32 +03004560 else
4561 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004562 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004563 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004564
Jamie Madilla5efff92013-06-06 11:56:47 -04004565 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004566}
4567
Olli Etuaho613b9592016-09-05 12:05:53 +03004568TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4569{
4570 return new TTypeQualifierBuilder(
4571 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4572 mShaderVersion);
4573}
4574
Olli Etuahocce89652017-06-19 16:04:09 +03004575TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4576 const TSourceLoc &loc)
4577{
4578 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4579 return new TStorageQualifierWrapper(qualifier, loc);
4580}
4581
4582TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4583{
4584 if (getShaderType() == GL_VERTEX_SHADER)
4585 {
4586 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4587 }
4588 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4589}
4590
4591TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4592{
4593 if (declaringFunction())
4594 {
4595 return new TStorageQualifierWrapper(EvqIn, loc);
4596 }
Shaob5cc1192017-07-06 10:47:20 +08004597
4598 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004599 {
Shaob5cc1192017-07-06 10:47:20 +08004600 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004601 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004602 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004603 {
4604 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4605 }
4606 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004607 }
Shaob5cc1192017-07-06 10:47:20 +08004608 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004609 {
Shaob5cc1192017-07-06 10:47:20 +08004610 if (mShaderVersion < 300)
4611 {
4612 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4613 }
4614 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004615 }
Shaob5cc1192017-07-06 10:47:20 +08004616 case GL_COMPUTE_SHADER:
4617 {
4618 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4619 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004620 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004621 {
4622 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4623 }
4624 default:
4625 {
4626 UNREACHABLE();
4627 return new TStorageQualifierWrapper(EvqLast, loc);
4628 }
Olli Etuahocce89652017-06-19 16:04:09 +03004629 }
Olli Etuahocce89652017-06-19 16:04:09 +03004630}
4631
4632TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4633{
4634 if (declaringFunction())
4635 {
4636 return new TStorageQualifierWrapper(EvqOut, loc);
4637 }
Shaob5cc1192017-07-06 10:47:20 +08004638 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004639 {
Shaob5cc1192017-07-06 10:47:20 +08004640 case GL_VERTEX_SHADER:
4641 {
4642 if (mShaderVersion < 300)
4643 {
4644 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4645 }
4646 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4647 }
4648 case GL_FRAGMENT_SHADER:
4649 {
4650 if (mShaderVersion < 300)
4651 {
4652 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4653 }
4654 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4655 }
4656 case GL_COMPUTE_SHADER:
4657 {
4658 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4659 return new TStorageQualifierWrapper(EvqLast, loc);
4660 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004661 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004662 {
4663 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4664 }
4665 default:
4666 {
4667 UNREACHABLE();
4668 return new TStorageQualifierWrapper(EvqLast, loc);
4669 }
Olli Etuahocce89652017-06-19 16:04:09 +03004670 }
Olli Etuahocce89652017-06-19 16:04:09 +03004671}
4672
4673TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4674{
4675 if (!declaringFunction())
4676 {
4677 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4678 }
4679 return new TStorageQualifierWrapper(EvqInOut, loc);
4680}
4681
Jamie Madillb98c3a82015-07-23 14:26:04 -04004682TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004683 TLayoutQualifier rightQualifier,
4684 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004685{
Martin Radevc28888b2016-07-22 15:27:42 +03004686 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004687 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004688}
4689
Olli Etuahofbb1c792018-01-19 16:26:59 +02004690TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4691 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004692{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004693 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004694 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004695}
4696
Olli Etuahofbb1c792018-01-19 16:26:59 +02004697TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004698 const TSourceLoc &loc,
4699 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004700{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004701 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004702 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004703}
4704
Olli Etuaho722bfb52017-10-26 17:00:11 +03004705void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4706 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004707 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004708 const TSourceLoc &location)
4709{
4710 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4711 {
4712 if ((*fieldIter)->name() == name)
4713 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004714 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004715 }
4716 }
4717}
4718
4719TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4720{
4721 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4722 ++fieldIter)
4723 {
4724 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4725 location);
4726 }
4727 return fields;
4728}
4729
Olli Etuaho4de340a2016-12-16 09:32:03 +00004730TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4731 const TFieldList *newlyAddedFields,
4732 const TSourceLoc &location)
4733{
4734 for (TField *field : *newlyAddedFields)
4735 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004736 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4737 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004738 processedFields->push_back(field);
4739 }
4740 return processedFields;
4741}
4742
Martin Radev70866b82016-07-22 15:27:42 +03004743TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4744 const TTypeQualifierBuilder &typeQualifierBuilder,
4745 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004746 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004747{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004748 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004749
Martin Radev70866b82016-07-22 15:27:42 +03004750 typeSpecifier->qualifier = typeQualifier.qualifier;
4751 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004752 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004753 typeSpecifier->invariant = typeQualifier.invariant;
4754 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304755 {
Martin Radev70866b82016-07-22 15:27:42 +03004756 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004757 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004758 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004759}
4760
Jamie Madillb98c3a82015-07-23 14:26:04 -04004761TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004762 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004763{
Martin Radev4a9cd802016-09-01 16:51:51 +03004764 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4765 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004766
Olli Etuahofbb1c792018-01-19 16:26:59 +02004767 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004768 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004769
Martin Radev4a9cd802016-09-01 16:51:51 +03004770 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004771
Olli Etuahod5f44c92017-11-29 17:15:40 +02004772 TFieldList *fieldList = new TFieldList();
4773
4774 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304775 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004776 TType *type = new TType(typeSpecifier);
4777 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304778 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004779 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004780 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004781 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004782 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004783
Jamie Madillf5557ac2018-06-15 09:46:58 -04004784 TField *field =
4785 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004786 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4787 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004788 }
4789
Olli Etuahod5f44c92017-11-29 17:15:40 +02004790 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004791}
4792
Martin Radev4a9cd802016-09-01 16:51:51 +03004793TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4794 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004795 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004796 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004797{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004798 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004799 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004800 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004801 structSymbolType = SymbolType::Empty;
4802 }
4803 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004804
Jamie Madill9b820842015-02-12 10:40:10 -05004805 // Store a bool in the struct if we're at global scope, to allow us to
4806 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004807 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004808
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004809 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004810 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004811 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004812 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304813 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004814 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004815 }
4816 }
4817
4818 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004819 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004820 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004821 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004822 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004823 switch (qualifier)
4824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004825 case EvqGlobal:
4826 case EvqTemporary:
4827 break;
4828 default:
4829 error(field.line(), "invalid qualifier on struct member",
4830 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004832 }
Martin Radev70866b82016-07-22 15:27:42 +03004833 if (field.type()->isInvariant())
4834 {
4835 error(field.line(), "invalid qualifier on struct member", "invariant");
4836 }
jchen104cdac9e2017-05-08 11:01:20 +08004837 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4838 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004839 {
4840 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4841 }
4842
Olli Etuahoebee5b32017-11-23 12:56:32 +02004843 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004844 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004845
Olli Etuaho43364892017-02-13 16:00:12 +00004846 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4847
Olli Etuahoa78092c2018-09-26 14:16:13 +03004848 checkIndexIsNotSpecified(field.line(), field.type()->getLayoutQualifier().index);
4849
Olli Etuaho43364892017-02-13 16:00:12 +00004850 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004851
4852 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004853 }
4854
Martin Radev4a9cd802016-09-01 16:51:51 +03004855 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004856 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004857 exitStructDeclaration();
4858
Martin Radev4a9cd802016-09-01 16:51:51 +03004859 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004860}
4861
Jamie Madillb98c3a82015-07-23 14:26:04 -04004862TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004863 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004864 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004865{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004866 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004867 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 init->isVector())
4869 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004870 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4871 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004872 return nullptr;
4873 }
4874
Olli Etuaho923ecef2017-10-11 12:01:38 +03004875 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004876 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004877 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004878 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004879 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004880 }
4881
Olli Etuaho94bbed12018-03-20 14:44:53 +02004882 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004883 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4884 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004885 return node;
4886}
4887
4888TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4889{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 if (mSwitchNestingLevel == 0)
4891 {
4892 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004893 return nullptr;
4894 }
4895 if (condition == nullptr)
4896 {
4897 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004898 return nullptr;
4899 }
4900 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004901 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004902 {
4903 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004904 }
4905 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004906 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4907 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4908 // fold in case labels.
4909 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004910 {
4911 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004912 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004913 TIntermCase *node = new TIntermCase(condition);
4914 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004915 return node;
4916}
4917
4918TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4919{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004920 if (mSwitchNestingLevel == 0)
4921 {
4922 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004923 return nullptr;
4924 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004925 TIntermCase *node = new TIntermCase(nullptr);
4926 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004927 return node;
4928}
4929
Jamie Madillb98c3a82015-07-23 14:26:04 -04004930TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4931 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004932 const TSourceLoc &loc,
4933 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004934{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004935 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004936
4937 switch (op)
4938 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004939 case EOpLogicalNot:
4940 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4941 child->isVector())
4942 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004943 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004944 return nullptr;
4945 }
4946 break;
4947 case EOpBitwiseNot:
4948 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4949 child->isMatrix() || child->isArray())
4950 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004951 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004952 return nullptr;
4953 }
4954 break;
4955 case EOpPostIncrement:
4956 case EOpPreIncrement:
4957 case EOpPostDecrement:
4958 case EOpPreDecrement:
4959 case EOpNegative:
4960 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004961 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4962 child->getBasicType() == EbtBool || child->isArray() ||
4963 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004965 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004966 return nullptr;
4967 }
Nico Weber41b072b2018-02-09 10:01:32 -05004968 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004969 // Operators for built-ins are already type checked against their prototype.
4970 default:
4971 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004972 }
4973
Jiajia Qinbc585152017-06-23 15:42:17 +08004974 if (child->getMemoryQualifier().writeonly)
4975 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004976 unaryOpError(loc, GetOperatorString(op), child->getType());
Jiajia Qinbc585152017-06-23 15:42:17 +08004977 return nullptr;
4978 }
4979
Olli Etuaho94bbed12018-03-20 14:44:53 +02004980 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004981 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03004982 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004983
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004984 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004985}
4986
Olli Etuaho09b22472015-02-11 11:47:26 +02004987TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4988{
Olli Etuahocce89652017-06-19 16:04:09 +03004989 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004990 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004991 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004992 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004993 return child;
4994 }
4995 return node;
4996}
4997
Jamie Madillb98c3a82015-07-23 14:26:04 -04004998TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4999 TIntermTyped *child,
5000 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005001{
Olli Etuaho856c4972016-08-08 11:38:39 +03005002 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02005003 return addUnaryMath(op, child, loc);
5004}
5005
Olli Etuaho765924f2018-01-04 12:48:36 +02005006TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
5007{
5008 // If we can, we should return the folded version of the expression for subsequent parsing. This
5009 // enables folding the containing expression during parsing as well, instead of the separate
5010 // FoldExpressions() step where folding nested expressions requires multiple full AST
5011 // traversals.
5012
5013 // Even if folding fails the fold() functions return some node representing the expression,
5014 // typically the original node. So "folded" can be assumed to be non-null.
5015 TIntermTyped *folded = expression->fold(mDiagnostics);
5016 ASSERT(folded != nullptr);
5017 if (folded->getQualifier() == expression->getQualifier())
5018 {
5019 // We need this expression to have the correct qualifier when validating the consuming
5020 // expression. So we can only return the folded node from here in case it has the same
5021 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
5022 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
5023 // 1. (true ? 1.0 : non_constant)
5024 // 2. (non_constant, 1.0)
5025 return folded;
5026 }
5027 return expression;
5028}
5029
Jamie Madillb98c3a82015-07-23 14:26:04 -04005030bool TParseContext::binaryOpCommonCheck(TOperator op,
5031 TIntermTyped *left,
5032 TIntermTyped *right,
5033 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005034{
jchen10b4cf5652017-05-05 18:51:17 +08005035 // Check opaque types are not allowed to be operands in expressions other than array indexing
5036 // and structure member selection.
5037 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5038 {
5039 switch (op)
5040 {
5041 case EOpIndexDirect:
5042 case EOpIndexIndirect:
5043 break;
jchen10b4cf5652017-05-05 18:51:17 +08005044
5045 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005046 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005047 error(loc, "Invalid operation for variables with an opaque type",
5048 GetOperatorString(op));
5049 return false;
5050 }
5051 }
jchen10cc2a10e2017-05-03 14:05:12 +08005052
Jiajia Qinbc585152017-06-23 15:42:17 +08005053 if (right->getMemoryQualifier().writeonly)
5054 {
5055 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5056 return false;
5057 }
5058
5059 if (left->getMemoryQualifier().writeonly)
5060 {
5061 switch (op)
5062 {
5063 case EOpAssign:
5064 case EOpInitialize:
5065 case EOpIndexDirect:
5066 case EOpIndexIndirect:
5067 case EOpIndexDirectStruct:
5068 case EOpIndexDirectInterfaceBlock:
5069 break;
5070 default:
5071 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5072 return false;
5073 }
5074 }
5075
Olli Etuaho244be012016-08-18 15:26:02 +03005076 if (left->getType().getStruct() || right->getType().getStruct())
5077 {
5078 switch (op)
5079 {
5080 case EOpIndexDirectStruct:
5081 ASSERT(left->getType().getStruct());
5082 break;
5083 case EOpEqual:
5084 case EOpNotEqual:
5085 case EOpAssign:
5086 case EOpInitialize:
5087 if (left->getType() != right->getType())
5088 {
5089 return false;
5090 }
5091 break;
5092 default:
5093 error(loc, "Invalid operation for structs", GetOperatorString(op));
5094 return false;
5095 }
5096 }
5097
Olli Etuaho94050052017-05-08 14:17:44 +03005098 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5099 {
5100 switch (op)
5101 {
5102 case EOpIndexDirectInterfaceBlock:
5103 ASSERT(left->getType().getInterfaceBlock());
5104 break;
5105 default:
5106 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5107 return false;
5108 }
5109 }
5110
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005111 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005112 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005113 error(loc, "array / non-array mismatch", GetOperatorString(op));
5114 return false;
5115 }
5116
5117 if (left->isArray())
5118 {
5119 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005120 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005121 {
5122 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5123 return false;
5124 }
5125
Olli Etuahoe79904c2015-03-18 16:56:42 +02005126 switch (op)
5127 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005128 case EOpEqual:
5129 case EOpNotEqual:
5130 case EOpAssign:
5131 case EOpInitialize:
5132 break;
5133 default:
5134 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5135 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005136 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005137 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005138 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005139 {
5140 error(loc, "array size mismatch", GetOperatorString(op));
5141 return false;
5142 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005143 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005144
5145 // Check ops which require integer / ivec parameters
5146 bool isBitShift = false;
5147 switch (op)
5148 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005149 case EOpBitShiftLeft:
5150 case EOpBitShiftRight:
5151 case EOpBitShiftLeftAssign:
5152 case EOpBitShiftRightAssign:
5153 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5154 // check that the basic type is an integer type.
5155 isBitShift = true;
5156 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5157 {
5158 return false;
5159 }
5160 break;
5161 case EOpBitwiseAnd:
5162 case EOpBitwiseXor:
5163 case EOpBitwiseOr:
5164 case EOpBitwiseAndAssign:
5165 case EOpBitwiseXorAssign:
5166 case EOpBitwiseOrAssign:
5167 // It is enough to check the type of only one operand, since later it
5168 // is checked that the operand types match.
5169 if (!IsInteger(left->getBasicType()))
5170 {
5171 return false;
5172 }
5173 break;
5174 default:
5175 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005176 }
5177
5178 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5179 // So the basic type should usually match.
5180 if (!isBitShift && left->getBasicType() != right->getBasicType())
5181 {
5182 return false;
5183 }
5184
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005185 // Check that:
5186 // 1. Type sizes match exactly on ops that require that.
5187 // 2. Restrictions for structs that contain arrays or samplers are respected.
5188 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005189 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005190 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 case EOpAssign:
5192 case EOpInitialize:
5193 case EOpEqual:
5194 case EOpNotEqual:
5195 // ESSL 1.00 sections 5.7, 5.8, 5.9
5196 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5197 {
5198 error(loc, "undefined operation for structs containing arrays",
5199 GetOperatorString(op));
5200 return false;
5201 }
5202 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5203 // we interpret the spec so that this extends to structs containing samplers,
5204 // similarly to ESSL 1.00 spec.
5205 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5206 left->getType().isStructureContainingSamplers())
5207 {
5208 error(loc, "undefined operation for structs containing samplers",
5209 GetOperatorString(op));
5210 return false;
5211 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005212
Olli Etuahoe1805592017-01-02 16:41:20 +00005213 if ((left->getNominalSize() != right->getNominalSize()) ||
5214 (left->getSecondarySize() != right->getSecondarySize()))
5215 {
5216 error(loc, "dimension mismatch", GetOperatorString(op));
5217 return false;
5218 }
5219 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005220 case EOpLessThan:
5221 case EOpGreaterThan:
5222 case EOpLessThanEqual:
5223 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005224 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005225 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005226 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 return false;
5228 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005229 break;
5230 case EOpAdd:
5231 case EOpSub:
5232 case EOpDiv:
5233 case EOpIMod:
5234 case EOpBitShiftLeft:
5235 case EOpBitShiftRight:
5236 case EOpBitwiseAnd:
5237 case EOpBitwiseXor:
5238 case EOpBitwiseOr:
5239 case EOpAddAssign:
5240 case EOpSubAssign:
5241 case EOpDivAssign:
5242 case EOpIModAssign:
5243 case EOpBitShiftLeftAssign:
5244 case EOpBitShiftRightAssign:
5245 case EOpBitwiseAndAssign:
5246 case EOpBitwiseXorAssign:
5247 case EOpBitwiseOrAssign:
5248 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5249 {
5250 return false;
5251 }
5252
5253 // Are the sizes compatible?
5254 if (left->getNominalSize() != right->getNominalSize() ||
5255 left->getSecondarySize() != right->getSecondarySize())
5256 {
5257 // If the nominal sizes of operands do not match:
5258 // One of them must be a scalar.
5259 if (!left->isScalar() && !right->isScalar())
5260 return false;
5261
5262 // In the case of compound assignment other than multiply-assign,
5263 // the right side needs to be a scalar. Otherwise a vector/matrix
5264 // would be assigned to a scalar. A scalar can't be shifted by a
5265 // vector either.
5266 if (!right->isScalar() &&
5267 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5268 return false;
5269 }
5270 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005271 default:
5272 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005273 }
5274
Olli Etuahod6b14282015-03-17 14:31:35 +02005275 return true;
5276}
5277
Olli Etuaho1dded802016-08-18 18:13:13 +03005278bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5279 const TType &left,
5280 const TType &right)
5281{
5282 switch (op)
5283 {
5284 case EOpMul:
5285 case EOpMulAssign:
5286 return left.getNominalSize() == right.getNominalSize() &&
5287 left.getSecondarySize() == right.getSecondarySize();
5288 case EOpVectorTimesScalar:
5289 return true;
5290 case EOpVectorTimesScalarAssign:
5291 ASSERT(!left.isMatrix() && !right.isMatrix());
5292 return left.isVector() && !right.isVector();
5293 case EOpVectorTimesMatrix:
5294 return left.getNominalSize() == right.getRows();
5295 case EOpVectorTimesMatrixAssign:
5296 ASSERT(!left.isMatrix() && right.isMatrix());
5297 return left.isVector() && left.getNominalSize() == right.getRows() &&
5298 left.getNominalSize() == right.getCols();
5299 case EOpMatrixTimesVector:
5300 return left.getCols() == right.getNominalSize();
5301 case EOpMatrixTimesScalar:
5302 return true;
5303 case EOpMatrixTimesScalarAssign:
5304 ASSERT(left.isMatrix() && !right.isMatrix());
5305 return !right.isVector();
5306 case EOpMatrixTimesMatrix:
5307 return left.getCols() == right.getRows();
5308 case EOpMatrixTimesMatrixAssign:
5309 ASSERT(left.isMatrix() && right.isMatrix());
5310 // We need to check two things:
5311 // 1. The matrix multiplication step is valid.
5312 // 2. The result will have the same number of columns as the lvalue.
5313 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5314
5315 default:
5316 UNREACHABLE();
5317 return false;
5318 }
5319}
5320
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5322 TIntermTyped *left,
5323 TIntermTyped *right,
5324 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005325{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005326 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005327 return nullptr;
5328
Olli Etuahofc1806e2015-03-17 13:03:11 +02005329 switch (op)
5330 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005331 case EOpEqual:
5332 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005333 case EOpLessThan:
5334 case EOpGreaterThan:
5335 case EOpLessThanEqual:
5336 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005337 break;
5338 case EOpLogicalOr:
5339 case EOpLogicalXor:
5340 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005341 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5342 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005343 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005344 {
5345 return nullptr;
5346 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005347 // Basic types matching should have been already checked.
5348 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005349 break;
5350 case EOpAdd:
5351 case EOpSub:
5352 case EOpDiv:
5353 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005354 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5355 !right->getType().getStruct());
5356 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005357 {
5358 return nullptr;
5359 }
5360 break;
5361 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005362 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5363 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005364 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005365 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005366 {
5367 return nullptr;
5368 }
5369 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005370 default:
5371 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005372 }
5373
Olli Etuaho1dded802016-08-18 18:13:13 +03005374 if (op == EOpMul)
5375 {
5376 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5377 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5378 {
5379 return nullptr;
5380 }
5381 }
5382
Olli Etuaho3fdec912016-08-18 15:08:06 +03005383 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005384 ASSERT(op != EOpAssign);
5385 markStaticReadIfSymbol(left);
5386 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005387 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005388 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005389}
5390
Jamie Madillb98c3a82015-07-23 14:26:04 -04005391TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5392 TIntermTyped *left,
5393 TIntermTyped *right,
5394 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005395{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005396 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005397 if (node == 0)
5398 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005399 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho09b22472015-02-11 11:47:26 +02005400 return left;
5401 }
5402 return node;
5403}
5404
Jamie Madillb98c3a82015-07-23 14:26:04 -04005405TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5406 TIntermTyped *left,
5407 TIntermTyped *right,
5408 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005409{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005410 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005411 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005412 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005413 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005414 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005415 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005416 }
5417 return node;
5418}
5419
Jamie Madillb98c3a82015-07-23 14:26:04 -04005420TIntermTyped *TParseContext::addAssign(TOperator op,
5421 TIntermTyped *left,
5422 TIntermTyped *right,
5423 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005424{
Olli Etuahocce89652017-06-19 16:04:09 +03005425 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005426 TIntermBinary *node = nullptr;
5427 if (binaryOpCommonCheck(op, left, right, loc))
5428 {
5429 if (op == EOpMulAssign)
5430 {
5431 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5432 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5433 {
5434 node = new TIntermBinary(op, left, right);
5435 }
5436 }
5437 else
5438 {
5439 node = new TIntermBinary(op, left, right);
5440 }
5441 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005442 if (node == nullptr)
5443 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005444 assignError(loc, "assign", left->getType(), right->getType());
Olli Etuahod6b14282015-03-17 14:31:35 +02005445 return left;
5446 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005447 if (op != EOpAssign)
5448 {
5449 markStaticReadIfSymbol(left);
5450 }
5451 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005452 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005453 return node;
5454}
5455
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005456TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5457 TIntermTyped *right,
5458 const TSourceLoc &loc)
5459{
Corentin Wallez0d959252016-07-12 17:26:32 -04005460 // WebGL2 section 5.26, the following results in an error:
5461 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005462 if (mShaderSpec == SH_WEBGL2_SPEC &&
5463 (left->isArray() || left->getBasicType() == EbtVoid ||
5464 left->getType().isStructureContainingArrays() || right->isArray() ||
5465 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005466 {
5467 error(loc,
5468 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5469 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005470 }
5471
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005472 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005473 markStaticReadIfSymbol(left);
5474 markStaticReadIfSymbol(right);
5475 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005476
5477 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005478}
5479
Olli Etuaho49300862015-02-20 14:54:49 +02005480TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5481{
5482 switch (op)
5483 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005484 case EOpContinue:
5485 if (mLoopNestingLevel <= 0)
5486 {
5487 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005488 }
5489 break;
5490 case EOpBreak:
5491 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5492 {
5493 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005494 }
5495 break;
5496 case EOpReturn:
5497 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5498 {
5499 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005500 }
5501 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005502 case EOpKill:
5503 if (mShaderType != GL_FRAGMENT_SHADER)
5504 {
5505 error(loc, "discard supported in fragment shaders only", "discard");
5506 }
5507 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005508 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005509 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005510 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005511 }
Olli Etuahocce89652017-06-19 16:04:09 +03005512 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005513}
5514
Jamie Madillb98c3a82015-07-23 14:26:04 -04005515TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005516 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005517 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005518{
Olli Etuahocce89652017-06-19 16:04:09 +03005519 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005520 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005521 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005522 ASSERT(op == EOpReturn);
5523 mFunctionReturnsValue = true;
5524 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5525 {
5526 error(loc, "void function cannot return a value", "return");
5527 }
5528 else if (*mCurrentFunctionType != expression->getType())
5529 {
5530 error(loc, "function return is not matching type:", "return");
5531 }
Olli Etuaho49300862015-02-20 14:54:49 +02005532 }
Olli Etuahocce89652017-06-19 16:04:09 +03005533 TIntermBranch *node = new TIntermBranch(op, expression);
5534 node->setLine(loc);
5535 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005536}
5537
Olli Etuaho94bbed12018-03-20 14:44:53 +02005538void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5539{
5540 if (statement != nullptr)
5541 {
5542 markStaticReadIfSymbol(statement);
5543 block->appendStatement(statement);
5544 }
5545}
5546
Martin Radev84aa2dc2017-09-11 15:51:02 +03005547void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5548{
5549 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005550 const TFunction *func = functionCall->getFunction();
5551 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005552 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005553 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005554 TIntermNode *componentNode = nullptr;
5555 TIntermSequence *arguments = functionCall->getSequence();
5556 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5557 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5558 ASSERT(sampler != nullptr);
5559 switch (sampler->getBasicType())
5560 {
5561 case EbtSampler2D:
5562 case EbtISampler2D:
5563 case EbtUSampler2D:
5564 case EbtSampler2DArray:
5565 case EbtISampler2DArray:
5566 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005567 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005568 (isTextureGatherOffset && arguments->size() == 4u))
5569 {
5570 componentNode = arguments->back();
5571 }
5572 break;
5573 case EbtSamplerCube:
5574 case EbtISamplerCube:
5575 case EbtUSamplerCube:
5576 ASSERT(!isTextureGatherOffset);
5577 if (arguments->size() == 3u)
5578 {
5579 componentNode = arguments->back();
5580 }
5581 break;
5582 case EbtSampler2DShadow:
5583 case EbtSampler2DArrayShadow:
5584 case EbtSamplerCubeShadow:
5585 break;
5586 default:
5587 UNREACHABLE();
5588 break;
5589 }
5590 if (componentNode)
5591 {
5592 const TIntermConstantUnion *componentConstantUnion =
5593 componentNode->getAsConstantUnion();
5594 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5595 {
5596 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005597 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005598 }
5599 else
5600 {
5601 int component = componentConstantUnion->getIConst(0);
5602 if (component < 0 || component > 3)
5603 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005604 error(functionCall->getLine(), "Component must be in the range [0;3]",
5605 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005606 }
5607 }
5608 }
5609 }
5610}
5611
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5613{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005614 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005615 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005616 TIntermNode *offset = nullptr;
5617 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005618 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005619 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005620 {
5621 offset = arguments->back();
5622 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005623 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005624 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005625 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005626 ASSERT(arguments->size() >= 3);
5627 offset = (*arguments)[2];
5628 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005629 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005630 {
5631 ASSERT(arguments->size() >= 3u);
5632 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5633 ASSERT(sampler != nullptr);
5634 switch (sampler->getBasicType())
5635 {
5636 case EbtSampler2D:
5637 case EbtISampler2D:
5638 case EbtUSampler2D:
5639 case EbtSampler2DArray:
5640 case EbtISampler2DArray:
5641 case EbtUSampler2DArray:
5642 offset = (*arguments)[2];
5643 break;
5644 case EbtSampler2DShadow:
5645 case EbtSampler2DArrayShadow:
5646 offset = (*arguments)[3];
5647 break;
5648 default:
5649 UNREACHABLE();
5650 break;
5651 }
5652 useTextureGatherOffsetConstraints = true;
5653 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005654 if (offset != nullptr)
5655 {
5656 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5657 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5658 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005659 error(functionCall->getLine(), "Texture offset must be a constant expression",
5660 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005661 }
5662 else
5663 {
5664 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5665 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005666 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005667 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5668 : mMinProgramTexelOffset;
5669 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5670 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005671 for (size_t i = 0u; i < size; ++i)
5672 {
5673 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005674 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005675 {
5676 std::stringstream tokenStream;
5677 tokenStream << offsetValue;
5678 std::string token = tokenStream.str();
5679 error(offset->getLine(), "Texture offset value out of valid range",
5680 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005681 }
5682 }
5683 }
5684 }
5685}
5686
Jiajia Qina3106c52017-11-03 09:39:39 +08005687void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5688{
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005689 const TFunction *func = functionCall->getFunction();
5690 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005691 {
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005692 ASSERT(IsAtomicFunction(functionCall->getOp()));
Jiajia Qina3106c52017-11-03 09:39:39 +08005693 TIntermSequence *arguments = functionCall->getSequence();
5694 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5695
5696 if (IsBufferOrSharedVariable(memNode))
5697 {
5698 return;
5699 }
5700
5701 while (memNode->getAsBinaryNode())
5702 {
5703 memNode = memNode->getAsBinaryNode()->getLeft();
5704 if (IsBufferOrSharedVariable(memNode))
5705 {
5706 return;
5707 }
5708 }
5709
5710 error(memNode->getLine(),
5711 "The value passed to the mem argument of an atomic memory function does not "
5712 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005713 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005714 }
5715}
5716
Martin Radev2cc85b32016-08-05 16:22:53 +03005717// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5718void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5719{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005720 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005721
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005722 const TFunction *func = functionCall->getFunction();
5723
5724 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005725 {
5726 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005727 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005728
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005730
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005731 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005732 {
5733 if (memoryQualifier.readonly)
5734 {
5735 error(imageNode->getLine(),
5736 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005738 }
5739 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005740 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005741 {
5742 if (memoryQualifier.writeonly)
5743 {
5744 error(imageNode->getLine(),
5745 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005746 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005747 }
5748 }
5749 }
5750}
5751
5752// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5753void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5754 const TFunction *functionDefinition,
5755 const TIntermAggregate *functionCall)
5756{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005757 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005758
5759 const TIntermSequence &arguments = *functionCall->getSequence();
5760
5761 ASSERT(functionDefinition->getParamCount() == arguments.size());
5762
5763 for (size_t i = 0; i < arguments.size(); ++i)
5764 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005765 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5766 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005767 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005768 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5769
5770 if (IsImage(functionArgumentType.getBasicType()))
5771 {
5772 const TMemoryQualifier &functionArgumentMemoryQualifier =
5773 functionArgumentType.getMemoryQualifier();
5774 const TMemoryQualifier &functionParameterMemoryQualifier =
5775 functionParameterType.getMemoryQualifier();
5776 if (functionArgumentMemoryQualifier.readonly &&
5777 !functionParameterMemoryQualifier.readonly)
5778 {
5779 error(functionCall->getLine(),
5780 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005781 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005782 }
5783
5784 if (functionArgumentMemoryQualifier.writeonly &&
5785 !functionParameterMemoryQualifier.writeonly)
5786 {
5787 error(functionCall->getLine(),
5788 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005789 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005790 }
Martin Radev049edfa2016-11-11 14:35:37 +02005791
5792 if (functionArgumentMemoryQualifier.coherent &&
5793 !functionParameterMemoryQualifier.coherent)
5794 {
5795 error(functionCall->getLine(),
5796 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005797 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005798 }
5799
5800 if (functionArgumentMemoryQualifier.volatileQualifier &&
5801 !functionParameterMemoryQualifier.volatileQualifier)
5802 {
5803 error(functionCall->getLine(),
5804 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005805 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005806 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005807 }
5808 }
5809}
5810
Olli Etuaho95ed1942018-02-01 14:01:19 +02005811TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005812{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005813 if (fnCall->thisNode() != nullptr)
5814 {
5815 return addMethod(fnCall, loc);
5816 }
5817 if (fnCall->isConstructor())
5818 {
5819 return addConstructor(fnCall, loc);
5820 }
5821 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005822}
5823
Olli Etuaho95ed1942018-02-01 14:01:19 +02005824TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005825{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005826 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5828 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5829 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005830 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005831 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005833 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005834 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005835 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 {
5837 error(loc, "method takes no parameters", "length");
5838 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005839 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 {
5841 error(loc, "length can only be called on arrays", "length");
5842 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005843 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005844 mGeometryShaderInputPrimitiveType == EptUndefined)
5845 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005846 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005847 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5848 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005849 else
5850 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005851 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005852 node->setLine(loc);
5853 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005855 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856}
5857
Olli Etuaho95ed1942018-02-01 14:01:19 +02005858TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005859 const TSourceLoc &loc)
5860{
Olli Etuaho697bf652018-02-16 11:50:54 +02005861 // First check whether the function has been hidden by a variable name or struct typename by
5862 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5863 // with a matching argument list.
5864 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005865 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005866 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005867 }
5868 else
5869 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005870 // There are no inner functions, so it's enough to look for user-defined functions in the
5871 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005872 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005873 if (symbol != nullptr)
5874 {
5875 // A user-defined function - could be an overloaded built-in as well.
5876 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5877 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5878 TIntermAggregate *callNode =
5879 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5880 callNode->setLine(loc);
5881 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5882 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5883 return callNode;
5884 }
5885
5886 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005887 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005888 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005889 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005890 }
5891 else
5892 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005893 // A built-in function.
5894 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005895 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005896
Olli Etuaho37b697e2018-01-29 12:19:27 +02005897 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005898 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005899 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005901 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005902 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005903 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905 if (fnCandidate->getParamCount() == 1)
5906 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005907 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005908 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005909 TIntermTyped *callNode =
5910 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005911 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005912 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005913 }
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005914
Olli Etuahoe80825e2018-02-16 10:24:53 +02005915 TIntermAggregate *callNode =
5916 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005917 callNode->setLine(loc);
5918
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005919 checkAtomicMemoryBuiltinFunctions(callNode);
5920
Olli Etuahoe80825e2018-02-16 10:24:53 +02005921 // Some built-in functions have out parameters too.
5922 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5923
5924 // See if we can constant fold a built-in. Note that this may be possible
5925 // even if it is not const-qualified.
5926 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005927 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005928
5929 // This is a built-in function with no op associated with it.
5930 TIntermAggregate *callNode =
5931 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5932 callNode->setLine(loc);
5933 checkTextureOffsetConst(callNode);
5934 checkTextureGather(callNode);
5935 checkImageMemoryAccessForBuiltinFunctions(callNode);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005936 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5937 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005938 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005939 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005940
5941 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005942 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005943}
5944
Jamie Madillb98c3a82015-07-23 14:26:04 -04005945TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005946 TIntermTyped *trueExpression,
5947 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005948 const TSourceLoc &loc)
5949{
Olli Etuaho56229f12017-07-10 14:16:33 +03005950 if (!checkIsScalarBool(loc, cond))
5951 {
5952 return falseExpression;
5953 }
Olli Etuaho52901742015-04-15 13:42:45 +03005954
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005955 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005956 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005957 TInfoSinkBase reasonStream;
5958 reasonStream << "mismatching ternary operator operand types '" << trueExpression->getType()
5959 << " and '" << falseExpression->getType() << "'";
5960 error(loc, reasonStream.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005961 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005962 }
Olli Etuahode318b22016-10-25 16:18:25 +01005963 if (IsOpaqueType(trueExpression->getBasicType()))
5964 {
5965 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005966 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005967 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5968 // Note that structs containing opaque types don't need to be checked as structs are
5969 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005971 return falseExpression;
5972 }
5973
Jiajia Qinbc585152017-06-23 15:42:17 +08005974 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5975 falseExpression->getMemoryQualifier().writeonly)
5976 {
5977 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5978 return falseExpression;
5979 }
5980
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005981 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005982 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005983 // ESSL 3.00.6 section 5.7:
5984 // Ternary operator support is optional for arrays. No certainty that it works across all
5985 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5986 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005987 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005988 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005989 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005990 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005991 }
Olli Etuaho94050052017-05-08 14:17:44 +03005992 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5993 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005994 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005995 return falseExpression;
5996 }
5997
Corentin Wallez0d959252016-07-12 17:26:32 -04005998 // WebGL2 section 5.26, the following results in an error:
5999 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006000 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04006001 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006002 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006003 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04006004 }
6005
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006006 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02006007 markStaticReadIfSymbol(cond);
6008 markStaticReadIfSymbol(trueExpression);
6009 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006010 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02006011 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03006012}
Olli Etuaho49300862015-02-20 14:54:49 +02006013
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006014//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006015// Parse an array of strings using yyparse.
6016//
6017// Returns 0 for success.
6018//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006019int PaParseStrings(size_t count,
6020 const char *const string[],
6021 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306022 TParseContext *context)
6023{
Yunchao He4f285442017-04-21 12:15:49 +08006024 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006025 return 1;
6026
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006027 if (glslang_initialize(context))
6028 return 1;
6029
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006030 int error = glslang_scan(count, string, length, context);
6031 if (!error)
6032 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006033
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006034 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006035
alokp@chromium.org6b495712012-06-29 00:06:58 +00006036 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006037}
Jamie Madill45bcc782016-11-07 13:58:48 -05006038
6039} // namespace sh