blob: 784426f7d2ebd0fb1918520ab634a0436747b5fa [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 Etuaho1dfd8ae2018-10-01 15:59:59 +0300978 // ANGLE should be able to fold any EvqConst expressions resulting in an integer - but to be
979 // safe against corner cases we still check for constant folding. Some interpretations of the
980 // spec have allowed constant expressions with side effects - like array length() method on a
981 // non-constant array.
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200982 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000983 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000984 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300985 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987
Olli Etuaho856c4972016-08-08 11:38:39 +0300988 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400989
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 if (constant->getBasicType() == EbtUInt)
991 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000993 }
994 else
995 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300996 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000997
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000999 {
Nicolas Capens906744a2014-06-06 15:18:07 -04001000 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001001 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001002 }
Nicolas Capens906744a2014-06-06 15:18:07 -04001003
Olli Etuaho856c4972016-08-08 11:38:39 +03001004 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -04001005 }
1006
Olli Etuaho856c4972016-08-08 11:38:39 +03001007 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -04001008 {
1009 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001010 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -04001011 }
1012
1013 // The size of arrays is restricted here to prevent issues further down the
1014 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1015 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1016 const unsigned int sizeLimit = 65536;
1017
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001019 {
1020 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001021 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001023
1024 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001028bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1029 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030{
Olli Etuaho8a176262016-08-16 14:23:01 +03001031 if ((elementQualifier.qualifier == EvqAttribute) ||
1032 (elementQualifier.qualifier == EvqVertexIn) ||
1033 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001034 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001035 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001036 TType(elementQualifier).getQualifierString());
1037 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001038 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
Olli Etuaho8a176262016-08-16 14:23:01 +03001040 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
Olli Etuaho8a176262016-08-16 14:23:01 +03001043// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001044bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1045 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001047 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001048 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001049 TInfoSinkBase typeString;
1050 typeString << TType(elementType);
1051 error(line, "cannot declare arrays of arrays", typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001052 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001053 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001054 return true;
1055}
1056
1057// Check if this qualified element type can be formed into an array. This is only called when array
1058// brackets are associated with an identifier in a declaration, like this:
1059// float a[2];
1060// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1061// are associated with the type, like this:
1062// float[2] a;
1063bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1064 const TPublicType &elementType)
1065{
1066 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1067 {
1068 return false;
1069 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001070 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1071 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1072 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001073 // Geometry shader requires each user-defined input be declared as arrays or inside input
1074 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1075 // interface matching, such variables and blocks are treated as though they were not declared
1076 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001077 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001078 sh::IsVarying(elementType.qualifier) &&
1079 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001080 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001081 TInfoSinkBase typeString;
1082 typeString << TType(elementType);
Olli Etuahoe0803872017-08-23 15:30:23 +03001083 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho72e35892018-06-20 11:43:08 +03001084 typeString.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001085 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001086 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001087 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001091void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001092 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001093 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094{
Olli Etuaho3739d232015-04-08 12:23:44 +03001095 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001096 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001097 {
1098 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001099 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001100
1101 // Generate informative error messages for ESSL1.
1102 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001103 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001104 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301105 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106 "structures containing arrays may not be declared constant since they cannot be "
1107 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001108 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001109 }
1110 else
1111 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001112 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001113 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001114 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001115 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001116 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1117 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
Olli Etuaho2935c582015-04-08 14:32:06 +03001120// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121// and update the symbol table.
1122//
Olli Etuaho2935c582015-04-08 14:32:06 +03001123// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001125bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001126 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001127 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129{
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001131
Olli Etuahofbb1c792018-01-19 16:26:59 +02001132 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001133
Olli Etuahoa78092c2018-09-26 14:16:13 +03001134 ASSERT(type->getLayoutQualifier().index == -1 ||
1135 (isExtensionEnabled(TExtension::EXT_blend_func_extended) &&
1136 mShaderType == GL_FRAGMENT_SHADER && mShaderVersion >= 300));
1137 if (type->getQualifier() == EvqFragmentOut)
1138 {
1139 if (type->getLayoutQualifier().index != -1 && type->getLayoutQualifier().location == -1)
1140 {
1141 error(line,
1142 "If index layout qualifier is specified for a fragment output, location must "
1143 "also be specified.",
1144 "index");
1145 return false;
1146 }
1147 }
1148 else
1149 {
1150 checkIndexIsNotSpecified(line, type->getLayoutQualifier().index);
1151 }
1152
Olli Etuahob60d30f2018-01-16 12:31:06 +02001153 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001154
Olli Etuaho856c4972016-08-08 11:38:39 +03001155 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156
Olli Etuaho2935c582015-04-08 14:32:06 +03001157 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001158 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001159 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001160 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001161 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001162 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001163 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001164 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001165 return false;
1166 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001167 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001168 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001169 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001170 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001171 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001172 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001173 }
1174 }
1175 else
1176 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001177 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001178 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001179 return false;
1180 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001181 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001182
Olli Etuaho8a176262016-08-16 14:23:01 +03001183 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001184 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185
Olli Etuaho437664b2018-02-28 15:38:14 +02001186 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001187 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001188 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001189 return false;
1190 }
1191
Olli Etuahob60d30f2018-01-16 12:31:06 +02001192 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001193 return false;
1194
1195 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196}
1197
Martin Radev70866b82016-07-22 15:27:42 +03001198void TParseContext::checkIsParameterQualifierValid(
1199 const TSourceLoc &line,
1200 const TTypeQualifierBuilder &typeQualifierBuilder,
1201 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301202{
Olli Etuahocce89652017-06-19 16:04:09 +03001203 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001204 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001205
1206 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301207 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001208 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1209 }
1210
1211 if (!IsImage(type->getBasicType()))
1212 {
Olli Etuaho43364892017-02-13 16:00:12 +00001213 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001214 }
1215 else
1216 {
1217 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001218 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219
Martin Radev70866b82016-07-22 15:27:42 +03001220 type->setQualifier(typeQualifier.qualifier);
1221
1222 if (typeQualifier.precision != EbpUndefined)
1223 {
1224 type->setPrecision(typeQualifier.precision);
1225 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226}
1227
Olli Etuaho703671e2017-11-08 17:47:18 +02001228template <size_t size>
1229bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1230 const std::array<TExtension, size> &extensions)
1231{
1232 ASSERT(!extensions.empty());
1233 const TExtensionBehavior &extBehavior = extensionBehavior();
1234
1235 bool canUseWithWarning = false;
1236 bool canUseWithoutWarning = false;
1237
1238 const char *errorMsgString = "";
1239 TExtension errorMsgExtension = TExtension::UNDEFINED;
1240
1241 for (TExtension extension : extensions)
1242 {
1243 auto extIter = extBehavior.find(extension);
1244 if (canUseWithWarning)
1245 {
1246 // We already have an extension that we can use, but with a warning.
1247 // See if we can use the alternative extension without a warning.
1248 if (extIter == extBehavior.end())
1249 {
1250 continue;
1251 }
1252 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1253 {
1254 canUseWithoutWarning = true;
1255 break;
1256 }
1257 continue;
1258 }
1259 if (extIter == extBehavior.end())
1260 {
1261 errorMsgString = "extension is not supported";
1262 errorMsgExtension = extension;
1263 }
1264 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1265 {
1266 errorMsgString = "extension is disabled";
1267 errorMsgExtension = extension;
1268 }
1269 else if (extIter->second == EBhWarn)
1270 {
1271 errorMsgExtension = extension;
1272 canUseWithWarning = true;
1273 }
1274 else
1275 {
1276 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1277 canUseWithoutWarning = true;
1278 break;
1279 }
1280 }
1281
1282 if (canUseWithoutWarning)
1283 {
1284 return true;
1285 }
1286 if (canUseWithWarning)
1287 {
1288 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1289 return true;
1290 }
1291 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1292 return false;
1293}
1294
1295template bool TParseContext::checkCanUseOneOfExtensions(
1296 const TSourceLoc &line,
1297 const std::array<TExtension, 1> &extensions);
1298template bool TParseContext::checkCanUseOneOfExtensions(
1299 const TSourceLoc &line,
1300 const std::array<TExtension, 2> &extensions);
1301template bool TParseContext::checkCanUseOneOfExtensions(
1302 const TSourceLoc &line,
1303 const std::array<TExtension, 3> &extensions);
1304
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001305bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001306{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001307 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001308 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309}
1310
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001311// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1312// compile-time or link-time errors are the same whether or not the declaration is empty".
1313// This function implements all the checks that are done on qualifiers regardless of if the
1314// declaration is empty.
1315void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1316 const sh::TLayoutQualifier &layoutQualifier,
1317 const TSourceLoc &location)
1318{
1319 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1320 {
1321 error(location, "Shared memory declarations cannot have layout specified", "layout");
1322 }
1323
1324 if (layoutQualifier.matrixPacking != EmpUnspecified)
1325 {
1326 error(location, "layout qualifier only valid for interface blocks",
1327 getMatrixPackingString(layoutQualifier.matrixPacking));
1328 return;
1329 }
1330
1331 if (layoutQualifier.blockStorage != EbsUnspecified)
1332 {
1333 error(location, "layout qualifier only valid for interface blocks",
1334 getBlockStorageString(layoutQualifier.blockStorage));
1335 return;
1336 }
1337
1338 if (qualifier == EvqFragmentOut)
1339 {
1340 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1341 {
1342 error(location, "invalid layout qualifier combination", "yuv");
1343 return;
1344 }
1345 }
1346 else
1347 {
1348 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1349 }
1350
Olli Etuaho95468d12017-05-04 11:14:34 +03001351 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1352 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001353 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1354 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001355 {
1356 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1357 }
1358
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001359 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001360 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001361 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001362 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001363 // We're not checking whether the uniform location is in range here since that depends on
1364 // the type of the variable.
1365 // The type can only be fully determined for non-empty declarations.
1366 }
1367 if (!canHaveLocation)
1368 {
1369 checkLocationIsNotSpecified(location, layoutQualifier);
1370 }
1371}
1372
jchen104cdac9e2017-05-08 11:01:20 +08001373void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1374 const TSourceLoc &location)
1375{
1376 if (publicType.precision != EbpHigh)
1377 {
1378 error(location, "Can only be highp", "atomic counter");
1379 }
1380 // dEQP enforces compile error if location is specified. See uniform_location.test.
1381 if (publicType.layoutQualifier.location != -1)
1382 {
1383 error(location, "location must not be set for atomic_uint", "layout");
1384 }
1385 if (publicType.layoutQualifier.binding == -1)
1386 {
1387 error(location, "no binding specified", "atomic counter");
1388 }
1389}
1390
Olli Etuaho55bde912017-10-25 13:41:13 +03001391void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001392{
Olli Etuaho55bde912017-10-25 13:41:13 +03001393 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001394 {
1395 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1396 // error. It is assumed that this applies to empty declarations as well.
1397 error(location, "empty array declaration needs to specify a size", "");
1398 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03001399
1400 if (type.getQualifier() != EvqFragmentOut)
1401 {
1402 checkIndexIsNotSpecified(location, type.getLayoutQualifier().index);
1403 }
Martin Radevb8b01222016-11-20 23:25:53 +02001404}
1405
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001406// These checks are done for all declarations that are non-empty. They're done for non-empty
1407// declarations starting a declarator list, and declarators that follow an empty declaration.
1408void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1409 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001410{
Olli Etuahofa33d582015-04-09 14:33:12 +03001411 switch (publicType.qualifier)
1412 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001413 case EvqVaryingIn:
1414 case EvqVaryingOut:
1415 case EvqAttribute:
1416 case EvqVertexIn:
1417 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001418 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001419 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001420 {
1421 error(identifierLocation, "cannot be used with a structure",
1422 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001423 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001424 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001425 break;
1426 case EvqBuffer:
1427 if (publicType.getBasicType() != EbtInterfaceBlock)
1428 {
1429 error(identifierLocation,
1430 "cannot declare buffer variables at global scope(outside a block)",
1431 getQualifierString(publicType.qualifier));
1432 return;
1433 }
1434 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001435 default:
1436 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001437 }
jchen10cc2a10e2017-05-03 14:05:12 +08001438 std::string reason(getBasicString(publicType.getBasicType()));
1439 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001440 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001441 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001442 {
1443 return;
1444 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001445
Andrei Volykhina5527072017-03-22 16:46:30 +03001446 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1447 publicType.qualifier != EvqConst) &&
1448 publicType.getBasicType() == EbtYuvCscStandardEXT)
1449 {
1450 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1451 getQualifierString(publicType.qualifier));
1452 return;
1453 }
1454
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001455 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1456 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001457 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1458 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001459 TType type(publicType);
1460 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001461 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001462 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1463 publicType.layoutQualifier);
1464 }
1465 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001466
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001467 // check for layout qualifier issues
1468 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001469
Martin Radev2cc85b32016-08-05 16:22:53 +03001470 if (IsImage(publicType.getBasicType()))
1471 {
1472
1473 switch (layoutQualifier.imageInternalFormat)
1474 {
1475 case EiifRGBA32F:
1476 case EiifRGBA16F:
1477 case EiifR32F:
1478 case EiifRGBA8:
1479 case EiifRGBA8_SNORM:
1480 if (!IsFloatImage(publicType.getBasicType()))
1481 {
1482 error(identifierLocation,
1483 "internal image format requires a floating image type",
1484 getBasicString(publicType.getBasicType()));
1485 return;
1486 }
1487 break;
1488 case EiifRGBA32I:
1489 case EiifRGBA16I:
1490 case EiifRGBA8I:
1491 case EiifR32I:
1492 if (!IsIntegerImage(publicType.getBasicType()))
1493 {
1494 error(identifierLocation,
1495 "internal image format requires an integer image type",
1496 getBasicString(publicType.getBasicType()));
1497 return;
1498 }
1499 break;
1500 case EiifRGBA32UI:
1501 case EiifRGBA16UI:
1502 case EiifRGBA8UI:
1503 case EiifR32UI:
1504 if (!IsUnsignedImage(publicType.getBasicType()))
1505 {
1506 error(identifierLocation,
1507 "internal image format requires an unsigned image type",
1508 getBasicString(publicType.getBasicType()));
1509 return;
1510 }
1511 break;
1512 case EiifUnspecified:
1513 error(identifierLocation, "layout qualifier", "No image internal format specified");
1514 return;
1515 default:
1516 error(identifierLocation, "layout qualifier", "unrecognized token");
1517 return;
1518 }
1519
1520 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1521 switch (layoutQualifier.imageInternalFormat)
1522 {
1523 case EiifR32F:
1524 case EiifR32I:
1525 case EiifR32UI:
1526 break;
1527 default:
1528 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1529 {
1530 error(identifierLocation, "layout qualifier",
1531 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1532 "image variables must be qualified readonly and/or writeonly");
1533 return;
1534 }
1535 break;
1536 }
1537 }
1538 else
1539 {
Olli Etuaho43364892017-02-13 16:00:12 +00001540 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001541 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1542 }
jchen104cdac9e2017-05-08 11:01:20 +08001543
1544 if (IsAtomicCounter(publicType.getBasicType()))
1545 {
1546 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1547 }
1548 else
1549 {
1550 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1551 }
Olli Etuaho43364892017-02-13 16:00:12 +00001552}
Martin Radev2cc85b32016-08-05 16:22:53 +03001553
Olli Etuaho43364892017-02-13 16:00:12 +00001554void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1555{
1556 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001557 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1558 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1559 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1560 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1561 // when it comes to which shaders are accepted by the compiler.
1562 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001563 if (IsImage(type.getBasicType()))
1564 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001565 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1566 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001567 }
1568 else if (IsSampler(type.getBasicType()))
1569 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001570 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1571 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001572 }
jchen104cdac9e2017-05-08 11:01:20 +08001573 else if (IsAtomicCounter(type.getBasicType()))
1574 {
1575 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1576 }
Olli Etuaho43364892017-02-13 16:00:12 +00001577 else
1578 {
1579 ASSERT(!IsOpaqueType(type.getBasicType()));
1580 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001581 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001582}
1583
Olli Etuaho856c4972016-08-08 11:38:39 +03001584void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001585 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001586 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001587{
1588
1589 if (mShaderVersion < versionRequired)
1590 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001591 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001592 }
1593}
1594
Olli Etuaho856c4972016-08-08 11:38:39 +03001595bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1596 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001597{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001598 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001599 for (size_t i = 0u; i < localSize.size(); ++i)
1600 {
1601 if (localSize[i] != -1)
1602 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001603 error(location,
1604 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1605 "global layout declaration",
1606 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001607 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001608 }
1609 }
1610
Olli Etuaho8a176262016-08-16 14:23:01 +03001611 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001612}
1613
Olli Etuaho43364892017-02-13 16:00:12 +00001614void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001615 TLayoutImageInternalFormat internalFormat)
1616{
1617 if (internalFormat != EiifUnspecified)
1618 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001619 error(location, "invalid layout qualifier: only valid when used with images",
1620 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001621 }
Olli Etuaho43364892017-02-13 16:00:12 +00001622}
1623
Olli Etuahoa78092c2018-09-26 14:16:13 +03001624void TParseContext::checkIndexIsNotSpecified(const TSourceLoc &location, int index)
1625{
1626 if (index != -1)
1627 {
1628 error(location,
1629 "invalid layout qualifier: only valid when used with a fragment shader output in "
1630 "ESSL version >= 3.00 and EXT_blend_func_extended is enabled",
1631 "index");
1632 }
1633}
1634
Olli Etuaho43364892017-02-13 16:00:12 +00001635void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1636{
1637 if (binding != -1)
1638 {
1639 error(location,
1640 "invalid layout qualifier: only valid when used with opaque types or blocks",
1641 "binding");
1642 }
1643}
1644
jchen104cdac9e2017-05-08 11:01:20 +08001645void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1646{
1647 if (offset != -1)
1648 {
1649 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1650 "offset");
1651 }
1652}
1653
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001654void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1655 int binding,
1656 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001657{
1658 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001659 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001660 {
1661 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1662 }
1663}
1664
1665void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1666 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001667 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001668{
1669 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001670 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001671 {
1672 error(location, "sampler binding greater than maximum texture units", "binding");
1673 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001674}
1675
Jiajia Qinbc585152017-06-23 15:42:17 +08001676void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1677 const TQualifier &qualifier,
1678 int binding,
1679 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001680{
1681 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001682 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001683 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001684 if (binding + size > mMaxUniformBufferBindings)
1685 {
1686 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1687 "binding");
1688 }
1689 }
1690 else if (qualifier == EvqBuffer)
1691 {
1692 if (binding + size > mMaxShaderStorageBufferBindings)
1693 {
1694 error(location,
1695 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1696 "binding");
1697 }
jchen10af713a22017-04-19 09:10:56 +08001698 }
1699}
jchen104cdac9e2017-05-08 11:01:20 +08001700void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1701{
1702 if (binding >= mMaxAtomicCounterBindings)
1703 {
1704 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1705 "binding");
1706 }
1707}
jchen10af713a22017-04-19 09:10:56 +08001708
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001709void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1710 int objectLocationCount,
1711 const TLayoutQualifier &layoutQualifier)
1712{
1713 int loc = layoutQualifier.location;
1714 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1715 {
1716 error(location, "Uniform location out of range", "location");
1717 }
1718}
1719
Andrei Volykhina5527072017-03-22 16:46:30 +03001720void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1721{
1722 if (yuv != false)
1723 {
1724 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1725 }
1726}
1727
Jiajia Qinbc585152017-06-23 15:42:17 +08001728void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1729 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001730{
1731 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1732 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001733 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001734 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho94bbed12018-03-20 14:44:53 +02001735 bool argumentIsRead = (IsQualifierUnspecified(qual) || qual == EvqIn || qual == EvqInOut ||
1736 qual == EvqConstReadOnly);
1737 if (argumentIsRead)
Jiajia Qinbc585152017-06-23 15:42:17 +08001738 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001739 markStaticReadIfSymbol(argument);
1740 if (!IsImage(argument->getBasicType()))
Jiajia Qinbc585152017-06-23 15:42:17 +08001741 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001742 if (argument->getMemoryQualifier().writeonly)
1743 {
1744 error(argument->getLine(),
1745 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1746 fnCall->functionName());
1747 return;
1748 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001749 }
1750 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001751 if (qual == EvqOut || qual == EvqInOut)
1752 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001753 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001754 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001755 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001756 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001757 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001758 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001759 }
1760 }
1761 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001762}
1763
Martin Radev70866b82016-07-22 15:27:42 +03001764void TParseContext::checkInvariantVariableQualifier(bool invariant,
1765 const TQualifier qualifier,
1766 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001767{
Martin Radev70866b82016-07-22 15:27:42 +03001768 if (!invariant)
1769 return;
1770
1771 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001772 {
Martin Radev70866b82016-07-22 15:27:42 +03001773 // input variables in the fragment shader can be also qualified as invariant
1774 if (!sh::CanBeInvariantESSL1(qualifier))
1775 {
1776 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1777 }
1778 }
1779 else
1780 {
1781 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1782 {
1783 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1784 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001785 }
1786}
1787
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001788bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001789{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001790 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001791}
1792
Jamie Madillb98c3a82015-07-23 14:26:04 -04001793void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1794 const char *extName,
1795 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001796{
Geoff Lang197d5292018-04-25 14:29:00 -04001797 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001798 srcLoc.file = loc.first_file;
1799 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001800 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001801}
1802
Jamie Madillb98c3a82015-07-23 14:26:04 -04001803void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1804 const char *name,
1805 const char *value,
1806 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001807{
Geoff Lang197d5292018-04-25 14:29:00 -04001808 angle::pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -04001809 srcLoc.file = loc.first_file;
1810 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001811 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001812}
1813
Martin Radev4c4c8e72016-08-04 12:25:34 +03001814sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001815{
Jamie Madill2f294c92017-11-20 14:47:26 -05001816 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001817 for (size_t i = 0u; i < result.size(); ++i)
1818 {
1819 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1820 {
1821 result[i] = 1;
1822 }
1823 else
1824 {
1825 result[i] = mComputeShaderLocalSize[i];
1826 }
1827 }
1828 return result;
1829}
1830
Olli Etuaho56229f12017-07-10 14:16:33 +03001831TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1832 const TSourceLoc &line)
1833{
1834 TIntermConstantUnion *node = new TIntermConstantUnion(
1835 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1836 node->setLine(line);
1837 return node;
1838}
1839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840/////////////////////////////////////////////////////////////////////////////////
1841//
1842// Non-Errors.
1843//
1844/////////////////////////////////////////////////////////////////////////////////
1845
Jamie Madill5c097022014-08-20 16:38:32 -04001846const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001847 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001848 const TSymbol *symbol)
1849{
Jamie Madill5c097022014-08-20 16:38:32 -04001850 if (!symbol)
1851 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001852 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001853 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001854 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001855
1856 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001857 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001858 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001859 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001860 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001861
1862 const TVariable *variable = static_cast<const TVariable *>(symbol);
1863
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001864 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001865 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001866 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001867 }
1868
Olli Etuaho0f684632017-07-13 12:42:15 +03001869 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1870 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
Olli Etuaho59c5b892018-04-03 11:44:50 +03001871 variable->getType().getQualifier() == EvqWorkGroupSize)
Olli Etuaho0f684632017-07-13 12:42:15 +03001872 {
1873 error(location,
1874 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1875 "gl_WorkGroupSize");
1876 }
Jamie Madill5c097022014-08-20 16:38:32 -04001877 return variable;
1878}
1879
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001880TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001881 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001882 const TSymbol *symbol)
1883{
1884 const TVariable *variable = getNamedVariable(location, name, symbol);
1885
Olli Etuaho0f684632017-07-13 12:42:15 +03001886 if (!variable)
1887 {
1888 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1889 node->setLine(location);
1890 return node;
1891 }
1892
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001893 const TType &variableType = variable->getType();
Jamie Madill50cf2be2018-06-15 09:46:57 -04001894 TIntermTyped *node = nullptr;
Olli Etuaho56229f12017-07-10 14:16:33 +03001895
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001896 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001897 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001898 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001899 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001900 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001901 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001902 {
1903 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1904 // needs to be added to the AST as a constant and not as a symbol.
1905 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1906 TConstantUnion *constArray = new TConstantUnion[3];
1907 for (size_t i = 0; i < 3; ++i)
1908 {
1909 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1910 }
1911
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001912 ASSERT(variableType.getBasicType() == EbtUInt);
1913 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001914
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001915 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001916 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001917 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001918 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001919 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1920 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001921 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02001922 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
1923 node = new TIntermSymbol(symbolTable.getGlInVariableWithArraySize());
Jiawei Shaod8105a02017-08-08 09:54:36 +08001924 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001925 else
1926 {
Olli Etuaho195be942017-12-04 23:40:14 +02001927 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001928 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001929 ASSERT(node != nullptr);
1930 node->setLine(location);
1931 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001932}
1933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934// Initializers show up in several places in the grammar. Have one set of
1935// code to handle them here.
1936//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001938bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001939 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001940 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001941 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001942 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943{
Olli Etuaho13389b62016-10-16 11:48:18 +01001944 ASSERT(initNode != nullptr);
1945 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946
Olli Etuahob60d30f2018-01-16 12:31:06 +02001947 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001948 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001949 // In case initializer is not an array or type has more dimensions than initializer, this
1950 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1951 // actually is an array or not. Having a non-array initializer for an unsized array will
1952 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001953 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001954 type->sizeUnsizedArrays(arraySizes);
1955 }
1956
1957 const TQualifier qualifier = type->getQualifier();
1958
1959 bool constError = false;
1960 if (qualifier == EvqConst)
1961 {
1962 if (EvqConst != initializer->getType().getQualifier())
1963 {
Olli Etuaho72e35892018-06-20 11:43:08 +03001964 TInfoSinkBase reasonStream;
1965 reasonStream << "assigning non-constant to '" << *type << "'";
1966 error(line, reasonStream.c_str(), "=");
Olli Etuahob60d30f2018-01-16 12:31:06 +02001967
1968 // We're still going to declare the variable to avoid extra error messages.
1969 type->setQualifier(EvqTemporary);
1970 constError = true;
1971 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001972 }
Olli Etuaho195be942017-12-04 23:40:14 +02001973
1974 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001975 if (!declareVariable(line, identifier, type, &variable))
1976 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001977 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001978 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979
Olli Etuahob60d30f2018-01-16 12:31:06 +02001980 if (constError)
1981 {
1982 return false;
1983 }
1984
Olli Etuahob0c645e2015-05-12 14:25:36 +03001985 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001986 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001987 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001988 {
1989 // Error message does not completely match behavior with ESSL 1.00, but
1990 // we want to steer developers towards only using constant expressions.
1991 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001993 }
1994 if (globalInitWarning)
1995 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001996 warning(
1997 line,
1998 "global variable initializers should be constant expressions "
1999 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
2000 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03002001 }
2002
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002003 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05302004 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
2005 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002006 error(line, " cannot initialize this type of qualifier ",
2007 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002008 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002009 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02002010
2011 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
2012 intermSymbol->setLine(line);
2013
2014 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
2015 {
Olli Etuaho72e35892018-06-20 11:43:08 +03002016 assignError(line, "=", variable->getType(), initializer->getType());
Olli Etuahob60d30f2018-01-16 12:31:06 +02002017 return false;
2018 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019
Arun Patole7e7e68d2015-05-22 12:02:25 +05302020 if (qualifier == EvqConst)
2021 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002022 // Save the constant folded value to the variable if possible.
2023 const TConstantUnion *constArray = initializer->getConstantValue();
2024 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302025 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002026 variable->shareConstPointer(constArray);
2027 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002028 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002029 ASSERT(*initNode == nullptr);
2030 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002031 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002032 }
2033 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002034
Olli Etuahob60d30f2018-01-16 12:31:06 +02002035 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002036 markStaticReadIfSymbol(initializer);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002037 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002038 return true;
2039}
2040
2041TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002042 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002043 TIntermTyped *initializer,
2044 const TSourceLoc &loc)
2045{
2046 checkIsScalarBool(loc, pType);
2047 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002048 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002049 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002050 {
2051 // The initializer is valid. The init condition needs to have a node - either the
2052 // initializer node, or a constant node in case the initialized variable is const and won't
2053 // be recorded in the AST.
2054 if (initNode == nullptr)
2055 {
2056 return initializer;
2057 }
2058 else
2059 {
2060 TIntermDeclaration *declaration = new TIntermDeclaration();
2061 declaration->appendDeclarator(initNode);
2062 return declaration;
2063 }
2064 }
2065 return nullptr;
2066}
2067
2068TIntermNode *TParseContext::addLoop(TLoopType type,
2069 TIntermNode *init,
2070 TIntermNode *cond,
2071 TIntermTyped *expr,
2072 TIntermNode *body,
2073 const TSourceLoc &line)
2074{
2075 TIntermNode *node = nullptr;
2076 TIntermTyped *typedCond = nullptr;
2077 if (cond)
2078 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002079 markStaticReadIfSymbol(cond);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002080 typedCond = cond->getAsTyped();
2081 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02002082 if (expr)
2083 {
2084 markStaticReadIfSymbol(expr);
2085 }
2086 // In case the loop body was not parsed as a block and contains a statement that simply refers
2087 // to a variable, we need to mark it as statically used.
2088 if (body)
2089 {
2090 markStaticReadIfSymbol(body);
2091 }
Olli Etuaho914b79a2017-06-19 16:03:19 +03002092 if (cond == nullptr || typedCond)
2093 {
Olli Etuahocce89652017-06-19 16:04:09 +03002094 if (type == ELoopDoWhile)
2095 {
2096 checkIsScalarBool(line, typedCond);
2097 }
2098 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2099 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2100 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2101 !typedCond->isVector()));
2102
Olli Etuaho3ec75682017-07-05 17:02:55 +03002103 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002104 node->setLine(line);
2105 return node;
2106 }
2107
Olli Etuahocce89652017-06-19 16:04:09 +03002108 ASSERT(type != ELoopDoWhile);
2109
Olli Etuaho914b79a2017-06-19 16:03:19 +03002110 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2111 ASSERT(declaration);
2112 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2113 ASSERT(declarator->getLeft()->getAsSymbolNode());
2114
2115 // The condition is a declaration. In the AST representation we don't support declarations as
2116 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2117 // the loop.
2118 TIntermBlock *block = new TIntermBlock();
2119
2120 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2121 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2122 block->appendStatement(declareCondition);
2123
2124 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2125 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002126 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002127 block->appendStatement(loop);
2128 loop->setLine(line);
2129 block->setLine(line);
2130 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131}
2132
Olli Etuahocce89652017-06-19 16:04:09 +03002133TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2134 TIntermNodePair code,
2135 const TSourceLoc &loc)
2136{
Olli Etuaho56229f12017-07-10 14:16:33 +03002137 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuaho94bbed12018-03-20 14:44:53 +02002138 // In case the conditional statements were not parsed as blocks and contain a statement that
2139 // simply refers to a variable, we need to mark them as statically used.
2140 if (code.node1)
2141 {
2142 markStaticReadIfSymbol(code.node1);
2143 }
2144 if (code.node2)
2145 {
2146 markStaticReadIfSymbol(code.node2);
2147 }
Olli Etuahocce89652017-06-19 16:04:09 +03002148
2149 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002150 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002151 {
2152 if (cond->getAsConstantUnion()->getBConst(0) == true)
2153 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002154 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002155 }
2156 else
2157 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002158 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002159 }
2160 }
2161
Olli Etuaho3ec75682017-07-05 17:02:55 +03002162 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuaho94bbed12018-03-20 14:44:53 +02002163 markStaticReadIfSymbol(cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002164 node->setLine(loc);
2165
2166 return node;
2167}
2168
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002169void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2170{
2171 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2172 typeSpecifier->getBasicType());
2173
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002174 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002175 {
2176 error(typeSpecifier->getLine(), "not supported", "first-class array");
2177 typeSpecifier->clearArrayness();
2178 }
2179}
2180
Martin Radev70866b82016-07-22 15:27:42 +03002181TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302182 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002183{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002184 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002185
Martin Radev70866b82016-07-22 15:27:42 +03002186 TPublicType returnType = typeSpecifier;
2187 returnType.qualifier = typeQualifier.qualifier;
2188 returnType.invariant = typeQualifier.invariant;
2189 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002190 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002191 returnType.precision = typeSpecifier.precision;
2192
2193 if (typeQualifier.precision != EbpUndefined)
2194 {
2195 returnType.precision = typeQualifier.precision;
2196 }
2197
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2199 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002200
Martin Radev4a9cd802016-09-01 16:51:51 +03002201 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2202 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002203
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002205
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002206 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002207 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002208 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002209 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002210 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002211 returnType.clearArrayness();
2212 }
2213
Martin Radev70866b82016-07-22 15:27:42 +03002214 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002215 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002216 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002217 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002218 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002219 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002220
Martin Radev70866b82016-07-22 15:27:42 +03002221 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002222 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002223 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002224 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002225 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002226 }
2227 }
2228 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002229 {
Martin Radev70866b82016-07-22 15:27:42 +03002230 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002231 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002232 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002233 }
Martin Radev70866b82016-07-22 15:27:42 +03002234 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2235 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002236 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002237 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2238 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002239 }
Martin Radev70866b82016-07-22 15:27:42 +03002240 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002241 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002242 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002243 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002244 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002245 }
2246
2247 return returnType;
2248}
2249
Olli Etuaho856c4972016-08-08 11:38:39 +03002250void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2251 const TPublicType &type,
2252 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002253{
2254 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002255 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 {
2257 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002258 }
2259
2260 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2261 switch (qualifier)
2262 {
2263 case EvqVertexIn:
2264 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002265 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002266 {
2267 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002268 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002269 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 return;
2271 case EvqFragmentOut:
2272 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002273 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002274 {
2275 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002276 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002277 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002278 return;
2279 default:
2280 break;
2281 }
2282
2283 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2284 // restrictions.
2285 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002286 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2287 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002288 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2289 {
2290 error(qualifierLocation, "must use 'flat' interpolation here",
2291 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002292 }
2293
Martin Radev4a9cd802016-09-01 16:51:51 +03002294 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002295 {
2296 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2297 // These restrictions are only implied by the ESSL 3.00 spec, but
2298 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002299 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002300 {
2301 error(qualifierLocation, "cannot be an array of structures",
2302 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002303 }
2304 if (type.isStructureContainingArrays())
2305 {
2306 error(qualifierLocation, "cannot be a structure containing an array",
2307 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002308 }
2309 if (type.isStructureContainingType(EbtStruct))
2310 {
2311 error(qualifierLocation, "cannot be a structure containing a structure",
2312 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002313 }
2314 if (type.isStructureContainingType(EbtBool))
2315 {
2316 error(qualifierLocation, "cannot be a structure containing a bool",
2317 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002318 }
2319 }
2320}
2321
Martin Radev2cc85b32016-08-05 16:22:53 +03002322void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2323{
2324 if (qualifier.getType() == QtStorage)
2325 {
2326 const TStorageQualifierWrapper &storageQualifier =
2327 static_cast<const TStorageQualifierWrapper &>(qualifier);
2328 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2329 !symbolTable.atGlobalLevel())
2330 {
2331 error(storageQualifier.getLine(),
2332 "Local variables can only use the const storage qualifier.",
Olli Etuaho1a3bbaa2018-01-25 11:41:31 +02002333 storageQualifier.getQualifierString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002334 }
2335 }
2336}
2337
Olli Etuaho43364892017-02-13 16:00:12 +00002338void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002339 const TSourceLoc &location)
2340{
Jiajia Qinbc585152017-06-23 15:42:17 +08002341 const std::string reason(
2342 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2343 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002344 if (memoryQualifier.readonly)
2345 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002346 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002347 }
2348 if (memoryQualifier.writeonly)
2349 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002350 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002351 }
Martin Radev049edfa2016-11-11 14:35:37 +02002352 if (memoryQualifier.coherent)
2353 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002354 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002355 }
2356 if (memoryQualifier.restrictQualifier)
2357 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002358 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002359 }
2360 if (memoryQualifier.volatileQualifier)
2361 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002362 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002363 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002364}
2365
jchen104cdac9e2017-05-08 11:01:20 +08002366// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2367// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002368void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2369 const TSourceLoc &loc,
2370 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002371{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002372 if (!IsAtomicCounter(type->getBasicType()))
2373 {
2374 return;
2375 }
2376
2377 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2378 : kAtomicCounterSize;
2379 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2380 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002381 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002382 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002383 {
2384 offset = bindingState.appendSpan(size);
2385 }
2386 else
2387 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002388 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002389 }
2390 if (offset == -1)
2391 {
2392 error(loc, "Offset overlapping", "atomic counter");
2393 return;
2394 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002395 layoutQualifier.offset = offset;
2396 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002397}
2398
Olli Etuaho454c34c2017-10-25 16:35:56 +03002399void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002400 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002401 TType *type)
2402{
2403 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2404 {
2405 if (type->isArray() && type->getOutermostArraySize() == 0u)
2406 {
2407 // Set size for the unsized geometry shader inputs if they are declared after a valid
2408 // input primitive declaration.
2409 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2410 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02002411 ASSERT(symbolTable.getGlInVariableWithArraySize() != nullptr);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002412 type->sizeOutermostUnsizedArray(
Olli Etuaho94bbed12018-03-20 14:44:53 +02002413 symbolTable.getGlInVariableWithArraySize()->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002414 }
2415 else
2416 {
2417 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2418 // An input can be declared without an array size if there is a previous layout
2419 // which specifies the size.
2420 error(location,
2421 "Missing a valid input primitive declaration before declaring an unsized "
2422 "array input",
2423 token);
2424 }
2425 }
2426 else if (type->isArray())
2427 {
2428 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2429 }
2430 else
2431 {
2432 error(location, "Geometry shader input variable must be declared as an array", token);
2433 }
2434 }
2435}
2436
Olli Etuaho13389b62016-10-16 11:48:18 +01002437TIntermDeclaration *TParseContext::parseSingleDeclaration(
2438 TPublicType &publicType,
2439 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002440 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002441{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002442 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002443 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2444 mDirectiveHandler.pragma().stdgl.invariantAll)
2445 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002446 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002447
2448 // The directive handler has already taken care of rejecting invalid uses of this pragma
2449 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2450 // affected variable declarations:
2451 //
2452 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2453 // elsewhere, in TranslatorGLSL.)
2454 //
2455 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2456 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2457 // the way this is currently implemented we have to enable this compiler option before
2458 // parsing the shader and determining the shading language version it uses. If this were
2459 // implemented as a post-pass, the workaround could be more targeted.
2460 //
2461 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2462 // the specification, but there are desktop OpenGL drivers that expect that this is the
2463 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2464 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2465 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002466 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002467 }
2468 }
2469
Olli Etuahofbb1c792018-01-19 16:26:59 +02002470 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002471
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002472 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2473 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002474
Jamie Madill50cf2be2018-06-15 09:46:57 -04002475 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002477
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002478 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002479 if (emptyDeclaration)
2480 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002481 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002482 // In most cases we don't need to create a symbol node for an empty declaration.
2483 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002484 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002485 {
Olli Etuaho195be942017-12-04 23:40:14 +02002486 TVariable *emptyVariable =
Jamie Madillb779b122018-06-20 11:46:43 -04002487 new TVariable(&symbolTable, kEmptyImmutableString, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002488 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002489 }
jchen104cdac9e2017-05-08 11:01:20 +08002490 else if (IsAtomicCounter(publicType.getBasicType()))
2491 {
2492 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2493 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002494 }
2495 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002496 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002497 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuahob60d30f2018-01-16 12:31:06 +02002499 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002500
Olli Etuahob60d30f2018-01-16 12:31:06 +02002501 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002502
Olli Etuaho2935c582015-04-08 14:32:06 +03002503 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002504 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002505 {
Olli Etuaho195be942017-12-04 23:40:14 +02002506 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002507 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002508 }
2509
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002510 TIntermDeclaration *declaration = new TIntermDeclaration();
2511 declaration->setLine(identifierOrTypeLocation);
2512 if (symbol)
2513 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002514 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002515 declaration->appendDeclarator(symbol);
2516 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002518}
2519
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002520TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2521 TPublicType &elementType,
2522 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002523 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002524 const TSourceLoc &indexLocation,
2525 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002526{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002527 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002528
Olli Etuaho55bde912017-10-25 13:41:13 +03002529 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002530 identifierLocation);
2531
Olli Etuaho55bde912017-10-25 13:41:13 +03002532 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002533
Olli Etuaho55bde912017-10-25 13:41:13 +03002534 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002535
Olli Etuahob60d30f2018-01-16 12:31:06 +02002536 TType *arrayType = new TType(elementType);
2537 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002538
Olli Etuahofbb1c792018-01-19 16:26:59 +02002539 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002540
Olli Etuahob60d30f2018-01-16 12:31:06 +02002541 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002542
Olli Etuahob60d30f2018-01-16 12:31:06 +02002543 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002544
Olli Etuaho13389b62016-10-16 11:48:18 +01002545 TIntermDeclaration *declaration = new TIntermDeclaration();
2546 declaration->setLine(identifierLocation);
2547
Olli Etuaho195be942017-12-04 23:40:14 +02002548 TVariable *variable = nullptr;
2549 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002550 {
Olli Etuaho195be942017-12-04 23:40:14 +02002551 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002552 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002553 declaration->appendDeclarator(symbol);
2554 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002557}
2558
Olli Etuaho13389b62016-10-16 11:48:18 +01002559TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2560 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002561 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002562 const TSourceLoc &initLocation,
2563 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002564{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002566
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2568 identifierLocation);
2569
2570 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002571
Olli Etuaho13389b62016-10-16 11:48:18 +01002572 TIntermDeclaration *declaration = new TIntermDeclaration();
2573 declaration->setLine(identifierLocation);
2574
2575 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002576 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002577 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002578 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002579 if (initNode)
2580 {
2581 declaration->appendDeclarator(initNode);
2582 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002583 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002584 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002585}
2586
Olli Etuaho13389b62016-10-16 11:48:18 +01002587TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002588 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002589 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002590 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002591 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002592 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002593 const TSourceLoc &initLocation,
2594 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002595{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002596 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002597
Olli Etuaho55bde912017-10-25 13:41:13 +03002598 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002599 identifierLocation);
2600
Olli Etuaho55bde912017-10-25 13:41:13 +03002601 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002602
Olli Etuaho55bde912017-10-25 13:41:13 +03002603 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002604
Olli Etuahob60d30f2018-01-16 12:31:06 +02002605 TType *arrayType = new TType(elementType);
2606 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002607
Olli Etuaho13389b62016-10-16 11:48:18 +01002608 TIntermDeclaration *declaration = new TIntermDeclaration();
2609 declaration->setLine(identifierLocation);
2610
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002611 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002612 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002613 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002614 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002615 if (initNode)
2616 {
2617 declaration->appendDeclarator(initNode);
2618 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002619 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002620
2621 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002622}
2623
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002624TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002625 const TTypeQualifierBuilder &typeQualifierBuilder,
2626 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002627 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002628 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002629{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002630 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002631
Martin Radev70866b82016-07-22 15:27:42 +03002632 if (!typeQualifier.invariant)
2633 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002634 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002635 return nullptr;
2636 }
2637 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2638 {
2639 return nullptr;
2640 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002641 if (!symbol)
2642 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002643 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002644 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002645 }
Martin Radev70866b82016-07-22 15:27:42 +03002646 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002647 {
Martin Radev70866b82016-07-22 15:27:42 +03002648 error(identifierLoc, "invariant declaration specifies qualifier",
2649 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002650 }
Martin Radev70866b82016-07-22 15:27:42 +03002651 if (typeQualifier.precision != EbpUndefined)
2652 {
2653 error(identifierLoc, "invariant declaration specifies precision",
2654 getPrecisionString(typeQualifier.precision));
2655 }
2656 if (!typeQualifier.layoutQualifier.isEmpty())
2657 {
2658 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2659 }
2660
2661 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002662 if (!variable)
2663 {
2664 return nullptr;
2665 }
Martin Radev70866b82016-07-22 15:27:42 +03002666 const TType &type = variable->getType();
2667
2668 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2669 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002670 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002671
Olli Etuaho76b2c382018-03-19 15:51:29 +02002672 symbolTable.addInvariantVarying(*variable);
Martin Radev70866b82016-07-22 15:27:42 +03002673
Olli Etuaho195be942017-12-04 23:40:14 +02002674 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002675 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002676
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002677 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002678}
2679
Olli Etuaho13389b62016-10-16 11:48:18 +01002680void TParseContext::parseDeclarator(TPublicType &publicType,
2681 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002682 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002684{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002685 // If the declaration starting this declarator list was empty (example: int,), some checks were
2686 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002687 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002688 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002689 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2690 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002691 }
2692
Olli Etuaho856c4972016-08-08 11:38:39 +03002693 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002694
Olli Etuahob60d30f2018-01-16 12:31:06 +02002695 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002696
Olli Etuahofbb1c792018-01-19 16:26:59 +02002697 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002698
Olli Etuahob60d30f2018-01-16 12:31:06 +02002699 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002700
Olli Etuahob60d30f2018-01-16 12:31:06 +02002701 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002702
Olli Etuaho195be942017-12-04 23:40:14 +02002703 TVariable *variable = nullptr;
2704 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002705 {
Olli Etuaho195be942017-12-04 23:40:14 +02002706 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002707 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002708 declarationOut->appendDeclarator(symbol);
2709 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002710}
2711
Olli Etuaho55bde912017-10-25 13:41:13 +03002712void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002713 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002714 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002715 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002716 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002717 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002718{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002719 // If the declaration starting this declarator list was empty (example: int,), some checks were
2720 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002721 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002722 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002723 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002724 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002725 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002726
Olli Etuaho55bde912017-10-25 13:41:13 +03002727 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002728
Olli Etuaho55bde912017-10-25 13:41:13 +03002729 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002730 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002731 TType *arrayType = new TType(elementType);
2732 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002733
Olli Etuahofbb1c792018-01-19 16:26:59 +02002734 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002735
Olli Etuahob60d30f2018-01-16 12:31:06 +02002736 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002737
Olli Etuahob60d30f2018-01-16 12:31:06 +02002738 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002739
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002740 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002741 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002742 {
Olli Etuaho195be942017-12-04 23:40:14 +02002743 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002744 symbol->setLine(identifierLocation);
2745 declarationOut->appendDeclarator(symbol);
2746 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002747 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002748}
2749
Olli Etuaho13389b62016-10-16 11:48:18 +01002750void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2751 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002752 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002753 const TSourceLoc &initLocation,
2754 TIntermTyped *initializer,
2755 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002756{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002757 // If the declaration starting this declarator list was empty (example: int,), some checks were
2758 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002759 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002760 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002761 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2762 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002763 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002764
Olli Etuaho856c4972016-08-08 11:38:39 +03002765 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002766
Olli Etuaho13389b62016-10-16 11:48:18 +01002767 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002768 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002769 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002770 {
2771 //
2772 // build the intermediate representation
2773 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002774 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002775 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002776 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002777 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002778 }
2779}
2780
Olli Etuaho55bde912017-10-25 13:41:13 +03002781void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002782 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002783 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002784 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002785 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002786 const TSourceLoc &initLocation,
2787 TIntermTyped *initializer,
2788 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002789{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790 // If the declaration starting this declarator list was empty (example: int,), some checks were
2791 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002792 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002793 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002794 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002795 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002796 }
2797
Olli Etuaho55bde912017-10-25 13:41:13 +03002798 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002799
Olli Etuaho55bde912017-10-25 13:41:13 +03002800 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002801
Olli Etuahob60d30f2018-01-16 12:31:06 +02002802 TType *arrayType = new TType(elementType);
2803 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002804
2805 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002806 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002807 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002808 {
2809 if (initNode)
2810 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002811 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002812 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002813 }
2814}
2815
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002816TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2817{
2818 // It's simpler to parse an empty statement as a constant expression rather than having a
2819 // different type of node just for empty statements, that will be pruned from the AST anyway.
2820 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2821 node->setLine(location);
2822 return node;
2823}
2824
jchen104cdac9e2017-05-08 11:01:20 +08002825void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2826 const TSourceLoc &location)
2827{
2828 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2829 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2830 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2831 {
2832 error(location, "Requires both binding and offset", "layout");
2833 return;
2834 }
2835 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2836}
2837
Olli Etuahocce89652017-06-19 16:04:09 +03002838void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2839 const TPublicType &type,
2840 const TSourceLoc &loc)
2841{
2842 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2843 !getFragmentPrecisionHigh())
2844 {
2845 error(loc, "precision is not supported in fragment shader", "highp");
2846 }
2847
2848 if (!CanSetDefaultPrecisionOnType(type))
2849 {
2850 error(loc, "illegal type argument for default precision qualifier",
2851 getBasicString(type.getBasicType()));
2852 return;
2853 }
2854 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2855}
2856
Shaob5cc1192017-07-06 10:47:20 +08002857bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2858{
2859 switch (typeQualifier.layoutQualifier.primitiveType)
2860 {
2861 case EptLines:
2862 case EptLinesAdjacency:
2863 case EptTriangles:
2864 case EptTrianglesAdjacency:
2865 return typeQualifier.qualifier == EvqGeometryIn;
2866
2867 case EptLineStrip:
2868 case EptTriangleStrip:
2869 return typeQualifier.qualifier == EvqGeometryOut;
2870
2871 case EptPoints:
2872 return true;
2873
2874 default:
2875 UNREACHABLE();
2876 return false;
2877 }
2878}
2879
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002880void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2881 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002882{
Olli Etuaho94bbed12018-03-20 14:44:53 +02002883 if (!symbolTable.setGlInArraySize(inputArraySize))
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002884 {
2885 error(line,
2886 "Array size or input primitive declaration doesn't match the size of earlier sized "
2887 "array inputs.",
2888 "layout");
2889 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002890}
2891
Shaob5cc1192017-07-06 10:47:20 +08002892bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2893{
2894 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2895
2896 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2897
2898 if (layoutQualifier.maxVertices != -1)
2899 {
2900 error(typeQualifier.line,
2901 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2902 return false;
2903 }
2904
2905 // Set mGeometryInputPrimitiveType if exists
2906 if (layoutQualifier.primitiveType != EptUndefined)
2907 {
2908 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2909 {
2910 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2911 return false;
2912 }
2913
2914 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2915 {
2916 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002917 setGeometryShaderInputArraySize(
2918 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2919 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002920 }
2921 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2922 {
2923 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2924 "layout");
2925 return false;
2926 }
2927 }
2928
2929 // Set mGeometryInvocations if exists
2930 if (layoutQualifier.invocations > 0)
2931 {
2932 if (mGeometryShaderInvocations == 0)
2933 {
2934 mGeometryShaderInvocations = layoutQualifier.invocations;
2935 }
2936 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2937 {
2938 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2939 "layout");
2940 return false;
2941 }
2942 }
2943
2944 return true;
2945}
2946
2947bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2948{
2949 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2950
2951 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2952
2953 if (layoutQualifier.invocations > 0)
2954 {
2955 error(typeQualifier.line,
2956 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2957 return false;
2958 }
2959
2960 // Set mGeometryOutputPrimitiveType if exists
2961 if (layoutQualifier.primitiveType != EptUndefined)
2962 {
2963 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2964 {
2965 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2966 return false;
2967 }
2968
2969 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2970 {
2971 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2972 }
2973 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2974 {
2975 error(typeQualifier.line,
2976 "primitive doesn't match earlier output primitive declaration", "layout");
2977 return false;
2978 }
2979 }
2980
2981 // Set mGeometryMaxVertices if exists
2982 if (layoutQualifier.maxVertices > -1)
2983 {
2984 if (mGeometryShaderMaxVertices == -1)
2985 {
2986 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2987 }
2988 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2989 {
2990 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2991 "layout");
2992 return false;
2993 }
2994 }
2995
2996 return true;
2997}
2998
Martin Radev70866b82016-07-22 15:27:42 +03002999void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04003000{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003001 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04003002 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04003003
Martin Radev70866b82016-07-22 15:27:42 +03003004 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
3005 typeQualifier.line);
3006
Jamie Madillc2128ff2016-07-04 10:26:17 -04003007 // It should never be the case, but some strange parser errors can send us here.
3008 if (layoutQualifier.isEmpty())
3009 {
3010 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04003011 return;
3012 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003013
Martin Radev802abe02016-08-04 17:48:32 +03003014 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04003015 {
Olli Etuaho43364892017-02-13 16:00:12 +00003016 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04003017 return;
3018 }
3019
Olli Etuahoa78092c2018-09-26 14:16:13 +03003020 checkIndexIsNotSpecified(typeQualifier.line, layoutQualifier.index);
3021
Olli Etuaho43364892017-02-13 16:00:12 +00003022 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
3023
3024 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03003025
3026 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
3027
Andrei Volykhina5527072017-03-22 16:46:30 +03003028 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
3029
jchen104cdac9e2017-05-08 11:01:20 +08003030 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
3031
Qin Jiajiaca68d982017-09-18 16:41:56 +08003032 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3033 typeQualifier.qualifier);
3034
Martin Radev802abe02016-08-04 17:48:32 +03003035 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003036 {
Martin Radev802abe02016-08-04 17:48:32 +03003037 if (mComputeShaderLocalSizeDeclared &&
3038 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3039 {
3040 error(typeQualifier.line, "Work group size does not match the previous declaration",
3041 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003042 return;
3043 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003044
Martin Radev802abe02016-08-04 17:48:32 +03003045 if (mShaderVersion < 310)
3046 {
3047 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003048 return;
3049 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003050
Martin Radev4c4c8e72016-08-04 12:25:34 +03003051 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003052 {
3053 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003054 return;
3055 }
3056
3057 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003058 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003059
3060 const TConstantUnion *maxComputeWorkGroupSizeData =
3061 maxComputeWorkGroupSize->getConstPointer();
3062
3063 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3064 {
3065 if (layoutQualifier.localSize[i] != -1)
3066 {
3067 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3068 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3069 if (mComputeShaderLocalSize[i] < 1 ||
3070 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3071 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003072 std::stringstream reasonStream;
3073 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3074 << maxComputeWorkGroupSizeValue;
3075 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003076
Olli Etuaho4de340a2016-12-16 09:32:03 +00003077 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003078 return;
3079 }
3080 }
3081 }
3082
3083 mComputeShaderLocalSizeDeclared = true;
3084 }
Shaob5cc1192017-07-06 10:47:20 +08003085 else if (typeQualifier.qualifier == EvqGeometryIn)
3086 {
3087 if (mShaderVersion < 310)
3088 {
3089 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3090 return;
3091 }
3092
3093 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3094 {
3095 return;
3096 }
3097 }
3098 else if (typeQualifier.qualifier == EvqGeometryOut)
3099 {
3100 if (mShaderVersion < 310)
3101 {
3102 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3103 "layout");
3104 return;
3105 }
3106
3107 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3108 {
3109 return;
3110 }
3111 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003112 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3113 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003114 {
3115 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3116 // specification.
3117 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3118 {
3119 error(typeQualifier.line, "Number of views does not match the previous declaration",
3120 "layout");
3121 return;
3122 }
3123
3124 if (layoutQualifier.numViews == -1)
3125 {
3126 error(typeQualifier.line, "No num_views specified", "layout");
3127 return;
3128 }
3129
3130 if (layoutQualifier.numViews > mMaxNumViews)
3131 {
3132 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3133 "layout");
3134 return;
3135 }
3136
3137 mNumViews = layoutQualifier.numViews;
3138 }
Martin Radev802abe02016-08-04 17:48:32 +03003139 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003140 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003141 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003142 {
Martin Radev802abe02016-08-04 17:48:32 +03003143 return;
3144 }
3145
Jiajia Qinbc585152017-06-23 15:42:17 +08003146 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003147 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003148 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003149 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003150 return;
3151 }
3152
3153 if (mShaderVersion < 300)
3154 {
3155 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3156 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003157 return;
3158 }
3159
Olli Etuaho09b04a22016-12-15 13:30:26 +00003160 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003161
3162 if (layoutQualifier.matrixPacking != EmpUnspecified)
3163 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003164 if (typeQualifier.qualifier == EvqUniform)
3165 {
3166 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3167 }
3168 else if (typeQualifier.qualifier == EvqBuffer)
3169 {
3170 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3171 }
Martin Radev802abe02016-08-04 17:48:32 +03003172 }
3173
3174 if (layoutQualifier.blockStorage != EbsUnspecified)
3175 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003176 if (typeQualifier.qualifier == EvqUniform)
3177 {
3178 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3179 }
3180 else if (typeQualifier.qualifier == EvqBuffer)
3181 {
3182 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3183 }
Martin Radev802abe02016-08-04 17:48:32 +03003184 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003185 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003186}
3187
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3189 const TFunction &function,
3190 const TSourceLoc &location,
3191 bool insertParametersToSymbolTable)
3192{
Olli Etuahobed35d72017-12-20 16:36:26 +02003193 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003194
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003195 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 prototype->setLine(location);
3197
3198 for (size_t i = 0; i < function.getParamCount(); i++)
3199 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003200 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003201
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3203 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003204 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003207 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003208 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003209 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003210 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003211 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003212 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003213 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003214 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003215 }
3216 else
3217 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003218 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003219 {
3220 error(location, "function parameter array must be sized at compile time", "[]");
3221 // We don't need to size the arrays since the parameter is unnamed and hence
3222 // inaccessible.
3223 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003224 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003225 }
3226 return prototype;
3227}
3228
Olli Etuaho16c745a2017-01-16 17:02:27 +00003229TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3230 const TFunction &parsedFunction,
3231 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232{
Olli Etuaho476197f2016-10-11 13:59:08 +01003233 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3234 // first declaration. Either way the instance in the symbol table is used to track whether the
3235 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003236 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003237 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003238 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3239
3240 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003241 {
3242 // ESSL 1.00.17 section 4.2.7.
3243 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3244 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003245 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003246
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003247 TIntermFunctionPrototype *prototype =
3248 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003249
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003250 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003251
3252 if (!symbolTable.atGlobalLevel())
3253 {
3254 // ESSL 3.00.4 section 4.2.4.
3255 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003256 }
3257
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003258 return prototype;
3259}
3260
Olli Etuaho336b1472016-10-05 16:37:55 +01003261TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003262 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003263 TIntermBlock *functionBody,
3264 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003266 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3268 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003269 error(location,
3270 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003271 }
3272
Olli Etuahof51fdd22016-10-03 10:03:40 +01003273 if (functionBody == nullptr)
3274 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003275 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003276 functionBody->setLine(location);
3277 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003278 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003279 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003280 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003281
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003282 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003283 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003284}
3285
Olli Etuaho476197f2016-10-11 13:59:08 +01003286void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003287 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003288 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003289{
Olli Etuaho476197f2016-10-11 13:59:08 +01003290 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003291
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003292 bool wasDefined = false;
3293 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3294 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003295 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003296 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003297 }
Jamie Madill185fb402015-06-12 15:48:48 -04003298
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003299 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003300 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003301 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003302
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003303 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003304 setLoopNestingLevel(0);
3305}
3306
Jamie Madillb98c3a82015-07-23 14:26:04 -04003307TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003308{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003309 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003310 // We don't know at this point whether this is a function definition or a prototype.
3311 // The definition production code will check for redefinitions.
3312 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003313 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303314
Olli Etuahod80f2942017-11-06 12:44:45 +02003315 for (size_t i = 0u; i < function->getParamCount(); ++i)
3316 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003317 const TVariable *param = function->getParam(i);
3318 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003319 {
3320 // ESSL 3.00.6 section 12.10.
3321 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003322 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003323 }
3324 }
3325
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003326 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303327 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003328 const UnmangledBuiltIn *builtIn =
3329 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3330 if (builtIn &&
3331 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3332 {
3333 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3334 // functions. Therefore overloading or redefining builtin functions is an error.
3335 error(location, "Name of a built-in function cannot be redeclared as function",
3336 function->name());
3337 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303338 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003339 else
3340 {
3341 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3342 // this applies to redeclarations as well.
3343 const TSymbol *builtIn =
3344 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3345 if (builtIn)
3346 {
3347 error(location, "built-in functions cannot be redefined", function->name());
3348 }
3349 }
3350
3351 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3352 // here.
3353 const TFunction *prevDec =
3354 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3355 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003356 {
3357 if (prevDec->getReturnType() != function->getReturnType())
3358 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003359 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003360 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003361 }
3362 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3363 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003364 if (prevDec->getParam(i)->getType().getQualifier() !=
3365 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003366 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003367 error(location,
3368 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003369 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 }
3371 }
3372 }
3373
Jamie Madill185fb402015-06-12 15:48:48 -04003374 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003375 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3376 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003377 if (prevSym)
3378 {
3379 if (!prevSym->isFunction())
3380 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003381 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003382 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003383 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003384 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003385 // Parsing is at the inner scope level of the function's arguments and body statement at this
3386 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3387 // scope.
3388 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003389
Olli Etuaho78d13742017-01-18 13:06:10 +00003390 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003391 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003392 {
3393 if (function->getParamCount() > 0)
3394 {
3395 error(location, "function cannot take any parameter(s)", "main");
3396 }
3397 if (function->getReturnType().getBasicType() != EbtVoid)
3398 {
3399 error(location, "main function cannot return a value",
3400 function->getReturnType().getBasicString());
3401 }
3402 }
3403
Jamie Madill185fb402015-06-12 15:48:48 -04003404 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003405 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3406 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003407 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3408 //
3409 return function;
3410}
3411
Olli Etuaho9de84a52016-06-14 17:36:01 +03003412TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003413 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003414 const TSourceLoc &location)
3415{
3416 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3417 {
3418 error(location, "no qualifiers allowed for function return",
3419 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003420 }
3421 if (!type.layoutQualifier.isEmpty())
3422 {
3423 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003424 }
jchen10cc2a10e2017-05-03 14:05:12 +08003425 // make sure an opaque type is not involved as well...
3426 std::string reason(getBasicString(type.getBasicType()));
3427 reason += "s can't be function return values";
3428 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003429 if (mShaderVersion < 300)
3430 {
3431 // Array return values are forbidden, but there's also no valid syntax for declaring array
3432 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003433 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003434
3435 if (type.isStructureContainingArrays())
3436 {
3437 // ESSL 1.00.17 section 6.1 Function Definitions
Olli Etuaho72e35892018-06-20 11:43:08 +03003438 TInfoSinkBase typeString;
3439 typeString << TType(type);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003440 error(location, "structures containing arrays can't be function return values",
Olli Etuaho72e35892018-06-20 11:43:08 +03003441 typeString.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003442 }
3443 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003444
3445 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003446 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003447}
3448
Olli Etuaho697bf652018-02-16 11:50:54 +02003449TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3450 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003451{
Olli Etuaho697bf652018-02-16 11:50:54 +02003452 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003453}
3454
Olli Etuaho95ed1942018-02-01 14:01:19 +02003455TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003456{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003457 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003458 {
3459 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3460 "[]");
3461 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003462 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003463 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003464 error(publicType.getLine(), "constructor can't be a structure definition",
3465 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003466 }
3467
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003468 TType *type = new TType(publicType);
3469 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003470 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003471 error(publicType.getLine(), "cannot construct this type",
3472 getBasicString(publicType.getBasicType()));
3473 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003474 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003475 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003476}
3477
Olli Etuaho55bde912017-10-25 13:41:13 +03003478void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3479 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003480 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003481 TType *arrayType)
3482{
3483 if (arrayType->isUnsizedArray())
3484 {
3485 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003486 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003487 }
3488}
3489
3490TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003491 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003492 const TSourceLoc &nameLoc)
3493{
Olli Etuaho55bde912017-10-25 13:41:13 +03003494 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003495 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003496 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003497 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003498 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003499 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003500 checkIsNotReserved(nameLoc, name);
3501 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003502 return param;
3503}
3504
Olli Etuaho55bde912017-10-25 13:41:13 +03003505TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003506 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003507 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003508{
Olli Etuaho55bde912017-10-25 13:41:13 +03003509 TType *type = new TType(publicType);
3510 return parseParameterDeclarator(type, name, nameLoc);
3511}
3512
Olli Etuahofbb1c792018-01-19 16:26:59 +02003513TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003514 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003515 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003516 const TSourceLoc &arrayLoc,
3517 TPublicType *elementType)
3518{
3519 checkArrayElementIsNotArray(arrayLoc, *elementType);
3520 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003521 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003522 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003523}
3524
Olli Etuaho95ed1942018-02-01 14:01:19 +02003525bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3526 const TIntermSequence &arguments,
3527 TType type,
3528 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003529{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003530 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003531 {
3532 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3533 return false;
3534 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003535 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003536 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003537 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003539 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3540 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003541 {
3542 error(line, "constructing from a non-dereferenced array", "constructor");
3543 return false;
3544 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003545 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003546 {
3547 if (dimensionalityFromElement == 1u)
3548 {
3549 error(line, "implicitly sized array of arrays constructor argument is not an array",
3550 "constructor");
3551 }
3552 else
3553 {
3554 error(line,
3555 "implicitly sized array of arrays constructor argument dimensionality is too "
3556 "low",
3557 "constructor");
3558 }
3559 return false;
3560 }
3561 }
3562 return true;
3563}
3564
Jamie Madillb98c3a82015-07-23 14:26:04 -04003565// This function is used to test for the correctness of the parameters passed to various constructor
3566// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003567//
Olli Etuaho856c4972016-08-08 11:38:39 +03003568// 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 +00003569//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003570TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003571{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003572 TType type = fnCall->constructorType();
3573 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003574 if (type.isUnsizedArray())
3575 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003576 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003577 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003578 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003579 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003580 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003581 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003582 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003583 if (type.getOutermostArraySize() == 0u)
3584 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003585 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003586 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003587 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003588 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003589 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003590 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003591 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 }
3593 }
3594 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003596
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003597 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003598 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003599 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003601
Olli Etuaho95ed1942018-02-01 14:01:19 +02003602 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003603 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003604
Olli Etuaho765924f2018-01-04 12:48:36 +02003605 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003606}
3607
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608//
3609// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003610// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003611//
Olli Etuaho13389b62016-10-16 11:48:18 +01003612TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003613 const TTypeQualifierBuilder &typeQualifierBuilder,
3614 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003615 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003616 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003617 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003618 const TSourceLoc &instanceLine,
3619 TIntermTyped *arrayIndex,
3620 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003621{
Olli Etuaho856c4972016-08-08 11:38:39 +03003622 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003623
Olli Etuaho77ba4082016-12-16 12:01:18 +00003624 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003625
Jiajia Qinbc585152017-06-23 15:42:17 +08003626 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003627 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003628 error(typeQualifier.line,
3629 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3630 "3.10",
3631 getQualifierString(typeQualifier.qualifier));
3632 }
3633 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3634 {
3635 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003636 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003637 }
3638
Martin Radev70866b82016-07-22 15:27:42 +03003639 if (typeQualifier.invariant)
3640 {
3641 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3642 }
3643
Jiajia Qinbc585152017-06-23 15:42:17 +08003644 if (typeQualifier.qualifier != EvqBuffer)
3645 {
3646 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3647 }
Olli Etuaho43364892017-02-13 16:00:12 +00003648
jchen10af713a22017-04-19 09:10:56 +08003649 // add array index
3650 unsigned int arraySize = 0;
3651 if (arrayIndex != nullptr)
3652 {
3653 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3654 }
3655
Olli Etuahoa78092c2018-09-26 14:16:13 +03003656 checkIndexIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.index);
3657
jchen10af713a22017-04-19 09:10:56 +08003658 if (mShaderVersion < 310)
3659 {
3660 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3661 }
3662 else
3663 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003664 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3665 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003666 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003667
Andrei Volykhina5527072017-03-22 16:46:30 +03003668 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3669
Jamie Madill099c0f32013-06-20 11:55:52 -04003670 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003671 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003672 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3673 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003674
Jamie Madill099c0f32013-06-20 11:55:52 -04003675 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3676 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003677 if (typeQualifier.qualifier == EvqUniform)
3678 {
3679 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3680 }
3681 else if (typeQualifier.qualifier == EvqBuffer)
3682 {
3683 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3684 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003685 }
3686
Jamie Madill1566ef72013-06-20 11:55:54 -04003687 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3688 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003689 if (typeQualifier.qualifier == EvqUniform)
3690 {
3691 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3692 }
3693 else if (typeQualifier.qualifier == EvqBuffer)
3694 {
3695 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3696 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003697 }
3698
Olli Etuaho856c4972016-08-08 11:38:39 +03003699 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003700
Martin Radev2cc85b32016-08-05 16:22:53 +03003701 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3702
Jamie Madill98493dd2013-07-08 14:39:03 -04003703 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303704 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3705 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003706 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303707 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003708 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303709 {
jchen10cc2a10e2017-05-03 14:05:12 +08003710 std::string reason("unsupported type - ");
3711 reason += fieldType->getBasicString();
3712 reason += " types are not allowed in interface blocks";
3713 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003714 }
3715
Jamie Madill98493dd2013-07-08 14:39:03 -04003716 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003717 switch (qualifier)
3718 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003719 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003720 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003721 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003722 if (typeQualifier.qualifier == EvqBuffer)
3723 {
3724 error(field->line(), "invalid qualifier on shader storage block member",
3725 getQualifierString(qualifier));
3726 }
3727 break;
3728 case EvqBuffer:
3729 if (typeQualifier.qualifier == EvqUniform)
3730 {
3731 error(field->line(), "invalid qualifier on uniform block member",
3732 getQualifierString(qualifier));
3733 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003734 break;
3735 default:
3736 error(field->line(), "invalid qualifier on interface block member",
3737 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003738 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003739 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003740
Martin Radev70866b82016-07-22 15:27:42 +03003741 if (fieldType->isInvariant())
3742 {
3743 error(field->line(), "invalid qualifier on interface block member", "invariant");
3744 }
3745
Jamie Madilla5efff92013-06-06 11:56:47 -04003746 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003747 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003748 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Olli Etuahoa78092c2018-09-26 14:16:13 +03003749 checkIndexIsNotSpecified(field->line(), fieldLayoutQualifier.index);
jchen10af713a22017-04-19 09:10:56 +08003750 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003751
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003753 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003754 error(field->line(), "invalid layout qualifier: cannot be used here",
3755 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003756 }
3757
Jamie Madill98493dd2013-07-08 14:39:03 -04003758 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003759 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003760 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003761 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003762 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003763 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003764 warning(field->line(),
3765 "extraneous layout qualifier: only has an effect on matrix types",
3766 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003767 }
3768
Jamie Madill98493dd2013-07-08 14:39:03 -04003769 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003770
Olli Etuahoebee5b32017-11-23 12:56:32 +02003771 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3772 typeQualifier.qualifier != EvqBuffer)
3773 {
3774 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3775 checkIsNotUnsizedArray(field->line(),
3776 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003777 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003778 }
3779
Jiajia Qinbc585152017-06-23 15:42:17 +08003780 if (typeQualifier.qualifier == EvqBuffer)
3781 {
3782 // set memory qualifiers
3783 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3784 // qualified with a memory qualifier, it is as if all of its members were declared with
3785 // the same memory qualifier.
3786 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3787 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3788 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3789 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3790 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3791 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3792 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3793 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3794 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3795 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3796 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003797 }
3798
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003799 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003800 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003801 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003802 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003803 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003804 }
3805
Olli Etuahob60d30f2018-01-16 12:31:06 +02003806 TType *interfaceBlockType =
3807 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003808 if (arrayIndex != nullptr)
3809 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003810 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003811 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003812
Olli Etuaho195be942017-12-04 23:40:14 +02003813 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003814 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3815 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003816 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003817 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003818 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003819
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003820 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003821 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003822 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003823 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003825 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003826 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003827
3828 // set parent pointer of the field variable
3829 fieldType->setInterfaceBlock(interfaceBlock);
3830
Olli Etuahob60d30f2018-01-16 12:31:06 +02003831 fieldType->setQualifier(typeQualifier.qualifier);
3832
Olli Etuaho195be942017-12-04 23:40:14 +02003833 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003834 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003835 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303836 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003837 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003838 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003839 }
3840 }
3841 }
3842 else
3843 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003844 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003845
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003846 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003847 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303848 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003849 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003850 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003851 }
3852
Olli Etuaho195be942017-12-04 23:40:14 +02003853 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3854 blockSymbol->setLine(typeQualifier.line);
3855 TIntermDeclaration *declaration = new TIntermDeclaration();
3856 declaration->appendDeclarator(blockSymbol);
3857 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003858
3859 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003860 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003861}
3862
Olli Etuahofbb1c792018-01-19 16:26:59 +02003863void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3864 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003865{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003866 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003867
3868 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003869 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303870 if (mStructNestingLevel > 1)
3871 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003872 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003873 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003874}
3875
3876void TParseContext::exitStructDeclaration()
3877{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003878 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879}
3880
Olli Etuaho8a176262016-08-16 14:23:01 +03003881void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882{
Jamie Madillacb4b812016-11-07 13:50:29 -05003883 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303884 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003885 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003886 }
3887
Arun Patole7e7e68d2015-05-22 12:02:25 +05303888 if (field.type()->getBasicType() != EbtStruct)
3889 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003890 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003891 }
3892
3893 // We're already inside a structure definition at this point, so add
3894 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303895 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3896 {
Jamie Madill41a49272014-03-18 16:10:13 -04003897 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003898 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3899 {
3900 // This may happen in case there are nested struct definitions. While they are also
3901 // invalid GLSL, they don't cause a syntax error.
3902 reasonStream << "Struct nesting";
3903 }
3904 else
3905 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003906 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003907 }
3908 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003909 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003910 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003911 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003912 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003913}
3914
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003915//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003916// Parse an array index expression
3917//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003918TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3919 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303920 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003921{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003922 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3923 {
3924 if (baseExpression->getAsSymbolNode())
3925 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303926 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003927 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928 }
3929 else
3930 {
3931 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3932 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003933
Olli Etuaho3ec75682017-07-05 17:02:55 +03003934 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003936
Jiawei Shaod8105a02017-08-08 09:54:36 +08003937 if (baseExpression->getQualifier() == EvqPerVertexIn)
3938 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003939 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003940 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3941 {
3942 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3943 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3944 }
3945 }
3946
Jamie Madill21c1e452014-12-29 11:33:41 -05003947 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3948
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03003949 // ANGLE should be able to fold any constant expressions resulting in an integer - but to be
3950 // safe we don't treat "EvqConst" that's evaluated according to the spec as being sufficient
3951 // for constness. Some interpretations of the spec have allowed constant expressions with side
3952 // effects - like array length() method on a non-constant array.
Olli Etuaho36b05142015-11-12 13:10:42 +02003953 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3954 {
3955 if (baseExpression->isInterfaceBlock())
3956 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003957 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003958 switch (baseExpression->getQualifier())
3959 {
3960 case EvqPerVertexIn:
3961 break;
3962 case EvqUniform:
3963 case EvqBuffer:
3964 error(location,
3965 "array indexes for uniform block arrays and shader storage block arrays "
3966 "must be constant integral expressions",
3967 "[");
3968 break;
3969 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003970 // We can reach here only in error cases.
3971 ASSERT(mDiagnostics->numErrors() > 0);
3972 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003973 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003974 }
3975 else if (baseExpression->getQualifier() == EvqFragmentOut)
3976 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003977 error(location,
3978 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003979 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003980 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3981 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003982 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003983 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003984 }
3985
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003986 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003987 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003988 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3989 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3990 // constant fold expressions that are not constant expressions). The most compatible way to
3991 // handle this case is to report a warning instead of an error and force the index to be in
3992 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003993 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003994 int index = 0;
3995 if (indexConstantUnion->getBasicType() == EbtInt)
3996 {
3997 index = indexConstantUnion->getIConst(0);
3998 }
3999 else if (indexConstantUnion->getBasicType() == EbtUInt)
4000 {
4001 index = static_cast<int>(indexConstantUnion->getUConst(0));
4002 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004003
4004 int safeIndex = -1;
4005
Olli Etuahoebee5b32017-11-23 12:56:32 +02004006 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004007 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004008 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4009 safeIndex = 0;
4010 }
4011
4012 if (!baseExpression->getType().isUnsizedArray())
4013 {
4014 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004015 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004016 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004017 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004018 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4019 {
4020 outOfRangeError(outOfRangeIndexIsError, location,
4021 "array index for gl_FragData must be zero when "
4022 "GL_EXT_draw_buffers is disabled",
4023 "[]");
4024 safeIndex = 0;
4025 }
4026 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004027 }
4028 // Only do generic out-of-range check if similar error hasn't already been reported.
4029 if (safeIndex < 0)
4030 {
4031 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004032 {
4033 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4034 baseExpression->getOutermostArraySize(),
4035 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004036 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004037 else if (baseExpression->isMatrix())
4038 {
4039 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4040 baseExpression->getType().getCols(),
4041 "matrix field selection out of range");
4042 }
4043 else
4044 {
4045 ASSERT(baseExpression->isVector());
4046 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4047 baseExpression->getType().getNominalSize(),
4048 "vector field selection out of range");
4049 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004050 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004051
Olli Etuahoebee5b32017-11-23 12:56:32 +02004052 ASSERT(safeIndex >= 0);
4053 // Data of constant unions can't be changed, because it may be shared with other
4054 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4055 // sanitized object.
4056 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4057 {
4058 TConstantUnion *safeConstantUnion = new TConstantUnion();
4059 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004060 indexExpression = new TIntermConstantUnion(
4061 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4062 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004064
Olli Etuahoebee5b32017-11-23 12:56:32 +02004065 TIntermBinary *node =
4066 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4067 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004068 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004069 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004070 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071
Olli Etuaho94bbed12018-03-20 14:44:53 +02004072 markStaticReadIfSymbol(indexExpression);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4074 node->setLine(location);
4075 // Indirect indexing can never be constant folded.
4076 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004077}
4078
Olli Etuahoebee5b32017-11-23 12:56:32 +02004079int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4080 const TSourceLoc &location,
4081 int index,
4082 int arraySize,
4083 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004084{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004085 // Should not reach here with an unsized / runtime-sized array.
4086 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004087 // A negative index should already have been checked.
4088 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004089 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004090 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004091 std::stringstream reasonStream;
4092 reasonStream << reason << " '" << index << "'";
4093 std::string token = reasonStream.str();
4094 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004095 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004096 }
4097 return index;
4098}
4099
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4101 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004102 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004103 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105 if (baseExpression->isArray())
4106 {
4107 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004108 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004109 }
4110
4111 if (baseExpression->isVector())
4112 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004113 TVector<int> fieldOffsets;
4114 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4115 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004117 fieldOffsets.resize(1);
4118 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004120 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4121 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122
Olli Etuaho765924f2018-01-04 12:48:36 +02004123 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004125 else if (baseExpression->getBasicType() == EbtStruct)
4126 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304127 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004128 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 {
4130 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004131 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004132 }
4133 else
4134 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004135 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004137 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004138 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004139 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004140 {
4141 fieldFound = true;
4142 break;
4143 }
4144 }
4145 if (fieldFound)
4146 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004147 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004148 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004149 TIntermBinary *node =
4150 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4151 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004152 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153 }
4154 else
4155 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004156 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004157 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158 }
4159 }
4160 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004161 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304163 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004164 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004165 {
4166 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004167 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004168 }
4169 else
4170 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004171 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004172 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004173 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004174 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004175 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004176 {
4177 fieldFound = true;
4178 break;
4179 }
4180 }
4181 if (fieldFound)
4182 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004183 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004184 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004185 TIntermBinary *node =
4186 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4187 node->setLine(dotLocation);
4188 // Indexing interface blocks can never be constant folded.
4189 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004190 }
4191 else
4192 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004193 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004194 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 }
4196 }
4197 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004198 else
4199 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004200 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004201 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004202 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004203 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004204 }
4205 else
4206 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304207 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004208 " field selection requires structure, vector, or interface block on left hand "
4209 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004210 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004211 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004212 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004213 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004214}
4215
Olli Etuahofbb1c792018-01-19 16:26:59 +02004216TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004217 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004218{
Jamie Madill2f294c92017-11-20 14:47:26 -05004219 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004220
4221 if (qualifierType == "shared")
4222 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004223 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004224 {
4225 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4226 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004227 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004228 }
4229 else if (qualifierType == "packed")
4230 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004231 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004232 {
4233 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4234 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004235 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004236 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004237 else if (qualifierType == "std430")
4238 {
4239 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4240 qualifier.blockStorage = EbsStd430;
4241 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004242 else if (qualifierType == "std140")
4243 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004244 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004245 }
4246 else if (qualifierType == "row_major")
4247 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004248 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004249 }
4250 else if (qualifierType == "column_major")
4251 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004252 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004253 }
4254 else if (qualifierType == "location")
4255 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004256 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004257 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004258 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004259 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004260 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004261 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4262 {
4263 qualifier.yuv = true;
4264 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004265 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004266 else if (qualifierType == "rgba32f")
4267 {
4268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4269 qualifier.imageInternalFormat = EiifRGBA32F;
4270 }
4271 else if (qualifierType == "rgba16f")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifRGBA16F;
4275 }
4276 else if (qualifierType == "r32f")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifR32F;
4280 }
4281 else if (qualifierType == "rgba8")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifRGBA8;
4285 }
4286 else if (qualifierType == "rgba8_snorm")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4290 }
4291 else if (qualifierType == "rgba32i")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA32I;
4295 }
4296 else if (qualifierType == "rgba16i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA16I;
4300 }
4301 else if (qualifierType == "rgba8i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifRGBA8I;
4305 }
4306 else if (qualifierType == "r32i")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifR32I;
4310 }
4311 else if (qualifierType == "rgba32ui")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifRGBA32UI;
4315 }
4316 else if (qualifierType == "rgba16ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifRGBA16UI;
4320 }
4321 else if (qualifierType == "rgba8ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifRGBA8UI;
4325 }
4326 else if (qualifierType == "r32ui")
4327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.imageInternalFormat = EiifR32UI;
4330 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004331 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4332 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004333 {
4334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4335 qualifier.primitiveType = EptPoints;
4336 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004337 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4338 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.primitiveType = EptLines;
4342 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004343 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4344 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.primitiveType = EptLinesAdjacency;
4348 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004349 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4350 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004351 {
4352 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4353 qualifier.primitiveType = EptTriangles;
4354 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004355 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4356 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004357 {
4358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4359 qualifier.primitiveType = EptTrianglesAdjacency;
4360 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004361 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4362 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004363 {
4364 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4365 qualifier.primitiveType = EptLineStrip;
4366 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004367 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4368 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004369 {
4370 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4371 qualifier.primitiveType = EptTriangleStrip;
4372 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004373
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004374 else
4375 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004376 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004377 }
4378
Jamie Madilla5efff92013-06-06 11:56:47 -04004379 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004380}
4381
Olli Etuahofbb1c792018-01-19 16:26:59 +02004382void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004383 const TSourceLoc &qualifierTypeLine,
4384 int intValue,
4385 const TSourceLoc &intValueLine,
4386 const std::string &intValueString,
4387 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004388 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004389{
Olli Etuaho856c4972016-08-08 11:38:39 +03004390 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004391 if (intValue < 1)
4392 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004393 std::stringstream reasonStream;
4394 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4395 std::string reason = reasonStream.str();
4396 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004397 }
4398 (*localSize)[index] = intValue;
4399}
4400
Olli Etuaho09b04a22016-12-15 13:30:26 +00004401void TParseContext::parseNumViews(int intValue,
4402 const TSourceLoc &intValueLine,
4403 const std::string &intValueString,
4404 int *numViews)
4405{
4406 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4407 // specification.
4408 if (intValue < 1)
4409 {
4410 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4411 }
4412 *numViews = intValue;
4413}
4414
Shaob5cc1192017-07-06 10:47:20 +08004415void TParseContext::parseInvocations(int intValue,
4416 const TSourceLoc &intValueLine,
4417 const std::string &intValueString,
4418 int *numInvocations)
4419{
4420 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4421 // it doesn't make sense to accept invocations <= 0.
4422 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4423 {
4424 error(intValueLine,
4425 "out of range: invocations must be in the range of [1, "
4426 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4427 intValueString.c_str());
4428 }
4429 else
4430 {
4431 *numInvocations = intValue;
4432 }
4433}
4434
4435void TParseContext::parseMaxVertices(int intValue,
4436 const TSourceLoc &intValueLine,
4437 const std::string &intValueString,
4438 int *maxVertices)
4439{
4440 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4441 // it doesn't make sense to accept max_vertices < 0.
4442 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4443 {
4444 error(
4445 intValueLine,
4446 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4447 intValueString.c_str());
4448 }
4449 else
4450 {
4451 *maxVertices = intValue;
4452 }
4453}
4454
Olli Etuahoa78092c2018-09-26 14:16:13 +03004455void TParseContext::parseIndexLayoutQualifier(int intValue,
4456 const TSourceLoc &intValueLine,
4457 const std::string &intValueString,
4458 int *index)
4459{
4460 // EXT_blend_func_extended specifies that most validation should happen at link time, but since
4461 // we're validating output variable locations at compile time, it makes sense to validate that
4462 // index is 0 or 1 also at compile time. Also since we use "-1" as a placeholder for unspecified
4463 // index, we can't accept it here.
4464 if (intValue < 0 || intValue > 1)
4465 {
4466 error(intValueLine, "out of range: index layout qualifier can only be 0 or 1",
4467 intValueString.c_str());
4468 }
4469 else
4470 {
4471 *index = intValue;
4472 }
4473}
4474
Olli Etuahofbb1c792018-01-19 16:26:59 +02004475TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004476 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004477 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304478 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004479{
Jamie Madill2f294c92017-11-20 14:47:26 -05004480 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004481
Martin Radev802abe02016-08-04 17:48:32 +03004482 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004483
Martin Radev802abe02016-08-04 17:48:32 +03004484 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004485 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004486 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004487 if (intValue < 0)
4488 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004489 error(intValueLine, "out of range: location must be non-negative",
4490 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004491 }
4492 else
4493 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004494 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004495 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004496 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004497 }
Olli Etuaho43364892017-02-13 16:00:12 +00004498 else if (qualifierType == "binding")
4499 {
4500 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4501 if (intValue < 0)
4502 {
4503 error(intValueLine, "out of range: binding must be non-negative",
4504 intValueString.c_str());
4505 }
4506 else
4507 {
4508 qualifier.binding = intValue;
4509 }
4510 }
jchen104cdac9e2017-05-08 11:01:20 +08004511 else if (qualifierType == "offset")
4512 {
4513 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4514 if (intValue < 0)
4515 {
4516 error(intValueLine, "out of range: offset must be non-negative",
4517 intValueString.c_str());
4518 }
4519 else
4520 {
4521 qualifier.offset = intValue;
4522 }
4523 }
Martin Radev802abe02016-08-04 17:48:32 +03004524 else if (qualifierType == "local_size_x")
4525 {
4526 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4527 &qualifier.localSize);
4528 }
4529 else if (qualifierType == "local_size_y")
4530 {
4531 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4532 &qualifier.localSize);
4533 }
4534 else if (qualifierType == "local_size_z")
4535 {
4536 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4537 &qualifier.localSize);
4538 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004539 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004540 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004541 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4542 {
4543 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4544 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004545 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004546 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4547 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004548 {
4549 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4550 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004551 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4552 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004553 {
4554 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4555 }
Olli Etuahoa78092c2018-09-26 14:16:13 +03004556 else if (qualifierType == "index" && mShaderType == GL_FRAGMENT_SHADER &&
4557 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_blend_func_extended))
4558 {
4559 parseIndexLayoutQualifier(intValue, intValueLine, intValueString, &qualifier.index);
4560 }
Martin Radev802abe02016-08-04 17:48:32 +03004561 else
4562 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004563 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004564 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004565
Jamie Madilla5efff92013-06-06 11:56:47 -04004566 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004567}
4568
Olli Etuaho613b9592016-09-05 12:05:53 +03004569TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4570{
4571 return new TTypeQualifierBuilder(
4572 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4573 mShaderVersion);
4574}
4575
Olli Etuahocce89652017-06-19 16:04:09 +03004576TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4577 const TSourceLoc &loc)
4578{
4579 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4580 return new TStorageQualifierWrapper(qualifier, loc);
4581}
4582
4583TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4584{
4585 if (getShaderType() == GL_VERTEX_SHADER)
4586 {
4587 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4588 }
4589 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4590}
4591
4592TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4593{
4594 if (declaringFunction())
4595 {
4596 return new TStorageQualifierWrapper(EvqIn, loc);
4597 }
Shaob5cc1192017-07-06 10:47:20 +08004598
4599 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004600 {
Shaob5cc1192017-07-06 10:47:20 +08004601 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004602 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004603 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004604 {
4605 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4606 }
4607 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004608 }
Shaob5cc1192017-07-06 10:47:20 +08004609 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004610 {
Shaob5cc1192017-07-06 10:47:20 +08004611 if (mShaderVersion < 300)
4612 {
4613 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4614 }
4615 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004616 }
Shaob5cc1192017-07-06 10:47:20 +08004617 case GL_COMPUTE_SHADER:
4618 {
4619 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4620 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004621 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004622 {
4623 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4624 }
4625 default:
4626 {
4627 UNREACHABLE();
4628 return new TStorageQualifierWrapper(EvqLast, loc);
4629 }
Olli Etuahocce89652017-06-19 16:04:09 +03004630 }
Olli Etuahocce89652017-06-19 16:04:09 +03004631}
4632
4633TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4634{
4635 if (declaringFunction())
4636 {
4637 return new TStorageQualifierWrapper(EvqOut, loc);
4638 }
Shaob5cc1192017-07-06 10:47:20 +08004639 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004640 {
Shaob5cc1192017-07-06 10:47:20 +08004641 case GL_VERTEX_SHADER:
4642 {
4643 if (mShaderVersion < 300)
4644 {
4645 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4646 }
4647 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4648 }
4649 case GL_FRAGMENT_SHADER:
4650 {
4651 if (mShaderVersion < 300)
4652 {
4653 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4654 }
4655 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4656 }
4657 case GL_COMPUTE_SHADER:
4658 {
4659 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4660 return new TStorageQualifierWrapper(EvqLast, loc);
4661 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004662 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004663 {
4664 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4665 }
4666 default:
4667 {
4668 UNREACHABLE();
4669 return new TStorageQualifierWrapper(EvqLast, loc);
4670 }
Olli Etuahocce89652017-06-19 16:04:09 +03004671 }
Olli Etuahocce89652017-06-19 16:04:09 +03004672}
4673
4674TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4675{
4676 if (!declaringFunction())
4677 {
4678 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4679 }
4680 return new TStorageQualifierWrapper(EvqInOut, loc);
4681}
4682
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004684 TLayoutQualifier rightQualifier,
4685 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004686{
Martin Radevc28888b2016-07-22 15:27:42 +03004687 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004688 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004689}
4690
Olli Etuahofbb1c792018-01-19 16:26:59 +02004691TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4692 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004693{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004694 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004695 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004696}
4697
Olli Etuahofbb1c792018-01-19 16:26:59 +02004698TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004699 const TSourceLoc &loc,
4700 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004701{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004702 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004703 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004704}
4705
Olli Etuaho722bfb52017-10-26 17:00:11 +03004706void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4707 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004708 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004709 const TSourceLoc &location)
4710{
4711 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4712 {
4713 if ((*fieldIter)->name() == name)
4714 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004715 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004716 }
4717 }
4718}
4719
4720TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4721{
4722 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4723 ++fieldIter)
4724 {
4725 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4726 location);
4727 }
4728 return fields;
4729}
4730
Olli Etuaho4de340a2016-12-16 09:32:03 +00004731TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4732 const TFieldList *newlyAddedFields,
4733 const TSourceLoc &location)
4734{
4735 for (TField *field : *newlyAddedFields)
4736 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004737 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4738 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004739 processedFields->push_back(field);
4740 }
4741 return processedFields;
4742}
4743
Martin Radev70866b82016-07-22 15:27:42 +03004744TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4745 const TTypeQualifierBuilder &typeQualifierBuilder,
4746 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004747 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004748{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004749 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004750
Martin Radev70866b82016-07-22 15:27:42 +03004751 typeSpecifier->qualifier = typeQualifier.qualifier;
4752 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004753 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004754 typeSpecifier->invariant = typeQualifier.invariant;
4755 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304756 {
Martin Radev70866b82016-07-22 15:27:42 +03004757 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004758 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004760}
4761
Jamie Madillb98c3a82015-07-23 14:26:04 -04004762TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004763 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004764{
Martin Radev4a9cd802016-09-01 16:51:51 +03004765 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4766 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004767
Olli Etuahofbb1c792018-01-19 16:26:59 +02004768 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004769 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004770
Martin Radev4a9cd802016-09-01 16:51:51 +03004771 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004772
Olli Etuahod5f44c92017-11-29 17:15:40 +02004773 TFieldList *fieldList = new TFieldList();
4774
4775 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304776 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004777 TType *type = new TType(typeSpecifier);
4778 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304779 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004780 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004781 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004782 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004784
Jamie Madillf5557ac2018-06-15 09:46:58 -04004785 TField *field =
4786 new TField(type, declarator->name(), declarator->line(), SymbolType::UserDefined);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004787 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4788 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 }
4790
Olli Etuahod5f44c92017-11-29 17:15:40 +02004791 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004792}
4793
Martin Radev4a9cd802016-09-01 16:51:51 +03004794TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4795 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004796 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004797 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004798{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004799 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004800 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004801 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004802 structSymbolType = SymbolType::Empty;
4803 }
4804 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004805
Jamie Madill9b820842015-02-12 10:40:10 -05004806 // Store a bool in the struct if we're at global scope, to allow us to
4807 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004808 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004809
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004810 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004811 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004812 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004813 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304814 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004815 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004816 }
4817 }
4818
4819 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004820 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004822 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004823 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824 switch (qualifier)
4825 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004826 case EvqGlobal:
4827 case EvqTemporary:
4828 break;
4829 default:
4830 error(field.line(), "invalid qualifier on struct member",
4831 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004833 }
Martin Radev70866b82016-07-22 15:27:42 +03004834 if (field.type()->isInvariant())
4835 {
4836 error(field.line(), "invalid qualifier on struct member", "invariant");
4837 }
jchen104cdac9e2017-05-08 11:01:20 +08004838 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4839 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004840 {
4841 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4842 }
4843
Olli Etuahoebee5b32017-11-23 12:56:32 +02004844 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004845 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004846
Olli Etuaho43364892017-02-13 16:00:12 +00004847 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4848
Olli Etuahoa78092c2018-09-26 14:16:13 +03004849 checkIndexIsNotSpecified(field.line(), field.type()->getLayoutQualifier().index);
4850
Olli Etuaho43364892017-02-13 16:00:12 +00004851 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004852
4853 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004854 }
4855
Martin Radev4a9cd802016-09-01 16:51:51 +03004856 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004857 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004858 exitStructDeclaration();
4859
Martin Radev4a9cd802016-09-01 16:51:51 +03004860 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004861}
4862
Jamie Madillb98c3a82015-07-23 14:26:04 -04004863TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004864 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004865 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004866{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004867 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004869 init->isVector())
4870 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4872 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004873 return nullptr;
4874 }
4875
Olli Etuaho923ecef2017-10-11 12:01:38 +03004876 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004877 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004878 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004879 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004880 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004881 }
4882
Olli Etuaho94bbed12018-03-20 14:44:53 +02004883 markStaticReadIfSymbol(init);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004884 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4885 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004886 return node;
4887}
4888
4889TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4890{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004891 if (mSwitchNestingLevel == 0)
4892 {
4893 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004894 return nullptr;
4895 }
4896 if (condition == nullptr)
4897 {
4898 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004899 return nullptr;
4900 }
4901 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004902 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004903 {
4904 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004905 }
4906 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho1dfd8ae2018-10-01 15:59:59 +03004907 // ANGLE should be able to fold any EvqConst expressions resulting in an integer - but to be
4908 // safe against corner cases we still check for conditionConst. Some interpretations of the
4909 // spec have allowed constant expressions with side effects - like array length() method on a
4910 // non-constant array.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004911 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004912 {
4913 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004914 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004915 TIntermCase *node = new TIntermCase(condition);
4916 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004917 return node;
4918}
4919
4920TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4921{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004922 if (mSwitchNestingLevel == 0)
4923 {
4924 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004925 return nullptr;
4926 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004927 TIntermCase *node = new TIntermCase(nullptr);
4928 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004929 return node;
4930}
4931
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4933 TIntermTyped *child,
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004934 const TSourceLoc &loc,
4935 const TFunction *func)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004936{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004937 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004938
4939 switch (op)
4940 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004941 case EOpLogicalNot:
4942 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4943 child->isVector())
4944 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004945 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004946 return nullptr;
4947 }
4948 break;
4949 case EOpBitwiseNot:
4950 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4951 child->isMatrix() || child->isArray())
4952 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004953 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004954 return nullptr;
4955 }
4956 break;
4957 case EOpPostIncrement:
4958 case EOpPreIncrement:
4959 case EOpPostDecrement:
4960 case EOpPreDecrement:
4961 case EOpNegative:
4962 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004963 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4964 child->getBasicType() == EbtBool || child->isArray() ||
4965 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004966 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004967 unaryOpError(loc, GetOperatorString(op), child->getType());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004968 return nullptr;
4969 }
Nico Weber41b072b2018-02-09 10:01:32 -05004970 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004971 // Operators for built-ins are already type checked against their prototype.
4972 default:
4973 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004974 }
4975
Jiajia Qinbc585152017-06-23 15:42:17 +08004976 if (child->getMemoryQualifier().writeonly)
4977 {
Olli Etuaho72e35892018-06-20 11:43:08 +03004978 unaryOpError(loc, GetOperatorString(op), child->getType());
Jiajia Qinbc585152017-06-23 15:42:17 +08004979 return nullptr;
4980 }
4981
Olli Etuaho94bbed12018-03-20 14:44:53 +02004982 markStaticReadIfSymbol(child);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004983 TIntermUnary *node = new TIntermUnary(op, child, func);
Olli Etuahof119a262016-08-19 15:54:22 +03004984 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004985
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004986 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004987}
4988
Olli Etuaho09b22472015-02-11 11:47:26 +02004989TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4990{
Olli Etuahocce89652017-06-19 16:04:09 +03004991 ASSERT(op != EOpNull);
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03004992 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004993 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004994 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004995 return child;
4996 }
4997 return node;
4998}
4999
Jamie Madillb98c3a82015-07-23 14:26:04 -04005000TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
5001 TIntermTyped *child,
5002 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005003{
Olli Etuaho856c4972016-08-08 11:38:39 +03005004 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02005005 return addUnaryMath(op, child, loc);
5006}
5007
Olli Etuaho765924f2018-01-04 12:48:36 +02005008TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
5009{
5010 // If we can, we should return the folded version of the expression for subsequent parsing. This
5011 // enables folding the containing expression during parsing as well, instead of the separate
5012 // FoldExpressions() step where folding nested expressions requires multiple full AST
5013 // traversals.
5014
5015 // Even if folding fails the fold() functions return some node representing the expression,
5016 // typically the original node. So "folded" can be assumed to be non-null.
5017 TIntermTyped *folded = expression->fold(mDiagnostics);
5018 ASSERT(folded != nullptr);
5019 if (folded->getQualifier() == expression->getQualifier())
5020 {
5021 // We need this expression to have the correct qualifier when validating the consuming
5022 // expression. So we can only return the folded node from here in case it has the same
5023 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
5024 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
5025 // 1. (true ? 1.0 : non_constant)
5026 // 2. (non_constant, 1.0)
5027 return folded;
5028 }
5029 return expression;
5030}
5031
Jamie Madillb98c3a82015-07-23 14:26:04 -04005032bool TParseContext::binaryOpCommonCheck(TOperator op,
5033 TIntermTyped *left,
5034 TIntermTyped *right,
5035 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005036{
jchen10b4cf5652017-05-05 18:51:17 +08005037 // Check opaque types are not allowed to be operands in expressions other than array indexing
5038 // and structure member selection.
5039 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5040 {
5041 switch (op)
5042 {
5043 case EOpIndexDirect:
5044 case EOpIndexIndirect:
5045 break;
jchen10b4cf5652017-05-05 18:51:17 +08005046
5047 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05005048 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08005049 error(loc, "Invalid operation for variables with an opaque type",
5050 GetOperatorString(op));
5051 return false;
5052 }
5053 }
jchen10cc2a10e2017-05-03 14:05:12 +08005054
Jiajia Qinbc585152017-06-23 15:42:17 +08005055 if (right->getMemoryQualifier().writeonly)
5056 {
5057 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5058 return false;
5059 }
5060
5061 if (left->getMemoryQualifier().writeonly)
5062 {
5063 switch (op)
5064 {
5065 case EOpAssign:
5066 case EOpInitialize:
5067 case EOpIndexDirect:
5068 case EOpIndexIndirect:
5069 case EOpIndexDirectStruct:
5070 case EOpIndexDirectInterfaceBlock:
5071 break;
5072 default:
5073 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5074 return false;
5075 }
5076 }
5077
Olli Etuaho244be012016-08-18 15:26:02 +03005078 if (left->getType().getStruct() || right->getType().getStruct())
5079 {
5080 switch (op)
5081 {
5082 case EOpIndexDirectStruct:
5083 ASSERT(left->getType().getStruct());
5084 break;
5085 case EOpEqual:
5086 case EOpNotEqual:
5087 case EOpAssign:
5088 case EOpInitialize:
5089 if (left->getType() != right->getType())
5090 {
5091 return false;
5092 }
5093 break;
5094 default:
5095 error(loc, "Invalid operation for structs", GetOperatorString(op));
5096 return false;
5097 }
5098 }
5099
Olli Etuaho94050052017-05-08 14:17:44 +03005100 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5101 {
5102 switch (op)
5103 {
5104 case EOpIndexDirectInterfaceBlock:
5105 ASSERT(left->getType().getInterfaceBlock());
5106 break;
5107 default:
5108 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5109 return false;
5110 }
5111 }
5112
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005113 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005114 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005115 error(loc, "array / non-array mismatch", GetOperatorString(op));
5116 return false;
5117 }
5118
5119 if (left->isArray())
5120 {
5121 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005122 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005123 {
5124 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5125 return false;
5126 }
5127
Olli Etuahoe79904c2015-03-18 16:56:42 +02005128 switch (op)
5129 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 case EOpEqual:
5131 case EOpNotEqual:
5132 case EOpAssign:
5133 case EOpInitialize:
5134 break;
5135 default:
5136 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5137 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005138 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005139 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005140 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005141 {
5142 error(loc, "array size mismatch", GetOperatorString(op));
5143 return false;
5144 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005145 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005146
5147 // Check ops which require integer / ivec parameters
5148 bool isBitShift = false;
5149 switch (op)
5150 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005151 case EOpBitShiftLeft:
5152 case EOpBitShiftRight:
5153 case EOpBitShiftLeftAssign:
5154 case EOpBitShiftRightAssign:
5155 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5156 // check that the basic type is an integer type.
5157 isBitShift = true;
5158 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5159 {
5160 return false;
5161 }
5162 break;
5163 case EOpBitwiseAnd:
5164 case EOpBitwiseXor:
5165 case EOpBitwiseOr:
5166 case EOpBitwiseAndAssign:
5167 case EOpBitwiseXorAssign:
5168 case EOpBitwiseOrAssign:
5169 // It is enough to check the type of only one operand, since later it
5170 // is checked that the operand types match.
5171 if (!IsInteger(left->getBasicType()))
5172 {
5173 return false;
5174 }
5175 break;
5176 default:
5177 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005178 }
5179
5180 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5181 // So the basic type should usually match.
5182 if (!isBitShift && left->getBasicType() != right->getBasicType())
5183 {
5184 return false;
5185 }
5186
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005187 // Check that:
5188 // 1. Type sizes match exactly on ops that require that.
5189 // 2. Restrictions for structs that contain arrays or samplers are respected.
5190 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005192 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005193 case EOpAssign:
5194 case EOpInitialize:
5195 case EOpEqual:
5196 case EOpNotEqual:
5197 // ESSL 1.00 sections 5.7, 5.8, 5.9
5198 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5199 {
5200 error(loc, "undefined operation for structs containing arrays",
5201 GetOperatorString(op));
5202 return false;
5203 }
5204 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5205 // we interpret the spec so that this extends to structs containing samplers,
5206 // similarly to ESSL 1.00 spec.
5207 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5208 left->getType().isStructureContainingSamplers())
5209 {
5210 error(loc, "undefined operation for structs containing samplers",
5211 GetOperatorString(op));
5212 return false;
5213 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005214
Olli Etuahoe1805592017-01-02 16:41:20 +00005215 if ((left->getNominalSize() != right->getNominalSize()) ||
5216 (left->getSecondarySize() != right->getSecondarySize()))
5217 {
5218 error(loc, "dimension mismatch", GetOperatorString(op));
5219 return false;
5220 }
5221 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005222 case EOpLessThan:
5223 case EOpGreaterThan:
5224 case EOpLessThanEqual:
5225 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005226 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005228 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005229 return false;
5230 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005231 break;
5232 case EOpAdd:
5233 case EOpSub:
5234 case EOpDiv:
5235 case EOpIMod:
5236 case EOpBitShiftLeft:
5237 case EOpBitShiftRight:
5238 case EOpBitwiseAnd:
5239 case EOpBitwiseXor:
5240 case EOpBitwiseOr:
5241 case EOpAddAssign:
5242 case EOpSubAssign:
5243 case EOpDivAssign:
5244 case EOpIModAssign:
5245 case EOpBitShiftLeftAssign:
5246 case EOpBitShiftRightAssign:
5247 case EOpBitwiseAndAssign:
5248 case EOpBitwiseXorAssign:
5249 case EOpBitwiseOrAssign:
5250 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5251 {
5252 return false;
5253 }
5254
5255 // Are the sizes compatible?
5256 if (left->getNominalSize() != right->getNominalSize() ||
5257 left->getSecondarySize() != right->getSecondarySize())
5258 {
5259 // If the nominal sizes of operands do not match:
5260 // One of them must be a scalar.
5261 if (!left->isScalar() && !right->isScalar())
5262 return false;
5263
5264 // In the case of compound assignment other than multiply-assign,
5265 // the right side needs to be a scalar. Otherwise a vector/matrix
5266 // would be assigned to a scalar. A scalar can't be shifted by a
5267 // vector either.
5268 if (!right->isScalar() &&
5269 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5270 return false;
5271 }
5272 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005273 default:
5274 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005275 }
5276
Olli Etuahod6b14282015-03-17 14:31:35 +02005277 return true;
5278}
5279
Olli Etuaho1dded802016-08-18 18:13:13 +03005280bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5281 const TType &left,
5282 const TType &right)
5283{
5284 switch (op)
5285 {
5286 case EOpMul:
5287 case EOpMulAssign:
5288 return left.getNominalSize() == right.getNominalSize() &&
5289 left.getSecondarySize() == right.getSecondarySize();
5290 case EOpVectorTimesScalar:
5291 return true;
5292 case EOpVectorTimesScalarAssign:
5293 ASSERT(!left.isMatrix() && !right.isMatrix());
5294 return left.isVector() && !right.isVector();
5295 case EOpVectorTimesMatrix:
5296 return left.getNominalSize() == right.getRows();
5297 case EOpVectorTimesMatrixAssign:
5298 ASSERT(!left.isMatrix() && right.isMatrix());
5299 return left.isVector() && left.getNominalSize() == right.getRows() &&
5300 left.getNominalSize() == right.getCols();
5301 case EOpMatrixTimesVector:
5302 return left.getCols() == right.getNominalSize();
5303 case EOpMatrixTimesScalar:
5304 return true;
5305 case EOpMatrixTimesScalarAssign:
5306 ASSERT(left.isMatrix() && !right.isMatrix());
5307 return !right.isVector();
5308 case EOpMatrixTimesMatrix:
5309 return left.getCols() == right.getRows();
5310 case EOpMatrixTimesMatrixAssign:
5311 ASSERT(left.isMatrix() && right.isMatrix());
5312 // We need to check two things:
5313 // 1. The matrix multiplication step is valid.
5314 // 2. The result will have the same number of columns as the lvalue.
5315 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5316
5317 default:
5318 UNREACHABLE();
5319 return false;
5320 }
5321}
5322
Jamie Madillb98c3a82015-07-23 14:26:04 -04005323TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5324 TIntermTyped *left,
5325 TIntermTyped *right,
5326 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005327{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005328 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005329 return nullptr;
5330
Olli Etuahofc1806e2015-03-17 13:03:11 +02005331 switch (op)
5332 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005333 case EOpEqual:
5334 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335 case EOpLessThan:
5336 case EOpGreaterThan:
5337 case EOpLessThanEqual:
5338 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005339 break;
5340 case EOpLogicalOr:
5341 case EOpLogicalXor:
5342 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005343 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5344 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005345 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346 {
5347 return nullptr;
5348 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005349 // Basic types matching should have been already checked.
5350 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005351 break;
5352 case EOpAdd:
5353 case EOpSub:
5354 case EOpDiv:
5355 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005356 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5357 !right->getType().getStruct());
5358 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005359 {
5360 return nullptr;
5361 }
5362 break;
5363 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005364 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5365 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005366 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005367 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005368 {
5369 return nullptr;
5370 }
5371 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005372 default:
5373 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005374 }
5375
Olli Etuaho1dded802016-08-18 18:13:13 +03005376 if (op == EOpMul)
5377 {
5378 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5379 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5380 {
5381 return nullptr;
5382 }
5383 }
5384
Olli Etuaho3fdec912016-08-18 15:08:06 +03005385 TIntermBinary *node = new TIntermBinary(op, left, right);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005386 ASSERT(op != EOpAssign);
5387 markStaticReadIfSymbol(left);
5388 markStaticReadIfSymbol(right);
Olli Etuaho3fdec912016-08-18 15:08:06 +03005389 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005390 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005391}
5392
Jamie Madillb98c3a82015-07-23 14:26:04 -04005393TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5394 TIntermTyped *left,
5395 TIntermTyped *right,
5396 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005397{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005398 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005399 if (node == 0)
5400 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005401 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho09b22472015-02-11 11:47:26 +02005402 return left;
5403 }
5404 return node;
5405}
5406
Jamie Madillb98c3a82015-07-23 14:26:04 -04005407TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5408 TIntermTyped *left,
5409 TIntermTyped *right,
5410 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005411{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005412 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005413 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005414 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005415 binaryOpError(loc, GetOperatorString(op), left->getType(), right->getType());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005416 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005417 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005418 }
5419 return node;
5420}
5421
Jamie Madillb98c3a82015-07-23 14:26:04 -04005422TIntermTyped *TParseContext::addAssign(TOperator op,
5423 TIntermTyped *left,
5424 TIntermTyped *right,
5425 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005426{
Olli Etuahocce89652017-06-19 16:04:09 +03005427 checkCanBeLValue(loc, "assign", left);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005428 TIntermBinary *node = nullptr;
5429 if (binaryOpCommonCheck(op, left, right, loc))
5430 {
5431 if (op == EOpMulAssign)
5432 {
5433 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5434 if (isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5435 {
5436 node = new TIntermBinary(op, left, right);
5437 }
5438 }
5439 else
5440 {
5441 node = new TIntermBinary(op, left, right);
5442 }
5443 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005444 if (node == nullptr)
5445 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005446 assignError(loc, "assign", left->getType(), right->getType());
Olli Etuahod6b14282015-03-17 14:31:35 +02005447 return left;
5448 }
Olli Etuaho94bbed12018-03-20 14:44:53 +02005449 if (op != EOpAssign)
5450 {
5451 markStaticReadIfSymbol(left);
5452 }
5453 markStaticReadIfSymbol(right);
Olli Etuaho7b7d2e62018-03-23 16:37:36 +02005454 node->setLine(loc);
Olli Etuahod6b14282015-03-17 14:31:35 +02005455 return node;
5456}
5457
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005458TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5459 TIntermTyped *right,
5460 const TSourceLoc &loc)
5461{
Corentin Wallez0d959252016-07-12 17:26:32 -04005462 // WebGL2 section 5.26, the following results in an error:
5463 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005464 if (mShaderSpec == SH_WEBGL2_SPEC &&
5465 (left->isArray() || left->getBasicType() == EbtVoid ||
5466 left->getType().isStructureContainingArrays() || right->isArray() ||
5467 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005468 {
5469 error(loc,
5470 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5471 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005472 }
5473
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005474 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho94bbed12018-03-20 14:44:53 +02005475 markStaticReadIfSymbol(left);
5476 markStaticReadIfSymbol(right);
5477 commaNode->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005478
5479 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005480}
5481
Olli Etuaho49300862015-02-20 14:54:49 +02005482TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5483{
5484 switch (op)
5485 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005486 case EOpContinue:
5487 if (mLoopNestingLevel <= 0)
5488 {
5489 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005490 }
5491 break;
5492 case EOpBreak:
5493 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5494 {
5495 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005496 }
5497 break;
5498 case EOpReturn:
5499 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5500 {
5501 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005502 }
5503 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005504 case EOpKill:
5505 if (mShaderType != GL_FRAGMENT_SHADER)
5506 {
5507 error(loc, "discard supported in fragment shaders only", "discard");
5508 }
5509 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005510 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005511 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005512 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005513 }
Olli Etuahocce89652017-06-19 16:04:09 +03005514 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005515}
5516
Jamie Madillb98c3a82015-07-23 14:26:04 -04005517TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005518 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005519 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005520{
Olli Etuahocce89652017-06-19 16:04:09 +03005521 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005522 {
Olli Etuaho94bbed12018-03-20 14:44:53 +02005523 markStaticReadIfSymbol(expression);
Olli Etuahocce89652017-06-19 16:04:09 +03005524 ASSERT(op == EOpReturn);
5525 mFunctionReturnsValue = true;
5526 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5527 {
5528 error(loc, "void function cannot return a value", "return");
5529 }
5530 else if (*mCurrentFunctionType != expression->getType())
5531 {
5532 error(loc, "function return is not matching type:", "return");
5533 }
Olli Etuaho49300862015-02-20 14:54:49 +02005534 }
Olli Etuahocce89652017-06-19 16:04:09 +03005535 TIntermBranch *node = new TIntermBranch(op, expression);
5536 node->setLine(loc);
5537 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005538}
5539
Olli Etuaho94bbed12018-03-20 14:44:53 +02005540void TParseContext::appendStatement(TIntermBlock *block, TIntermNode *statement)
5541{
5542 if (statement != nullptr)
5543 {
5544 markStaticReadIfSymbol(statement);
5545 block->appendStatement(statement);
5546 }
5547}
5548
Martin Radev84aa2dc2017-09-11 15:51:02 +03005549void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5550{
5551 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005552 const TFunction *func = functionCall->getFunction();
5553 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005554 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005555 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005556 TIntermNode *componentNode = nullptr;
5557 TIntermSequence *arguments = functionCall->getSequence();
5558 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5559 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5560 ASSERT(sampler != nullptr);
5561 switch (sampler->getBasicType())
5562 {
5563 case EbtSampler2D:
5564 case EbtISampler2D:
5565 case EbtUSampler2D:
5566 case EbtSampler2DArray:
5567 case EbtISampler2DArray:
5568 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005569 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005570 (isTextureGatherOffset && arguments->size() == 4u))
5571 {
5572 componentNode = arguments->back();
5573 }
5574 break;
5575 case EbtSamplerCube:
5576 case EbtISamplerCube:
5577 case EbtUSamplerCube:
5578 ASSERT(!isTextureGatherOffset);
5579 if (arguments->size() == 3u)
5580 {
5581 componentNode = arguments->back();
5582 }
5583 break;
5584 case EbtSampler2DShadow:
5585 case EbtSampler2DArrayShadow:
5586 case EbtSamplerCubeShadow:
5587 break;
5588 default:
5589 UNREACHABLE();
5590 break;
5591 }
5592 if (componentNode)
5593 {
5594 const TIntermConstantUnion *componentConstantUnion =
5595 componentNode->getAsConstantUnion();
5596 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5597 {
5598 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005599 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005600 }
5601 else
5602 {
5603 int component = componentConstantUnion->getIConst(0);
5604 if (component < 0 || component > 3)
5605 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005606 error(functionCall->getLine(), "Component must be in the range [0;3]",
5607 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005608 }
5609 }
5610 }
5611 }
5612}
5613
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005614void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5615{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005616 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005617 const TFunction *func = functionCall->getFunction();
Jamie Madill50cf2be2018-06-15 09:46:57 -04005618 TIntermNode *offset = nullptr;
5619 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005620 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005621 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005622 {
5623 offset = arguments->back();
5624 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005625 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005626 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005627 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005628 ASSERT(arguments->size() >= 3);
5629 offset = (*arguments)[2];
5630 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005631 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005632 {
5633 ASSERT(arguments->size() >= 3u);
5634 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5635 ASSERT(sampler != nullptr);
5636 switch (sampler->getBasicType())
5637 {
5638 case EbtSampler2D:
5639 case EbtISampler2D:
5640 case EbtUSampler2D:
5641 case EbtSampler2DArray:
5642 case EbtISampler2DArray:
5643 case EbtUSampler2DArray:
5644 offset = (*arguments)[2];
5645 break;
5646 case EbtSampler2DShadow:
5647 case EbtSampler2DArrayShadow:
5648 offset = (*arguments)[3];
5649 break;
5650 default:
5651 UNREACHABLE();
5652 break;
5653 }
5654 useTextureGatherOffsetConstraints = true;
5655 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005656 if (offset != nullptr)
5657 {
5658 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5659 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5660 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005661 error(functionCall->getLine(), "Texture offset must be a constant expression",
5662 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005663 }
5664 else
5665 {
5666 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5667 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005668 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005669 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5670 : mMinProgramTexelOffset;
5671 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5672 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005673 for (size_t i = 0u; i < size; ++i)
5674 {
5675 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005676 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005677 {
5678 std::stringstream tokenStream;
5679 tokenStream << offsetValue;
5680 std::string token = tokenStream.str();
5681 error(offset->getLine(), "Texture offset value out of valid range",
5682 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005683 }
5684 }
5685 }
5686 }
5687}
5688
Jiajia Qina3106c52017-11-03 09:39:39 +08005689void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5690{
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005691 const TFunction *func = functionCall->getFunction();
5692 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005693 {
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005694 ASSERT(IsAtomicFunction(functionCall->getOp()));
Jiajia Qina3106c52017-11-03 09:39:39 +08005695 TIntermSequence *arguments = functionCall->getSequence();
5696 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5697
5698 if (IsBufferOrSharedVariable(memNode))
5699 {
5700 return;
5701 }
5702
5703 while (memNode->getAsBinaryNode())
5704 {
5705 memNode = memNode->getAsBinaryNode()->getLeft();
5706 if (IsBufferOrSharedVariable(memNode))
5707 {
5708 return;
5709 }
5710 }
5711
5712 error(memNode->getLine(),
5713 "The value passed to the mem argument of an atomic memory function does not "
5714 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005715 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005716 }
5717}
5718
Martin Radev2cc85b32016-08-05 16:22:53 +03005719// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5720void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5721{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005722 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005723
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005724 const TFunction *func = functionCall->getFunction();
5725
5726 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005727 {
5728 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005730
Olli Etuaho485eefd2017-02-14 17:40:06 +00005731 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005732
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005733 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005734 {
5735 if (memoryQualifier.readonly)
5736 {
5737 error(imageNode->getLine(),
5738 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005739 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005740 }
5741 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005742 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005743 {
5744 if (memoryQualifier.writeonly)
5745 {
5746 error(imageNode->getLine(),
5747 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005748 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005749 }
5750 }
5751 }
5752}
5753
5754// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5755void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5756 const TFunction *functionDefinition,
5757 const TIntermAggregate *functionCall)
5758{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005759 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005760
5761 const TIntermSequence &arguments = *functionCall->getSequence();
5762
5763 ASSERT(functionDefinition->getParamCount() == arguments.size());
5764
5765 for (size_t i = 0; i < arguments.size(); ++i)
5766 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005767 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5768 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005769 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005770 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5771
5772 if (IsImage(functionArgumentType.getBasicType()))
5773 {
5774 const TMemoryQualifier &functionArgumentMemoryQualifier =
5775 functionArgumentType.getMemoryQualifier();
5776 const TMemoryQualifier &functionParameterMemoryQualifier =
5777 functionParameterType.getMemoryQualifier();
5778 if (functionArgumentMemoryQualifier.readonly &&
5779 !functionParameterMemoryQualifier.readonly)
5780 {
5781 error(functionCall->getLine(),
5782 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005783 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005784 }
5785
5786 if (functionArgumentMemoryQualifier.writeonly &&
5787 !functionParameterMemoryQualifier.writeonly)
5788 {
5789 error(functionCall->getLine(),
5790 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005791 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005792 }
Martin Radev049edfa2016-11-11 14:35:37 +02005793
5794 if (functionArgumentMemoryQualifier.coherent &&
5795 !functionParameterMemoryQualifier.coherent)
5796 {
5797 error(functionCall->getLine(),
5798 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005799 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005800 }
5801
5802 if (functionArgumentMemoryQualifier.volatileQualifier &&
5803 !functionParameterMemoryQualifier.volatileQualifier)
5804 {
5805 error(functionCall->getLine(),
5806 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005807 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005808 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005809 }
5810 }
5811}
5812
Olli Etuaho95ed1942018-02-01 14:01:19 +02005813TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005814{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005815 if (fnCall->thisNode() != nullptr)
5816 {
5817 return addMethod(fnCall, loc);
5818 }
5819 if (fnCall->isConstructor())
5820 {
5821 return addConstructor(fnCall, loc);
5822 }
5823 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005824}
5825
Olli Etuaho95ed1942018-02-01 14:01:19 +02005826TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005827{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005828 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5830 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5831 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005832 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005833 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005834 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005835 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005837 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005838 {
5839 error(loc, "method takes no parameters", "length");
5840 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005841 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 {
5843 error(loc, "length can only be called on arrays", "length");
5844 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005845 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005846 mGeometryShaderInputPrimitiveType == EptUndefined)
5847 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005848 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005849 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5850 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 else
5852 {
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005853 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode, nullptr);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005854 node->setLine(loc);
5855 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005856 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005857 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858}
5859
Olli Etuaho95ed1942018-02-01 14:01:19 +02005860TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 const TSourceLoc &loc)
5862{
Olli Etuaho697bf652018-02-16 11:50:54 +02005863 // First check whether the function has been hidden by a variable name or struct typename by
5864 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5865 // with a matching argument list.
5866 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005867 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005868 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005869 }
5870 else
5871 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005872 // There are no inner functions, so it's enough to look for user-defined functions in the
5873 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005874 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005875 if (symbol != nullptr)
5876 {
5877 // A user-defined function - could be an overloaded built-in as well.
5878 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5879 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5880 TIntermAggregate *callNode =
5881 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5882 callNode->setLine(loc);
5883 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5884 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5885 return callNode;
5886 }
5887
5888 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005890 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005891 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005892 }
5893 else
5894 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005895 // A built-in function.
5896 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005898
Olli Etuaho37b697e2018-01-29 12:19:27 +02005899 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005901 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005902 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005903 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005904 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005906 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005907 if (fnCandidate->getParamCount() == 1)
5908 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005909 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005910 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuaho5fec7ab2018-04-04 11:58:33 +03005911 TIntermTyped *callNode =
5912 createUnaryMath(op, unaryParamNode->getAsTyped(), loc, fnCandidate);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005913 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005914 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005915 }
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005916
Olli Etuahoe80825e2018-02-16 10:24:53 +02005917 TIntermAggregate *callNode =
5918 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005919 callNode->setLine(loc);
5920
Jiawei Shaoa6a78422018-06-28 08:32:54 +08005921 checkAtomicMemoryBuiltinFunctions(callNode);
5922
Olli Etuahoe80825e2018-02-16 10:24:53 +02005923 // Some built-in functions have out parameters too.
5924 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5925
5926 // See if we can constant fold a built-in. Note that this may be possible
5927 // even if it is not const-qualified.
5928 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005929 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005930
5931 // This is a built-in function with no op associated with it.
5932 TIntermAggregate *callNode =
5933 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5934 callNode->setLine(loc);
5935 checkTextureOffsetConst(callNode);
5936 checkTextureGather(callNode);
5937 checkImageMemoryAccessForBuiltinFunctions(callNode);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005938 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5939 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005940 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005941 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005942
5943 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005944 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005945}
5946
Jamie Madillb98c3a82015-07-23 14:26:04 -04005947TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005948 TIntermTyped *trueExpression,
5949 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005950 const TSourceLoc &loc)
5951{
Olli Etuaho56229f12017-07-10 14:16:33 +03005952 if (!checkIsScalarBool(loc, cond))
5953 {
5954 return falseExpression;
5955 }
Olli Etuaho52901742015-04-15 13:42:45 +03005956
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005957 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005958 {
Olli Etuaho72e35892018-06-20 11:43:08 +03005959 TInfoSinkBase reasonStream;
5960 reasonStream << "mismatching ternary operator operand types '" << trueExpression->getType()
5961 << " and '" << falseExpression->getType() << "'";
5962 error(loc, reasonStream.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005963 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005964 }
Olli Etuahode318b22016-10-25 16:18:25 +01005965 if (IsOpaqueType(trueExpression->getBasicType()))
5966 {
5967 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005968 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005969 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5970 // Note that structs containing opaque types don't need to be checked as structs are
5971 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005972 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005973 return falseExpression;
5974 }
5975
Jiajia Qinbc585152017-06-23 15:42:17 +08005976 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5977 falseExpression->getMemoryQualifier().writeonly)
5978 {
5979 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5980 return falseExpression;
5981 }
5982
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005983 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005984 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005985 // ESSL 3.00.6 section 5.7:
5986 // Ternary operator support is optional for arrays. No certainty that it works across all
5987 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5988 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005989 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005990 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005991 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005992 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005993 }
Olli Etuaho94050052017-05-08 14:17:44 +03005994 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5995 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005996 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005997 return falseExpression;
5998 }
5999
Corentin Wallez0d959252016-07-12 17:26:32 -04006000 // WebGL2 section 5.26, the following results in an error:
6001 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006002 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04006003 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03006004 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03006005 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04006006 }
6007
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006008 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
Olli Etuaho94bbed12018-03-20 14:44:53 +02006009 markStaticReadIfSymbol(cond);
6010 markStaticReadIfSymbol(trueExpression);
6011 markStaticReadIfSymbol(falseExpression);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03006012 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02006013 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03006014}
Olli Etuaho49300862015-02-20 14:54:49 +02006015
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00006016//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006017// Parse an array of strings using yyparse.
6018//
6019// Returns 0 for success.
6020//
Jamie Madillb98c3a82015-07-23 14:26:04 -04006021int PaParseStrings(size_t count,
6022 const char *const string[],
6023 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306024 TParseContext *context)
6025{
Yunchao He4f285442017-04-21 12:15:49 +08006026 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006027 return 1;
6028
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006029 if (glslang_initialize(context))
6030 return 1;
6031
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006032 int error = glslang_scan(count, string, length, context);
6033 if (!error)
6034 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006035
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006036 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006037
alokp@chromium.org6b495712012-06-29 00:06:58 +00006038 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006039}
Jamie Madill45bcc782016-11-07 13:58:48 -05006040
6041} // namespace sh