blob: 0a01022f00a3120d475ab13a6500cf4918b6d608 [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 Etuaho2bfe9f62018-03-02 16:53:29 +020014#include "compiler/translator/BuiltIn_autogen.h"
Olli Etuahod5f44c92017-11-29 17:15:40 +020015#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030016#include "compiler/translator/IntermNode_util.h"
Olli Etuaho2bfe9f62018-03-02 16:53:29 +020017#include "compiler/translator/ParseContext_autogen.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080018#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030019#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080020#include "compiler/translator/ValidateSwitch.h"
21#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030022#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
alokp@chromium.org8b851c62012-06-15 16:25:11 +000027///////////////////////////////////////////////////////////////////////
28//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000029// Sub- vector and matrix fields
30//
31////////////////////////////////////////////////////////////////////////
32
Martin Radev2cc85b32016-08-05 16:22:53 +030033namespace
34{
35
36const int kWebGLMaxStructNesting = 4;
37
Olli Etuaho0f684632017-07-13 12:42:15 +030038bool ContainsSampler(const TStructure *structType);
39
Martin Radev2cc85b32016-08-05 16:22:53 +030040bool ContainsSampler(const TType &type)
41{
42 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030043 {
Martin Radev2cc85b32016-08-05 16:22:53 +030044 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030045 }
jchen10cc2a10e2017-05-03 14:05:12 +080046 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030047 {
Olli Etuaho0f684632017-07-13 12:42:15 +030048 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030049 }
50
51 return false;
52}
53
Olli Etuaho0f684632017-07-13 12:42:15 +030054bool ContainsSampler(const TStructure *structType)
55{
56 for (const auto &field : structType->fields())
57 {
58 if (ContainsSampler(*field->type()))
59 return true;
60 }
61 return false;
62}
63
Olli Etuaho485eefd2017-02-14 17:40:06 +000064// Get a token from an image argument to use as an error message token.
65const char *GetImageArgumentToken(TIntermTyped *imageNode)
66{
67 ASSERT(IsImage(imageNode->getBasicType()));
68 while (imageNode->getAsBinaryNode() &&
69 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
70 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
71 {
72 imageNode = imageNode->getAsBinaryNode()->getLeft();
73 }
74 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
75 if (imageSymbol)
76 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020077 return imageSymbol->getName().data();
Olli Etuaho485eefd2017-02-14 17:40:06 +000078 }
79 return "image";
80}
81
Olli Etuahocce89652017-06-19 16:04:09 +030082bool CanSetDefaultPrecisionOnType(const TPublicType &type)
83{
84 if (!SupportsPrecision(type.getBasicType()))
85 {
86 return false;
87 }
88 if (type.getBasicType() == EbtUInt)
89 {
90 // ESSL 3.00.4 section 4.5.4
91 return false;
92 }
93 if (type.isAggregate())
94 {
95 // Not allowed to set for aggregate types
96 return false;
97 }
98 return true;
99}
100
Jiawei Shaod8105a02017-08-08 09:54:36 +0800101// Map input primitive types to input array sizes in a geometry shader.
102GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
103{
104 switch (primitiveType)
105 {
106 case EptPoints:
107 return 1u;
108 case EptLines:
109 return 2u;
110 case EptTriangles:
111 return 3u;
112 case EptLinesAdjacency:
113 return 4u;
114 case EptTrianglesAdjacency:
115 return 6u;
116 default:
117 UNREACHABLE();
118 return 0u;
119 }
120}
121
Jiajia Qina3106c52017-11-03 09:39:39 +0800122bool IsBufferOrSharedVariable(TIntermTyped *var)
123{
124 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
125 var->getQualifier() == EvqShared)
126 {
127 return true;
128 }
129 return false;
130}
131
Martin Radev2cc85b32016-08-05 16:22:53 +0300132} // namespace
133
jchen104cdac9e2017-05-08 11:01:20 +0800134// This tracks each binding point's current default offset for inheritance of subsequent
135// variables using the same binding, and keeps offsets unique and non overlapping.
136// See GLSL ES 3.1, section 4.4.6.
137class TParseContext::AtomicCounterBindingState
138{
139 public:
140 AtomicCounterBindingState() : mDefaultOffset(0) {}
141 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
142 // newly inserted span.
143 int insertSpan(int start, size_t length)
144 {
145 gl::RangeI newSpan(start, start + static_cast<int>(length));
146 for (const auto &span : mSpans)
147 {
148 if (newSpan.intersects(span))
149 {
150 return -1;
151 }
152 }
153 mSpans.push_back(newSpan);
154 mDefaultOffset = newSpan.high();
155 return start;
156 }
157 // Inserts a new span starting from the default offset.
158 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
159 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
160
161 private:
162 int mDefaultOffset;
163 std::vector<gl::RangeI> mSpans;
164};
165
Jamie Madillacb4b812016-11-07 13:50:29 -0500166TParseContext::TParseContext(TSymbolTable &symt,
167 TExtensionBehavior &ext,
168 sh::GLenum type,
169 ShShaderSpec spec,
170 ShCompileOptions options,
171 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000172 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500173 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300174 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300175 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500176 mShaderType(type),
177 mShaderSpec(spec),
178 mCompileOptions(options),
179 mShaderVersion(100),
180 mTreeRoot(nullptr),
181 mLoopNestingLevel(0),
182 mStructNestingLevel(0),
183 mSwitchNestingLevel(0),
184 mCurrentFunctionType(nullptr),
185 mFunctionReturnsValue(false),
186 mChecksPrecisionErrors(checksPrecErrors),
187 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800188 mDefaultUniformMatrixPacking(EmpColumnMajor),
189 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
190 mDefaultBufferMatrixPacking(EmpColumnMajor),
191 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000192 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500193 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000194 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500195 mShaderVersion,
196 mShaderType,
197 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000198 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500199 mScanner(nullptr),
200 mUsesFragData(false),
201 mUsesFragColor(false),
202 mUsesSecondaryOutputs(false),
203 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
204 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300205 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
206 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500208 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000209 mNumViews(-1),
210 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000211 mMaxImageUnits(resources.MaxImageUnits),
212 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000213 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800214 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800215 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800216 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800217 mDeclaringFunction(false),
218 mGeometryShaderInputPrimitiveType(EptUndefined),
219 mGeometryShaderOutputPrimitiveType(EptUndefined),
220 mGeometryShaderInvocations(0),
221 mGeometryShaderMaxVertices(-1),
222 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800223 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Olli Etuahoc74ec1a2018-01-09 15:23:28 +0200224 mGlInVariableWithArraySize(nullptr)
Jamie Madillacb4b812016-11-07 13:50:29 -0500225{
Jamie Madillacb4b812016-11-07 13:50:29 -0500226}
227
jchen104cdac9e2017-05-08 11:01:20 +0800228TParseContext::~TParseContext()
229{
230}
231
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300232bool TParseContext::parseVectorFields(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200233 const ImmutableString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400234 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300235 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300237 ASSERT(fieldOffsets);
Olli Etuahofbb1c792018-01-19 16:26:59 +0200238 size_t fieldCount = compString.length();
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300239 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200241 error(line, "illegal vector field selection", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000242 return false;
243 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300244 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 enum
247 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000248 exyzw,
249 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000250 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000251 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
255 switch (compString[i])
256 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400257 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 fieldSet[i] = exyzw;
260 break;
261 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300262 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400263 fieldSet[i] = ergba;
264 break;
265 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300266 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400267 fieldSet[i] = estpq;
268 break;
269 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300270 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 fieldSet[i] = exyzw;
272 break;
273 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300274 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400275 fieldSet[i] = ergba;
276 break;
277 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300278 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 fieldSet[i] = estpq;
280 break;
281 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300282 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 fieldSet[i] = exyzw;
284 break;
285 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300286 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287 fieldSet[i] = ergba;
288 break;
289 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300290 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400291 fieldSet[i] = estpq;
292 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530293
Jamie Madillb98c3a82015-07-23 14:26:04 -0400294 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300295 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 fieldSet[i] = exyzw;
297 break;
298 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300299 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400300 fieldSet[i] = ergba;
301 break;
302 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300303 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304 fieldSet[i] = estpq;
305 break;
306 default:
Olli Etuahofbb1c792018-01-19 16:26:59 +0200307 error(line, "illegal vector field selection", compString);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 }
310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300312 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300314 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530315 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200316 error(line, "vector field selection out of range", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 return false;
318 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319
Arun Patole7e7e68d2015-05-22 12:02:25 +0530320 if (i > 0)
321 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400322 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530323 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200324 error(line, "illegal - vector component fields not from the same set", compString);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 return false;
326 }
327 }
328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331}
332
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333///////////////////////////////////////////////////////////////////////
334//
335// Errors
336//
337////////////////////////////////////////////////////////////////////////
338
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339//
340// Used by flex/bison to output all syntax and parsing errors.
341//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000342void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000344 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345}
346
Olli Etuahofbb1c792018-01-19 16:26:59 +0200347void TParseContext::error(const TSourceLoc &loc, const char *reason, const ImmutableString &token)
348{
349 mDiagnostics->error(loc, reason, token.data());
350}
351
Olli Etuaho4de340a2016-12-16 09:32:03 +0000352void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530353{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000354 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000355}
356
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200357void TParseContext::outOfRangeError(bool isError,
358 const TSourceLoc &loc,
359 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000360 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200361{
362 if (isError)
363 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000364 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200365 }
366 else
367 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000368 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200369 }
370}
371
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372//
373// Same error message for all places assignments don't work.
374//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530375void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000377 std::stringstream reasonStream;
378 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
379 std::string reason = reasonStream.str();
380 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381}
382
383//
384// Same error message for all places unary operations don't work.
385//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530386void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000388 std::stringstream reasonStream;
389 reasonStream << "wrong operand type - no operation '" << op
390 << "' exists that takes an operand of type " << operand
391 << " (or there is no acceptable conversion)";
392 std::string reason = reasonStream.str();
393 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394}
395
396//
397// Same error message for all binary operations don't work.
398//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400399void TParseContext::binaryOpError(const TSourceLoc &line,
400 const char *op,
401 TString left,
402 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000404 std::stringstream reasonStream;
405 reasonStream << "wrong operand types - no operation '" << op
406 << "' exists that takes a left-hand operand of type '" << left
407 << "' and a right operand of type '" << right
408 << "' (or there is no acceptable conversion)";
409 std::string reason = reasonStream.str();
410 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411}
412
Olli Etuaho856c4972016-08-08 11:38:39 +0300413void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
414 TPrecision precision,
415 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530416{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400417 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300418 return;
Martin Radev70866b82016-07-22 15:27:42 +0300419
420 if (precision != EbpUndefined && !SupportsPrecision(type))
421 {
422 error(line, "illegal type for precision qualifier", getBasicString(type));
423 }
424
Olli Etuaho183d7e22015-11-20 15:59:09 +0200425 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200427 switch (type)
428 {
429 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400430 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300431 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200432 case EbtInt:
433 case EbtUInt:
434 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400435 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300436 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800438 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200439 {
jchen10cc2a10e2017-05-03 14:05:12 +0800440 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300441 return;
442 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200443 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000444 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000445}
446
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447// Both test and if necessary, spit out an error, to see if the node is really
448// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300449bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500451 TIntermSymbol *symNode = node->getAsSymbolNode();
452 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100453 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
454
455 if (swizzleNode)
456 {
457 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
458 if (ok && swizzleNode->hasDuplicateOffsets())
459 {
460 error(line, " l-value of swizzle cannot have duplicate components", op);
461 return false;
462 }
463 return ok;
464 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 if (binaryNode)
467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400468 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400470 case EOpIndexDirect:
471 case EOpIndexIndirect:
472 case EOpIndexDirectStruct:
473 case EOpIndexDirectInterfaceBlock:
Qin Jiajia76bf01d2018-02-22 14:11:34 +0800474 if (node->getMemoryQualifier().readonly)
475 {
476 error(line, "can't modify a readonly variable", op);
477 return false;
478 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300479 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 default:
481 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000483 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300484 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486
jchen10cc2a10e2017-05-03 14:05:12 +0800487 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530488 switch (node->getQualifier())
489 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400490 case EvqConst:
491 message = "can't modify a const";
492 break;
493 case EvqConstReadOnly:
494 message = "can't modify a const";
495 break;
496 case EvqAttribute:
497 message = "can't modify an attribute";
498 break;
499 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400500 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800501 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800502 case EvqFlatIn:
503 case EvqSmoothIn:
504 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 message = "can't modify an input";
506 break;
507 case EvqUniform:
508 message = "can't modify a uniform";
509 break;
510 case EvqVaryingIn:
511 message = "can't modify a varying";
512 break;
513 case EvqFragCoord:
514 message = "can't modify gl_FragCoord";
515 break;
516 case EvqFrontFacing:
517 message = "can't modify gl_FrontFacing";
518 break;
519 case EvqPointCoord:
520 message = "can't modify gl_PointCoord";
521 break;
Martin Radevb0883602016-08-04 17:48:58 +0300522 case EvqNumWorkGroups:
523 message = "can't modify gl_NumWorkGroups";
524 break;
525 case EvqWorkGroupSize:
526 message = "can't modify gl_WorkGroupSize";
527 break;
528 case EvqWorkGroupID:
529 message = "can't modify gl_WorkGroupID";
530 break;
531 case EvqLocalInvocationID:
532 message = "can't modify gl_LocalInvocationID";
533 break;
534 case EvqGlobalInvocationID:
535 message = "can't modify gl_GlobalInvocationID";
536 break;
537 case EvqLocalInvocationIndex:
538 message = "can't modify gl_LocalInvocationIndex";
539 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300540 case EvqViewIDOVR:
541 message = "can't modify gl_ViewID_OVR";
542 break;
Martin Radev802abe02016-08-04 17:48:32 +0300543 case EvqComputeIn:
544 message = "can't modify work group size variable";
545 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800546 case EvqPerVertexIn:
547 message = "can't modify any member in gl_in";
548 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800549 case EvqPrimitiveIDIn:
550 message = "can't modify gl_PrimitiveIDIn";
551 break;
552 case EvqInvocationID:
553 message = "can't modify gl_InvocationID";
554 break;
555 case EvqPrimitiveID:
556 if (mShaderType == GL_FRAGMENT_SHADER)
557 {
558 message = "can't modify gl_PrimitiveID in a fragment shader";
559 }
560 break;
561 case EvqLayer:
562 if (mShaderType == GL_FRAGMENT_SHADER)
563 {
564 message = "can't modify gl_Layer in a fragment shader";
565 }
566 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400567 default:
568 //
569 // Type that can't be written to?
570 //
571 if (node->getBasicType() == EbtVoid)
572 {
573 message = "can't modify void";
574 }
jchen10cc2a10e2017-05-03 14:05:12 +0800575 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 {
jchen10cc2a10e2017-05-03 14:05:12 +0800577 message = "can't modify a variable with type ";
578 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300579 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800580 else if (node->getMemoryQualifier().readonly)
581 {
582 message = "can't modify a readonly variable";
583 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585
jchen10cc2a10e2017-05-03 14:05:12 +0800586 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000588 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
Olli Etuaho8a176262016-08-16 14:23:01 +0300590 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 //
594 // Everything else is okay, no error.
595 //
jchen10cc2a10e2017-05-03 14:05:12 +0800596 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300597 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 //
600 // If we get here, we have an error and a message.
601 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530602 if (symNode)
603 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200604 // Symbol inside an expression can't be nameless.
605 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
606
Olli Etuahofbb1c792018-01-19 16:26:59 +0200607 const ImmutableString &symbol = symNode->getName();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000608 std::stringstream reasonStream;
609 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
610 std::string reason = reasonStream.str();
611 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000612 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 else
614 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000615 std::stringstream reasonStream;
616 reasonStream << "l-value required (" << message << ")";
617 std::string reason = reasonStream.str();
618 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
Olli Etuaho8a176262016-08-16 14:23:01 +0300621 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622}
623
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624// Both test, and if necessary spit out an error, to see if the node is really
625// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300626void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627{
Olli Etuaho383b7912016-08-05 11:22:59 +0300628 if (node->getQualifier() != EvqConst)
629 {
630 error(node->getLine(), "constant expression required", "");
631 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632}
633
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634// Both test, and if necessary spit out an error, to see if the node is really
635// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300636void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637{
Olli Etuaho383b7912016-08-05 11:22:59 +0300638 if (!node->isScalarInt())
639 {
640 error(node->getLine(), "integer expression required", token);
641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644// Both test, and if necessary spit out an error, to see if we are currently
645// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800646bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647{
Olli Etuaho856c4972016-08-08 11:38:39 +0300648 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300649 {
650 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800651 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300652 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800653 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300656// ESSL 3.00.5 sections 3.8 and 3.9.
657// If it starts "gl_" or contains two consecutive underscores, it's reserved.
658// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuahofbb1c792018-01-19 16:26:59 +0200659bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const ImmutableString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530661 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahofbb1c792018-01-19 16:26:59 +0200662 if (identifier.beginsWith("gl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 error(line, reservedErrMsg, "gl_");
665 return false;
666 }
667 if (sh::IsWebGLBasedSpec(mShaderSpec))
668 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200669 if (identifier.beginsWith("webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300671 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300672 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200674 if (identifier.beginsWith("_webgl_"))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300677 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 }
679 }
Olli Etuahofbb1c792018-01-19 16:26:59 +0200680 if (identifier.contains("__"))
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 {
682 error(line,
683 "identifiers containing two consecutive underscores (__) are reserved as "
684 "possible future keywords",
Olli Etuahofbb1c792018-01-19 16:26:59 +0200685 identifier);
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300686 return false;
687 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300688 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689}
690
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300691// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300692bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuaho95ed1942018-02-01 14:01:19 +0200693 const TIntermSequence &arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300694 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695{
Olli Etuaho95ed1942018-02-01 14:01:19 +0200696 if (arguments.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200698 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300699 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000700 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200701
Olli Etuaho95ed1942018-02-01 14:01:19 +0200702 for (TIntermNode *arg : arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530703 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200705 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200707 {
jchen10cc2a10e2017-05-03 14:05:12 +0800708 std::string reason("cannot convert a variable with type ");
709 reason += getBasicString(argTyped->getBasicType());
710 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300711 return false;
712 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800713 else if (argTyped->getMemoryQualifier().writeonly)
714 {
715 error(line, "cannot convert a variable with writeonly", "constructor");
716 return false;
717 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200718 if (argTyped->getBasicType() == EbtVoid)
719 {
720 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300721 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200722 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
Olli Etuaho856c4972016-08-08 11:38:39 +0300725 if (type.isArray())
726 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300727 // The size of an unsized constructor should already have been determined.
728 ASSERT(!type.isUnsizedArray());
Olli Etuaho95ed1942018-02-01 14:01:19 +0200729 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300730 {
731 error(line, "array constructor needs one argument per array element", "constructor");
732 return false;
733 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300734 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
735 // the array.
Olli Etuaho95ed1942018-02-01 14:01:19 +0200736 for (TIntermNode *const &argNode : arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300737 {
738 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300739 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500740 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300741 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500742 return false;
743 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300744 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000746 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300747 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300748 }
749 }
750 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300751 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300752 {
753 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho95ed1942018-02-01 14:01:19 +0200754 if (fields.size() != arguments.size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300755 {
756 error(line,
757 "Number of constructor parameters does not match the number of structure fields",
758 "constructor");
759 return false;
760 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300761
762 for (size_t i = 0; i < fields.size(); i++)
763 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200764 if (i >= arguments.size() ||
765 arguments[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300766 {
767 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000768 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300769 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300770 }
771 }
772 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300773 else
774 {
775 // We're constructing a scalar, vector, or matrix.
776
777 // Note: It's okay to have too many components available, but not okay to have unused
778 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
779 // there is an extra argument, so 'overFull' will become true.
780
781 size_t size = 0;
782 bool full = false;
783 bool overFull = false;
784 bool matrixArg = false;
Olli Etuaho95ed1942018-02-01 14:01:19 +0200785 for (TIntermNode *arg : arguments)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300786 {
787 const TIntermTyped *argTyped = arg->getAsTyped();
788 ASSERT(argTyped != nullptr);
789
Olli Etuaho487b63a2017-05-23 15:55:09 +0300790 if (argTyped->getBasicType() == EbtStruct)
791 {
792 error(line, "a struct cannot be used as a constructor argument for this type",
793 "constructor");
794 return false;
795 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300796 if (argTyped->getType().isArray())
797 {
798 error(line, "constructing from a non-dereferenced array", "constructor");
799 return false;
800 }
801 if (argTyped->getType().isMatrix())
802 {
803 matrixArg = true;
804 }
805
806 size += argTyped->getType().getObjectSize();
807 if (full)
808 {
809 overFull = true;
810 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300811 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300812 {
813 full = true;
814 }
815 }
816
817 if (type.isMatrix() && matrixArg)
818 {
Olli Etuaho95ed1942018-02-01 14:01:19 +0200819 if (arguments.size() != 1)
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300820 {
821 error(line, "constructing matrix from matrix can only take one argument",
822 "constructor");
823 return false;
824 }
825 }
826 else
827 {
828 if (size != 1 && size < type.getObjectSize())
829 {
830 error(line, "not enough data provided for construction", "constructor");
831 return false;
832 }
833 if (overFull)
834 {
835 error(line, "too many arguments", "constructor");
836 return false;
837 }
838 }
839 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300840
Olli Etuaho8a176262016-08-16 14:23:01 +0300841 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842}
843
Jamie Madillb98c3a82015-07-23 14:26:04 -0400844// This function checks to see if a void variable has been declared and raise an error message for
845// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846//
847// returns true in case of an error
848//
Olli Etuaho856c4972016-08-08 11:38:39 +0300849bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200850 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400851 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300853 if (type == EbtVoid)
854 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200855 error(line, "illegal use of type 'void'", identifier);
Olli Etuaho8a176262016-08-16 14:23:01 +0300856 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858
Olli Etuaho8a176262016-08-16 14:23:01 +0300859 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
Jamie Madillb98c3a82015-07-23 14:26:04 -0400862// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300863// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300864bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300866 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530867 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000868 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300869 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530870 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300871 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872}
873
Jamie Madillb98c3a82015-07-23 14:26:04 -0400874// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300875// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300876void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877{
Martin Radev4a9cd802016-09-01 16:51:51 +0300878 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000880 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
jchen10cc2a10e2017-05-03 14:05:12 +0800884bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
885 const TTypeSpecifierNonArray &pType,
886 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530888 if (pType.type == EbtStruct)
889 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300890 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000892 std::stringstream reasonStream;
893 reasonStream << reason << " (structure contains a sampler)";
894 std::string reasonStr = reasonStream.str();
895 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300896 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 }
jchen10cc2a10e2017-05-03 14:05:12 +0800898 // only samplers need to be checked from structs, since other opaque types can't be struct
899 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300900 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530901 }
jchen10cc2a10e2017-05-03 14:05:12 +0800902 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530903 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000904 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000906 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907
Olli Etuaho8a176262016-08-16 14:23:01 +0300908 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909}
910
Olli Etuaho856c4972016-08-08 11:38:39 +0300911void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
912 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400913{
914 if (pType.layoutQualifier.location != -1)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 error(line, "location must only be specified for a single input or output variable",
917 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400918 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400919}
920
Olli Etuaho856c4972016-08-08 11:38:39 +0300921void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
922 const TLayoutQualifier &layoutQualifier)
923{
924 if (layoutQualifier.location != -1)
925 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000926 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
927 if (mShaderVersion >= 310)
928 {
929 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800930 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000931 }
932 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300933 }
934}
935
Qin Jiajiaca68d982017-09-18 16:41:56 +0800936void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
937 const TLayoutBlockStorage &blockStorage,
938 const TQualifier &qualifier)
939{
940 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
941 {
942 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
943 }
944}
945
Martin Radev2cc85b32016-08-05 16:22:53 +0300946void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
947 TQualifier qualifier,
948 const TType &type)
949{
Martin Radev2cc85b32016-08-05 16:22:53 +0300950 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800951 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530952 {
jchen10cc2a10e2017-05-03 14:05:12 +0800953 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
956
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300958unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000961
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200962 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
963 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
964 // fold as array size.
965 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000966 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000967 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300968 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
Olli Etuaho856c4972016-08-08 11:38:39 +0300971 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400972
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000973 if (constant->getBasicType() == EbtUInt)
974 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300975 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000976 }
977 else
978 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300979 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000980
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400983 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400986
Olli Etuaho856c4972016-08-08 11:38:39 +0300987 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400988 }
989
Olli Etuaho856c4972016-08-08 11:38:39 +0300990 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400991 {
992 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300993 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400994 }
995
996 // The size of arrays is restricted here to prevent issues further down the
997 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
998 // 4096 registers so this should be reasonable even for aggressively optimizable code.
999 const unsigned int sizeLimit = 65536;
1000
Olli Etuaho856c4972016-08-08 11:38:39 +03001001 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001002 {
1003 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001004 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001006
1007 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008}
1009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001011bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1012 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013{
Olli Etuaho8a176262016-08-16 14:23:01 +03001014 if ((elementQualifier.qualifier == EvqAttribute) ||
1015 (elementQualifier.qualifier == EvqVertexIn) ||
1016 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001018 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 TType(elementQualifier).getQualifierString());
1020 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
Olli Etuaho8a176262016-08-16 14:23:01 +03001023 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024}
1025
Olli Etuaho8a176262016-08-16 14:23:01 +03001026// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001027bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1028 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001030 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001031 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001032 error(line, "cannot declare arrays of arrays",
1033 TType(elementType).getCompleteString().c_str());
1034 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001035 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001036 return true;
1037}
1038
1039// Check if this qualified element type can be formed into an array. This is only called when array
1040// brackets are associated with an identifier in a declaration, like this:
1041// float a[2];
1042// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1043// are associated with the type, like this:
1044// float[2] a;
1045bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1046 const TPublicType &elementType)
1047{
1048 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1049 {
1050 return false;
1051 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001052 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1053 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1054 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001055 // Geometry shader requires each user-defined input be declared as arrays or inside input
1056 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1057 // interface matching, such variables and blocks are treated as though they were not declared
1058 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001059 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001060 sh::IsVarying(elementType.qualifier) &&
1061 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001062 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001063 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001064 TType(elementType).getCompleteString().c_str());
1065 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001066 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001067 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001071void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001072 const ImmutableString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001073 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074{
Olli Etuaho3739d232015-04-08 12:23:44 +03001075 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001076 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001077 {
1078 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001079 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001080
1081 // Generate informative error messages for ESSL1.
1082 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001083 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001084 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001086 "structures containing arrays may not be declared constant since they cannot be "
1087 "initialized",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001088 identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001089 }
1090 else
1091 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001092 error(line, "variables with qualifier 'const' must be initialized", identifier);
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001093 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001094 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001095 // This will make the type sized if it isn't sized yet.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001096 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized", identifier,
1097 type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
Olli Etuaho2935c582015-04-08 14:32:06 +03001100// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101// and update the symbol table.
1102//
Olli Etuaho2935c582015-04-08 14:32:06 +03001103// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001105bool TParseContext::declareVariable(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001106 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001107 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001108 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109{
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111
Olli Etuahofbb1c792018-01-19 16:26:59 +02001112 (*variable) = new TVariable(&symbolTable, identifier, type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02001113
Olli Etuahob60d30f2018-01-16 12:31:06 +02001114 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001115
Olli Etuaho856c4972016-08-08 11:38:39 +03001116 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117
Olli Etuaho2935c582015-04-08 14:32:06 +03001118 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahofbb1c792018-01-19 16:26:59 +02001119 if (type->isArray() && identifier.beginsWith("gl_LastFragData"))
Olli Etuaho2935c582015-04-08 14:32:06 +03001120 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001121 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02001122 symbolTable.findBuiltIn(ImmutableString("gl_MaxDrawBuffers"), mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001123 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001124 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001125 error(line, "redeclaration of gl_LastFragData as an array of arrays", identifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001126 return false;
1127 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001128 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001129 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001131 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001132 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001133 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001134 }
1135 }
1136 else
1137 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001138 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
Olli Etuahofbb1c792018-01-19 16:26:59 +02001139 identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 return false;
1141 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143
Olli Etuaho8a176262016-08-16 14:23:01 +03001144 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001145 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146
Olli Etuaho437664b2018-02-28 15:38:14 +02001147 if (!symbolTable.declare(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001149 error(line, "redefinition", identifier);
Olli Etuaho2935c582015-04-08 14:32:06 +03001150 return false;
1151 }
1152
Olli Etuahob60d30f2018-01-16 12:31:06 +02001153 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 return false;
1155
1156 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157}
1158
Martin Radev70866b82016-07-22 15:27:42 +03001159void TParseContext::checkIsParameterQualifierValid(
1160 const TSourceLoc &line,
1161 const TTypeQualifierBuilder &typeQualifierBuilder,
1162 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301163{
Olli Etuahocce89652017-06-19 16:04:09 +03001164 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001165 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001166
1167 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301168 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001169 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1170 }
1171
1172 if (!IsImage(type->getBasicType()))
1173 {
Olli Etuaho43364892017-02-13 16:00:12 +00001174 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001175 }
1176 else
1177 {
1178 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001180
Martin Radev70866b82016-07-22 15:27:42 +03001181 type->setQualifier(typeQualifier.qualifier);
1182
1183 if (typeQualifier.precision != EbpUndefined)
1184 {
1185 type->setPrecision(typeQualifier.precision);
1186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187}
1188
Olli Etuaho703671e2017-11-08 17:47:18 +02001189template <size_t size>
1190bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1191 const std::array<TExtension, size> &extensions)
1192{
1193 ASSERT(!extensions.empty());
1194 const TExtensionBehavior &extBehavior = extensionBehavior();
1195
1196 bool canUseWithWarning = false;
1197 bool canUseWithoutWarning = false;
1198
1199 const char *errorMsgString = "";
1200 TExtension errorMsgExtension = TExtension::UNDEFINED;
1201
1202 for (TExtension extension : extensions)
1203 {
1204 auto extIter = extBehavior.find(extension);
1205 if (canUseWithWarning)
1206 {
1207 // We already have an extension that we can use, but with a warning.
1208 // See if we can use the alternative extension without a warning.
1209 if (extIter == extBehavior.end())
1210 {
1211 continue;
1212 }
1213 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1214 {
1215 canUseWithoutWarning = true;
1216 break;
1217 }
1218 continue;
1219 }
1220 if (extIter == extBehavior.end())
1221 {
1222 errorMsgString = "extension is not supported";
1223 errorMsgExtension = extension;
1224 }
1225 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1226 {
1227 errorMsgString = "extension is disabled";
1228 errorMsgExtension = extension;
1229 }
1230 else if (extIter->second == EBhWarn)
1231 {
1232 errorMsgExtension = extension;
1233 canUseWithWarning = true;
1234 }
1235 else
1236 {
1237 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1238 canUseWithoutWarning = true;
1239 break;
1240 }
1241 }
1242
1243 if (canUseWithoutWarning)
1244 {
1245 return true;
1246 }
1247 if (canUseWithWarning)
1248 {
1249 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1250 return true;
1251 }
1252 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1253 return false;
1254}
1255
1256template bool TParseContext::checkCanUseOneOfExtensions(
1257 const TSourceLoc &line,
1258 const std::array<TExtension, 1> &extensions);
1259template bool TParseContext::checkCanUseOneOfExtensions(
1260 const TSourceLoc &line,
1261 const std::array<TExtension, 2> &extensions);
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 3> &extensions);
1265
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001266bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001267{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001268 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001269 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270}
1271
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001272// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1273// compile-time or link-time errors are the same whether or not the declaration is empty".
1274// This function implements all the checks that are done on qualifiers regardless of if the
1275// declaration is empty.
1276void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1277 const sh::TLayoutQualifier &layoutQualifier,
1278 const TSourceLoc &location)
1279{
1280 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1281 {
1282 error(location, "Shared memory declarations cannot have layout specified", "layout");
1283 }
1284
1285 if (layoutQualifier.matrixPacking != EmpUnspecified)
1286 {
1287 error(location, "layout qualifier only valid for interface blocks",
1288 getMatrixPackingString(layoutQualifier.matrixPacking));
1289 return;
1290 }
1291
1292 if (layoutQualifier.blockStorage != EbsUnspecified)
1293 {
1294 error(location, "layout qualifier only valid for interface blocks",
1295 getBlockStorageString(layoutQualifier.blockStorage));
1296 return;
1297 }
1298
1299 if (qualifier == EvqFragmentOut)
1300 {
1301 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1302 {
1303 error(location, "invalid layout qualifier combination", "yuv");
1304 return;
1305 }
1306 }
1307 else
1308 {
1309 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1310 }
1311
Olli Etuaho95468d12017-05-04 11:14:34 +03001312 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1313 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001314 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1315 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001316 {
1317 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1318 }
1319
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001320 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001321 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001322 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001323 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001324 // We're not checking whether the uniform location is in range here since that depends on
1325 // the type of the variable.
1326 // The type can only be fully determined for non-empty declarations.
1327 }
1328 if (!canHaveLocation)
1329 {
1330 checkLocationIsNotSpecified(location, layoutQualifier);
1331 }
1332}
1333
jchen104cdac9e2017-05-08 11:01:20 +08001334void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1335 const TSourceLoc &location)
1336{
1337 if (publicType.precision != EbpHigh)
1338 {
1339 error(location, "Can only be highp", "atomic counter");
1340 }
1341 // dEQP enforces compile error if location is specified. See uniform_location.test.
1342 if (publicType.layoutQualifier.location != -1)
1343 {
1344 error(location, "location must not be set for atomic_uint", "layout");
1345 }
1346 if (publicType.layoutQualifier.binding == -1)
1347 {
1348 error(location, "no binding specified", "atomic counter");
1349 }
1350}
1351
Olli Etuaho55bde912017-10-25 13:41:13 +03001352void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001353{
Olli Etuaho55bde912017-10-25 13:41:13 +03001354 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001355 {
1356 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1357 // error. It is assumed that this applies to empty declarations as well.
1358 error(location, "empty array declaration needs to specify a size", "");
1359 }
Martin Radevb8b01222016-11-20 23:25:53 +02001360}
1361
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001362// These checks are done for all declarations that are non-empty. They're done for non-empty
1363// declarations starting a declarator list, and declarators that follow an empty declaration.
1364void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1365 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001366{
Olli Etuahofa33d582015-04-09 14:33:12 +03001367 switch (publicType.qualifier)
1368 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001369 case EvqVaryingIn:
1370 case EvqVaryingOut:
1371 case EvqAttribute:
1372 case EvqVertexIn:
1373 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001374 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001375 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001376 {
1377 error(identifierLocation, "cannot be used with a structure",
1378 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001379 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001380 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001381 break;
1382 case EvqBuffer:
1383 if (publicType.getBasicType() != EbtInterfaceBlock)
1384 {
1385 error(identifierLocation,
1386 "cannot declare buffer variables at global scope(outside a block)",
1387 getQualifierString(publicType.qualifier));
1388 return;
1389 }
1390 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001391 default:
1392 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001393 }
jchen10cc2a10e2017-05-03 14:05:12 +08001394 std::string reason(getBasicString(publicType.getBasicType()));
1395 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001396 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001397 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001398 {
1399 return;
1400 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001401
Andrei Volykhina5527072017-03-22 16:46:30 +03001402 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1403 publicType.qualifier != EvqConst) &&
1404 publicType.getBasicType() == EbtYuvCscStandardEXT)
1405 {
1406 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1407 getQualifierString(publicType.qualifier));
1408 return;
1409 }
1410
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001411 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1412 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001413 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1414 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001415 TType type(publicType);
1416 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001418 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1419 publicType.layoutQualifier);
1420 }
1421 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001422
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001423 // check for layout qualifier issues
1424 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001425
Martin Radev2cc85b32016-08-05 16:22:53 +03001426 if (IsImage(publicType.getBasicType()))
1427 {
1428
1429 switch (layoutQualifier.imageInternalFormat)
1430 {
1431 case EiifRGBA32F:
1432 case EiifRGBA16F:
1433 case EiifR32F:
1434 case EiifRGBA8:
1435 case EiifRGBA8_SNORM:
1436 if (!IsFloatImage(publicType.getBasicType()))
1437 {
1438 error(identifierLocation,
1439 "internal image format requires a floating image type",
1440 getBasicString(publicType.getBasicType()));
1441 return;
1442 }
1443 break;
1444 case EiifRGBA32I:
1445 case EiifRGBA16I:
1446 case EiifRGBA8I:
1447 case EiifR32I:
1448 if (!IsIntegerImage(publicType.getBasicType()))
1449 {
1450 error(identifierLocation,
1451 "internal image format requires an integer image type",
1452 getBasicString(publicType.getBasicType()));
1453 return;
1454 }
1455 break;
1456 case EiifRGBA32UI:
1457 case EiifRGBA16UI:
1458 case EiifRGBA8UI:
1459 case EiifR32UI:
1460 if (!IsUnsignedImage(publicType.getBasicType()))
1461 {
1462 error(identifierLocation,
1463 "internal image format requires an unsigned image type",
1464 getBasicString(publicType.getBasicType()));
1465 return;
1466 }
1467 break;
1468 case EiifUnspecified:
1469 error(identifierLocation, "layout qualifier", "No image internal format specified");
1470 return;
1471 default:
1472 error(identifierLocation, "layout qualifier", "unrecognized token");
1473 return;
1474 }
1475
1476 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1477 switch (layoutQualifier.imageInternalFormat)
1478 {
1479 case EiifR32F:
1480 case EiifR32I:
1481 case EiifR32UI:
1482 break;
1483 default:
1484 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1485 {
1486 error(identifierLocation, "layout qualifier",
1487 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1488 "image variables must be qualified readonly and/or writeonly");
1489 return;
1490 }
1491 break;
1492 }
1493 }
1494 else
1495 {
Olli Etuaho43364892017-02-13 16:00:12 +00001496 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001497 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1498 }
jchen104cdac9e2017-05-08 11:01:20 +08001499
1500 if (IsAtomicCounter(publicType.getBasicType()))
1501 {
1502 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1503 }
1504 else
1505 {
1506 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1507 }
Olli Etuaho43364892017-02-13 16:00:12 +00001508}
Martin Radev2cc85b32016-08-05 16:22:53 +03001509
Olli Etuaho43364892017-02-13 16:00:12 +00001510void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1511{
1512 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001513 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1514 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1515 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1516 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1517 // when it comes to which shaders are accepted by the compiler.
1518 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001519 if (IsImage(type.getBasicType()))
1520 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001521 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1522 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001523 }
1524 else if (IsSampler(type.getBasicType()))
1525 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001526 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1527 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001528 }
jchen104cdac9e2017-05-08 11:01:20 +08001529 else if (IsAtomicCounter(type.getBasicType()))
1530 {
1531 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1532 }
Olli Etuaho43364892017-02-13 16:00:12 +00001533 else
1534 {
1535 ASSERT(!IsOpaqueType(type.getBasicType()));
1536 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001537 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001538}
1539
Olli Etuaho856c4972016-08-08 11:38:39 +03001540void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001541 const ImmutableString &layoutQualifierName,
Olli Etuaho856c4972016-08-08 11:38:39 +03001542 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001543{
1544
1545 if (mShaderVersion < versionRequired)
1546 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001547 error(location, "invalid layout qualifier: not supported", layoutQualifierName);
Martin Radev802abe02016-08-04 17:48:32 +03001548 }
1549}
1550
Olli Etuaho856c4972016-08-08 11:38:39 +03001551bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1552 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001553{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001554 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001555 for (size_t i = 0u; i < localSize.size(); ++i)
1556 {
1557 if (localSize[i] != -1)
1558 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001559 error(location,
1560 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1561 "global layout declaration",
1562 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001563 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001564 }
1565 }
1566
Olli Etuaho8a176262016-08-16 14:23:01 +03001567 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001568}
1569
Olli Etuaho43364892017-02-13 16:00:12 +00001570void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001571 TLayoutImageInternalFormat internalFormat)
1572{
1573 if (internalFormat != EiifUnspecified)
1574 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001575 error(location, "invalid layout qualifier: only valid when used with images",
1576 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001577 }
Olli Etuaho43364892017-02-13 16:00:12 +00001578}
1579
1580void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1581{
1582 if (binding != -1)
1583 {
1584 error(location,
1585 "invalid layout qualifier: only valid when used with opaque types or blocks",
1586 "binding");
1587 }
1588}
1589
jchen104cdac9e2017-05-08 11:01:20 +08001590void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1591{
1592 if (offset != -1)
1593 {
1594 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1595 "offset");
1596 }
1597}
1598
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001599void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1600 int binding,
1601 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001602{
1603 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001604 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001605 {
1606 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1607 }
1608}
1609
1610void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1611 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001612 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001613{
1614 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001615 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001616 {
1617 error(location, "sampler binding greater than maximum texture units", "binding");
1618 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001619}
1620
Jiajia Qinbc585152017-06-23 15:42:17 +08001621void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1622 const TQualifier &qualifier,
1623 int binding,
1624 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001625{
1626 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001627 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001628 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001629 if (binding + size > mMaxUniformBufferBindings)
1630 {
1631 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1632 "binding");
1633 }
1634 }
1635 else if (qualifier == EvqBuffer)
1636 {
1637 if (binding + size > mMaxShaderStorageBufferBindings)
1638 {
1639 error(location,
1640 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1641 "binding");
1642 }
jchen10af713a22017-04-19 09:10:56 +08001643 }
1644}
jchen104cdac9e2017-05-08 11:01:20 +08001645void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1646{
1647 if (binding >= mMaxAtomicCounterBindings)
1648 {
1649 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1650 "binding");
1651 }
1652}
jchen10af713a22017-04-19 09:10:56 +08001653
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001654void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1655 int objectLocationCount,
1656 const TLayoutQualifier &layoutQualifier)
1657{
1658 int loc = layoutQualifier.location;
1659 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1660 {
1661 error(location, "Uniform location out of range", "location");
1662 }
1663}
1664
Andrei Volykhina5527072017-03-22 16:46:30 +03001665void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1666{
1667 if (yuv != false)
1668 {
1669 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1670 }
1671}
1672
Jiajia Qinbc585152017-06-23 15:42:17 +08001673void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1674 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001675{
1676 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1677 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001678 TQualifier qual = fnCandidate->getParam(i)->getType().getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001679 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1680 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1681 qual == EvqInOut || qual == EvqConstReadOnly))
1682 {
1683 if (argument->getMemoryQualifier().writeonly)
1684 {
1685 error(argument->getLine(),
1686 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001687 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001688 return;
1689 }
1690 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001691 if (qual == EvqOut || qual == EvqInOut)
1692 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001693 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001694 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001695 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001696 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001697 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001698 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001699 }
1700 }
1701 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001702}
1703
Martin Radev70866b82016-07-22 15:27:42 +03001704void TParseContext::checkInvariantVariableQualifier(bool invariant,
1705 const TQualifier qualifier,
1706 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001707{
Martin Radev70866b82016-07-22 15:27:42 +03001708 if (!invariant)
1709 return;
1710
1711 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001712 {
Martin Radev70866b82016-07-22 15:27:42 +03001713 // input variables in the fragment shader can be also qualified as invariant
1714 if (!sh::CanBeInvariantESSL1(qualifier))
1715 {
1716 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1717 }
1718 }
1719 else
1720 {
1721 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1722 {
1723 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1724 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001725 }
1726}
1727
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001728bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001729{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001730 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001731}
1732
Jamie Madillb98c3a82015-07-23 14:26:04 -04001733void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1734 const char *extName,
1735 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001736{
1737 pp::SourceLocation srcLoc;
1738 srcLoc.file = loc.first_file;
1739 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001740 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001741}
1742
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1744 const char *name,
1745 const char *value,
1746 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001747{
1748 pp::SourceLocation srcLoc;
1749 srcLoc.file = loc.first_file;
1750 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001751 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001752}
1753
Martin Radev4c4c8e72016-08-04 12:25:34 +03001754sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001755{
Jamie Madill2f294c92017-11-20 14:47:26 -05001756 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001757 for (size_t i = 0u; i < result.size(); ++i)
1758 {
1759 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1760 {
1761 result[i] = 1;
1762 }
1763 else
1764 {
1765 result[i] = mComputeShaderLocalSize[i];
1766 }
1767 }
1768 return result;
1769}
1770
Olli Etuaho56229f12017-07-10 14:16:33 +03001771TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1772 const TSourceLoc &line)
1773{
1774 TIntermConstantUnion *node = new TIntermConstantUnion(
1775 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1776 node->setLine(line);
1777 return node;
1778}
1779
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001780/////////////////////////////////////////////////////////////////////////////////
1781//
1782// Non-Errors.
1783//
1784/////////////////////////////////////////////////////////////////////////////////
1785
Jamie Madill5c097022014-08-20 16:38:32 -04001786const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001787 const ImmutableString &name,
Jamie Madill5c097022014-08-20 16:38:32 -04001788 const TSymbol *symbol)
1789{
Jamie Madill5c097022014-08-20 16:38:32 -04001790 if (!symbol)
1791 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001792 error(location, "undeclared identifier", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001793 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001794 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001795
1796 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001797 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001798 error(location, "variable expected", name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001799 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001800 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001801
1802 const TVariable *variable = static_cast<const TVariable *>(symbol);
1803
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001804 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001805 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001806 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001807 }
1808
Olli Etuaho0f684632017-07-13 12:42:15 +03001809 // Reject shaders using both gl_FragData and gl_FragColor
1810 TQualifier qualifier = variable->getType().getQualifier();
1811 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001812 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001813 mUsesFragData = true;
1814 }
1815 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1816 {
1817 mUsesFragColor = true;
1818 }
1819 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1820 {
1821 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001822 }
1823
Olli Etuaho0f684632017-07-13 12:42:15 +03001824 // This validation is not quite correct - it's only an error to write to
1825 // both FragData and FragColor. For simplicity, and because users shouldn't
1826 // be rewarded for reading from undefined varaibles, return an error
1827 // if they are both referenced, rather than assigned.
1828 if (mUsesFragData && mUsesFragColor)
1829 {
1830 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1831 if (mUsesSecondaryOutputs)
1832 {
1833 errorMessage =
1834 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1835 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1836 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02001837 error(location, errorMessage, name);
Olli Etuaho0f684632017-07-13 12:42:15 +03001838 }
1839
1840 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1841 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1842 qualifier == EvqWorkGroupSize)
1843 {
1844 error(location,
1845 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1846 "gl_WorkGroupSize");
1847 }
Jamie Madill5c097022014-08-20 16:38:32 -04001848 return variable;
1849}
1850
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001851TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001852 const ImmutableString &name,
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001853 const TSymbol *symbol)
1854{
1855 const TVariable *variable = getNamedVariable(location, name, symbol);
1856
Olli Etuaho0f684632017-07-13 12:42:15 +03001857 if (!variable)
1858 {
1859 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1860 node->setLine(location);
1861 return node;
1862 }
1863
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001864 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001865 TIntermTyped *node = nullptr;
1866
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001867 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001868 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001869 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001871 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001872 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001873 {
1874 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1875 // needs to be added to the AST as a constant and not as a symbol.
1876 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1877 TConstantUnion *constArray = new TConstantUnion[3];
1878 for (size_t i = 0; i < 3; ++i)
1879 {
1880 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1881 }
1882
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001883 ASSERT(variableType.getBasicType() == EbtUInt);
1884 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001885
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001886 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001887 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001888 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001889 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1891 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001892 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001893 ASSERT(mGlInVariableWithArraySize != nullptr);
1894 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001895 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 else
1897 {
Olli Etuaho195be942017-12-04 23:40:14 +02001898 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001899 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001900 ASSERT(node != nullptr);
1901 node->setLine(location);
1902 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903}
1904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905// Initializers show up in several places in the grammar. Have one set of
1906// code to handle them here.
1907//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001908// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001909bool TParseContext::executeInitializer(const TSourceLoc &line,
Olli Etuahofbb1c792018-01-19 16:26:59 +02001910 const ImmutableString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001911 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 ASSERT(initNode != nullptr);
1916 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
Olli Etuahob60d30f2018-01-16 12:31:06 +02001918 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001919 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001920 // In case initializer is not an array or type has more dimensions than initializer, this
1921 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1922 // actually is an array or not. Having a non-array initializer for an unsized array will
1923 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001924 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001925 type->sizeUnsizedArrays(arraySizes);
1926 }
1927
1928 const TQualifier qualifier = type->getQualifier();
1929
1930 bool constError = false;
1931 if (qualifier == EvqConst)
1932 {
1933 if (EvqConst != initializer->getType().getQualifier())
1934 {
1935 std::stringstream reasonStream;
1936 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1937 std::string reason = reasonStream.str();
1938 error(line, reason.c_str(), "=");
1939
1940 // We're still going to declare the variable to avoid extra error messages.
1941 type->setQualifier(EvqTemporary);
1942 constError = true;
1943 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001944 }
Olli Etuaho195be942017-12-04 23:40:14 +02001945
1946 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001947 if (!declareVariable(line, identifier, type, &variable))
1948 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001949 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951
Olli Etuahob60d30f2018-01-16 12:31:06 +02001952 if (constError)
1953 {
1954 return false;
1955 }
1956
Olli Etuahob0c645e2015-05-12 14:25:36 +03001957 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001958 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001959 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001960 {
1961 // Error message does not completely match behavior with ESSL 1.00, but
1962 // we want to steer developers towards only using constant expressions.
1963 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001964 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001965 }
1966 if (globalInitWarning)
1967 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001968 warning(
1969 line,
1970 "global variable initializers should be constant expressions "
1971 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1972 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001973 }
1974
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001975 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301976 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1977 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001978 error(line, " cannot initialize this type of qualifier ",
1979 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001980 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001982
1983 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1984 intermSymbol->setLine(line);
1985
1986 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1987 {
1988 assignError(line, "=", variable->getType().getCompleteString(),
1989 initializer->getCompleteString());
1990 return false;
1991 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
Arun Patole7e7e68d2015-05-22 12:02:25 +05301993 if (qualifier == EvqConst)
1994 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001995 // Save the constant folded value to the variable if possible.
1996 const TConstantUnion *constArray = initializer->getConstantValue();
1997 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301998 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001999 variable->shareConstPointer(constArray);
2000 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002001 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002002 ASSERT(*initNode == nullptr);
2003 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002004 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002005 }
2006 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002007
Olli Etuahob60d30f2018-01-16 12:31:06 +02002008 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
2009 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002010 return true;
2011}
2012
2013TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002014 const ImmutableString &identifier,
Olli Etuaho914b79a2017-06-19 16:03:19 +03002015 TIntermTyped *initializer,
2016 const TSourceLoc &loc)
2017{
2018 checkIsScalarBool(loc, pType);
2019 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002020 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002021 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 {
2023 // The initializer is valid. The init condition needs to have a node - either the
2024 // initializer node, or a constant node in case the initialized variable is const and won't
2025 // be recorded in the AST.
2026 if (initNode == nullptr)
2027 {
2028 return initializer;
2029 }
2030 else
2031 {
2032 TIntermDeclaration *declaration = new TIntermDeclaration();
2033 declaration->appendDeclarator(initNode);
2034 return declaration;
2035 }
2036 }
2037 return nullptr;
2038}
2039
2040TIntermNode *TParseContext::addLoop(TLoopType type,
2041 TIntermNode *init,
2042 TIntermNode *cond,
2043 TIntermTyped *expr,
2044 TIntermNode *body,
2045 const TSourceLoc &line)
2046{
2047 TIntermNode *node = nullptr;
2048 TIntermTyped *typedCond = nullptr;
2049 if (cond)
2050 {
2051 typedCond = cond->getAsTyped();
2052 }
2053 if (cond == nullptr || typedCond)
2054 {
Olli Etuahocce89652017-06-19 16:04:09 +03002055 if (type == ELoopDoWhile)
2056 {
2057 checkIsScalarBool(line, typedCond);
2058 }
2059 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2060 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2061 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2062 !typedCond->isVector()));
2063
Olli Etuaho3ec75682017-07-05 17:02:55 +03002064 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002065 node->setLine(line);
2066 return node;
2067 }
2068
Olli Etuahocce89652017-06-19 16:04:09 +03002069 ASSERT(type != ELoopDoWhile);
2070
Olli Etuaho914b79a2017-06-19 16:03:19 +03002071 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2072 ASSERT(declaration);
2073 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2074 ASSERT(declarator->getLeft()->getAsSymbolNode());
2075
2076 // The condition is a declaration. In the AST representation we don't support declarations as
2077 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2078 // the loop.
2079 TIntermBlock *block = new TIntermBlock();
2080
2081 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2082 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2083 block->appendStatement(declareCondition);
2084
2085 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2086 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002087 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002088 block->appendStatement(loop);
2089 loop->setLine(line);
2090 block->setLine(line);
2091 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092}
2093
Olli Etuahocce89652017-06-19 16:04:09 +03002094TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2095 TIntermNodePair code,
2096 const TSourceLoc &loc)
2097{
Olli Etuaho56229f12017-07-10 14:16:33 +03002098 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002099
2100 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002101 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002102 {
2103 if (cond->getAsConstantUnion()->getBConst(0) == true)
2104 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002105 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002106 }
2107 else
2108 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002109 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002110 }
2111 }
2112
Olli Etuaho3ec75682017-07-05 17:02:55 +03002113 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002114 node->setLine(loc);
2115
2116 return node;
2117}
2118
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002119void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2120{
2121 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2122 typeSpecifier->getBasicType());
2123
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002124 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002125 {
2126 error(typeSpecifier->getLine(), "not supported", "first-class array");
2127 typeSpecifier->clearArrayness();
2128 }
2129}
2130
Martin Radev70866b82016-07-22 15:27:42 +03002131TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302132 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002133{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002134 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002135
Martin Radev70866b82016-07-22 15:27:42 +03002136 TPublicType returnType = typeSpecifier;
2137 returnType.qualifier = typeQualifier.qualifier;
2138 returnType.invariant = typeQualifier.invariant;
2139 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002140 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002141 returnType.precision = typeSpecifier.precision;
2142
2143 if (typeQualifier.precision != EbpUndefined)
2144 {
2145 returnType.precision = typeQualifier.precision;
2146 }
2147
Martin Radev4a9cd802016-09-01 16:51:51 +03002148 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2149 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002150
Martin Radev4a9cd802016-09-01 16:51:51 +03002151 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2152 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002153
Martin Radev4a9cd802016-09-01 16:51:51 +03002154 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002155
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002156 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002157 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002158 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002159 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002161 returnType.clearArrayness();
2162 }
2163
Martin Radev70866b82016-07-22 15:27:42 +03002164 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002166 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002167 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002168 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002169 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002170
Martin Radev70866b82016-07-22 15:27:42 +03002171 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002172 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002173 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002174 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002175 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002176 }
2177 }
2178 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002179 {
Martin Radev70866b82016-07-22 15:27:42 +03002180 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002181 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002182 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002183 }
Martin Radev70866b82016-07-22 15:27:42 +03002184 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2185 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002186 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002187 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2188 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002189 }
Martin Radev70866b82016-07-22 15:27:42 +03002190 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002191 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002192 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002193 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002194 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002195 }
2196
2197 return returnType;
2198}
2199
Olli Etuaho856c4972016-08-08 11:38:39 +03002200void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2201 const TPublicType &type,
2202 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002203{
2204 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002205 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002206 {
2207 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002208 }
2209
2210 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2211 switch (qualifier)
2212 {
2213 case EvqVertexIn:
2214 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002215 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002216 {
2217 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002218 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002219 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 return;
2221 case EvqFragmentOut:
2222 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002223 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 {
2225 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002227 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 return;
2229 default:
2230 break;
2231 }
2232
2233 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2234 // restrictions.
2235 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002236 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2237 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2239 {
2240 error(qualifierLocation, "must use 'flat' interpolation here",
2241 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 }
2243
Martin Radev4a9cd802016-09-01 16:51:51 +03002244 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002245 {
2246 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2247 // These restrictions are only implied by the ESSL 3.00 spec, but
2248 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002249 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002250 {
2251 error(qualifierLocation, "cannot be an array of structures",
2252 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 }
2254 if (type.isStructureContainingArrays())
2255 {
2256 error(qualifierLocation, "cannot be a structure containing an array",
2257 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002258 }
2259 if (type.isStructureContainingType(EbtStruct))
2260 {
2261 error(qualifierLocation, "cannot be a structure containing a structure",
2262 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002263 }
2264 if (type.isStructureContainingType(EbtBool))
2265 {
2266 error(qualifierLocation, "cannot be a structure containing a bool",
2267 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002268 }
2269 }
2270}
2271
Martin Radev2cc85b32016-08-05 16:22:53 +03002272void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2273{
2274 if (qualifier.getType() == QtStorage)
2275 {
2276 const TStorageQualifierWrapper &storageQualifier =
2277 static_cast<const TStorageQualifierWrapper &>(qualifier);
2278 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2279 !symbolTable.atGlobalLevel())
2280 {
2281 error(storageQualifier.getLine(),
2282 "Local variables can only use the const storage qualifier.",
2283 storageQualifier.getQualifierString().c_str());
2284 }
2285 }
2286}
2287
Olli Etuaho43364892017-02-13 16:00:12 +00002288void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002289 const TSourceLoc &location)
2290{
Jiajia Qinbc585152017-06-23 15:42:17 +08002291 const std::string reason(
2292 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2293 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002294 if (memoryQualifier.readonly)
2295 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002296 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002297 }
2298 if (memoryQualifier.writeonly)
2299 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002300 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002301 }
Martin Radev049edfa2016-11-11 14:35:37 +02002302 if (memoryQualifier.coherent)
2303 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002304 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002305 }
2306 if (memoryQualifier.restrictQualifier)
2307 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002309 }
2310 if (memoryQualifier.volatileQualifier)
2311 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002312 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002313 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002314}
2315
jchen104cdac9e2017-05-08 11:01:20 +08002316// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2317// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002318void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2319 const TSourceLoc &loc,
2320 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002321{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002322 if (!IsAtomicCounter(type->getBasicType()))
2323 {
2324 return;
2325 }
2326
2327 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2328 : kAtomicCounterSize;
2329 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2330 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002331 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002332 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002333 {
2334 offset = bindingState.appendSpan(size);
2335 }
2336 else
2337 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002338 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002339 }
2340 if (offset == -1)
2341 {
2342 error(loc, "Offset overlapping", "atomic counter");
2343 return;
2344 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002345 layoutQualifier.offset = offset;
2346 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002347}
2348
Olli Etuaho454c34c2017-10-25 16:35:56 +03002349void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002350 const ImmutableString &token,
Olli Etuaho454c34c2017-10-25 16:35:56 +03002351 TType *type)
2352{
2353 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2354 {
2355 if (type->isArray() && type->getOutermostArraySize() == 0u)
2356 {
2357 // Set size for the unsized geometry shader inputs if they are declared after a valid
2358 // input primitive declaration.
2359 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2360 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002361 ASSERT(mGlInVariableWithArraySize != nullptr);
2362 type->sizeOutermostUnsizedArray(
2363 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002364 }
2365 else
2366 {
2367 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2368 // An input can be declared without an array size if there is a previous layout
2369 // which specifies the size.
2370 error(location,
2371 "Missing a valid input primitive declaration before declaring an unsized "
2372 "array input",
2373 token);
2374 }
2375 }
2376 else if (type->isArray())
2377 {
2378 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2379 }
2380 else
2381 {
2382 error(location, "Geometry shader input variable must be declared as an array", token);
2383 }
2384 }
2385}
2386
Olli Etuaho13389b62016-10-16 11:48:18 +01002387TIntermDeclaration *TParseContext::parseSingleDeclaration(
2388 TPublicType &publicType,
2389 const TSourceLoc &identifierOrTypeLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002390 const ImmutableString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002391{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002392 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002393 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2394 mDirectiveHandler.pragma().stdgl.invariantAll)
2395 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002396 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002397
2398 // The directive handler has already taken care of rejecting invalid uses of this pragma
2399 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2400 // affected variable declarations:
2401 //
2402 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2403 // elsewhere, in TranslatorGLSL.)
2404 //
2405 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2406 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2407 // the way this is currently implemented we have to enable this compiler option before
2408 // parsing the shader and determining the shading language version it uses. If this were
2409 // implemented as a post-pass, the workaround could be more targeted.
2410 //
2411 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2412 // the specification, but there are desktop OpenGL drivers that expect that this is the
2413 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2414 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2415 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002416 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002417 }
2418 }
2419
Olli Etuahofbb1c792018-01-19 16:26:59 +02002420 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier, type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002421
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002422 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2423 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002424
Olli Etuahobab4c082015-04-24 16:38:49 +03002425 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002426 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002428 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002429 if (emptyDeclaration)
2430 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002431 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002432 // In most cases we don't need to create a symbol node for an empty declaration.
2433 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002434 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 {
Olli Etuaho195be942017-12-04 23:40:14 +02002436 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002437 new TVariable(&symbolTable, ImmutableString(""), type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002438 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 }
jchen104cdac9e2017-05-08 11:01:20 +08002440 else if (IsAtomicCounter(publicType.getBasicType()))
2441 {
2442 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2443 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002444 }
2445 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002446 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002448
Olli Etuahob60d30f2018-01-16 12:31:06 +02002449 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002450
Olli Etuahob60d30f2018-01-16 12:31:06 +02002451 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002452
Olli Etuaho2935c582015-04-08 14:32:06 +03002453 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002454 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002455 {
Olli Etuaho195be942017-12-04 23:40:14 +02002456 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002458 }
2459
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002460 TIntermDeclaration *declaration = new TIntermDeclaration();
2461 declaration->setLine(identifierOrTypeLocation);
2462 if (symbol)
2463 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002464 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002465 declaration->appendDeclarator(symbol);
2466 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002467 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002468}
2469
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002470TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2471 TPublicType &elementType,
2472 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002473 const ImmutableString &identifier,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002474 const TSourceLoc &indexLocation,
2475 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002476{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002477 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002478
Olli Etuaho55bde912017-10-25 13:41:13 +03002479 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002480 identifierLocation);
2481
Olli Etuaho55bde912017-10-25 13:41:13 +03002482 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002483
Olli Etuaho55bde912017-10-25 13:41:13 +03002484 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002485
Olli Etuahob60d30f2018-01-16 12:31:06 +02002486 TType *arrayType = new TType(elementType);
2487 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002488
Olli Etuahofbb1c792018-01-19 16:26:59 +02002489 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002490
Olli Etuahob60d30f2018-01-16 12:31:06 +02002491 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002492
Olli Etuahob60d30f2018-01-16 12:31:06 +02002493 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002494
Olli Etuaho13389b62016-10-16 11:48:18 +01002495 TIntermDeclaration *declaration = new TIntermDeclaration();
2496 declaration->setLine(identifierLocation);
2497
Olli Etuaho195be942017-12-04 23:40:14 +02002498 TVariable *variable = nullptr;
2499 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002500 {
Olli Etuaho195be942017-12-04 23:40:14 +02002501 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002502 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002503 declaration->appendDeclarator(symbol);
2504 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002505
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002507}
2508
Olli Etuaho13389b62016-10-16 11:48:18 +01002509TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2510 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002511 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002512 const TSourceLoc &initLocation,
2513 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002514{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002515 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002516
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002517 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2518 identifierLocation);
2519
2520 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522 TIntermDeclaration *declaration = new TIntermDeclaration();
2523 declaration->setLine(identifierLocation);
2524
2525 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002526 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002527 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002528 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002529 if (initNode)
2530 {
2531 declaration->appendDeclarator(initNode);
2532 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002533 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002534 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002535}
2536
Olli Etuaho13389b62016-10-16 11:48:18 +01002537TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002538 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002539 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002540 const ImmutableString &identifier,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002541 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002542 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 const TSourceLoc &initLocation,
2544 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002545{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002546 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002547
Olli Etuaho55bde912017-10-25 13:41:13 +03002548 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002549 identifierLocation);
2550
Olli Etuaho55bde912017-10-25 13:41:13 +03002551 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552
Olli Etuaho55bde912017-10-25 13:41:13 +03002553 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002554
Olli Etuahob60d30f2018-01-16 12:31:06 +02002555 TType *arrayType = new TType(elementType);
2556 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002557
Olli Etuaho13389b62016-10-16 11:48:18 +01002558 TIntermDeclaration *declaration = new TIntermDeclaration();
2559 declaration->setLine(identifierLocation);
2560
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002561 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002562 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002563 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002565 if (initNode)
2566 {
2567 declaration->appendDeclarator(initNode);
2568 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002570
2571 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572}
2573
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002574TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002575 const TTypeQualifierBuilder &typeQualifierBuilder,
2576 const TSourceLoc &identifierLoc,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002577 const ImmutableString &identifier,
Martin Radev70866b82016-07-22 15:27:42 +03002578 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002579{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002580 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002581
Martin Radev70866b82016-07-22 15:27:42 +03002582 if (!typeQualifier.invariant)
2583 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002584 error(identifierLoc, "Expected invariant", identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002585 return nullptr;
2586 }
2587 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2588 {
2589 return nullptr;
2590 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002591 if (!symbol)
2592 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002593 error(identifierLoc, "undeclared identifier declared as invariant", identifier);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002594 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002595 }
Martin Radev70866b82016-07-22 15:27:42 +03002596 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597 {
Martin Radev70866b82016-07-22 15:27:42 +03002598 error(identifierLoc, "invariant declaration specifies qualifier",
2599 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002600 }
Martin Radev70866b82016-07-22 15:27:42 +03002601 if (typeQualifier.precision != EbpUndefined)
2602 {
2603 error(identifierLoc, "invariant declaration specifies precision",
2604 getPrecisionString(typeQualifier.precision));
2605 }
2606 if (!typeQualifier.layoutQualifier.isEmpty())
2607 {
2608 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2609 }
2610
2611 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002612 if (!variable)
2613 {
2614 return nullptr;
2615 }
Martin Radev70866b82016-07-22 15:27:42 +03002616 const TType &type = variable->getType();
2617
2618 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2619 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002620 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002621
Olli Etuahodefe3932018-02-13 11:56:09 +02002622 symbolTable.addInvariantVarying(identifier);
Martin Radev70866b82016-07-22 15:27:42 +03002623
Olli Etuaho195be942017-12-04 23:40:14 +02002624 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002625 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002626
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002627 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002628}
2629
Olli Etuaho13389b62016-10-16 11:48:18 +01002630void TParseContext::parseDeclarator(TPublicType &publicType,
2631 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002632 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002633 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002634{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002635 // If the declaration starting this declarator list was empty (example: int,), some checks were
2636 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002637 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002638 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002639 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2640 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002641 }
2642
Olli Etuaho856c4972016-08-08 11:38:39 +03002643 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002644
Olli Etuahob60d30f2018-01-16 12:31:06 +02002645 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002646
Olli Etuahofbb1c792018-01-19 16:26:59 +02002647 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002648
Olli Etuahob60d30f2018-01-16 12:31:06 +02002649 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002650
Olli Etuahob60d30f2018-01-16 12:31:06 +02002651 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002652
Olli Etuaho195be942017-12-04 23:40:14 +02002653 TVariable *variable = nullptr;
2654 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002655 {
Olli Etuaho195be942017-12-04 23:40:14 +02002656 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002657 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002658 declarationOut->appendDeclarator(symbol);
2659 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002660}
2661
Olli Etuaho55bde912017-10-25 13:41:13 +03002662void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002663 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002664 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002665 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002666 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002667 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002668{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002669 // If the declaration starting this declarator list was empty (example: int,), some checks were
2670 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002671 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002672 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002673 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002674 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002675 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002676
Olli Etuaho55bde912017-10-25 13:41:13 +03002677 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002678
Olli Etuaho55bde912017-10-25 13:41:13 +03002679 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002680 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002681 TType *arrayType = new TType(elementType);
2682 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002683
Olli Etuahofbb1c792018-01-19 16:26:59 +02002684 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier, arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002685
Olli Etuahob60d30f2018-01-16 12:31:06 +02002686 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002687
Olli Etuahob60d30f2018-01-16 12:31:06 +02002688 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002689
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002690 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002691 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002692 {
Olli Etuaho195be942017-12-04 23:40:14 +02002693 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002694 symbol->setLine(identifierLocation);
2695 declarationOut->appendDeclarator(symbol);
2696 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002697 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002698}
2699
Olli Etuaho13389b62016-10-16 11:48:18 +01002700void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2701 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002702 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002703 const TSourceLoc &initLocation,
2704 TIntermTyped *initializer,
2705 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002706{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002707 // If the declaration starting this declarator list was empty (example: int,), some checks were
2708 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002709 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002710 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002711 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2712 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002713 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002714
Olli Etuaho856c4972016-08-08 11:38:39 +03002715 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002716
Olli Etuaho13389b62016-10-16 11:48:18 +01002717 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002718 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002719 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002720 {
2721 //
2722 // build the intermediate representation
2723 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002724 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002725 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002726 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002727 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002728 }
2729}
2730
Olli Etuaho55bde912017-10-25 13:41:13 +03002731void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 const TSourceLoc &identifierLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02002733 const ImmutableString &identifier,
Olli Etuaho13389b62016-10-16 11:48:18 +01002734 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002735 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002736 const TSourceLoc &initLocation,
2737 TIntermTyped *initializer,
2738 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002739{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002740 // If the declaration starting this declarator list was empty (example: int,), some checks were
2741 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002742 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002743 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002744 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002745 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002746 }
2747
Olli Etuaho55bde912017-10-25 13:41:13 +03002748 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002749
Olli Etuaho55bde912017-10-25 13:41:13 +03002750 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002751
Olli Etuahob60d30f2018-01-16 12:31:06 +02002752 TType *arrayType = new TType(elementType);
2753 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754
2755 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002756 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002757 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002758 {
2759 if (initNode)
2760 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002761 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002763 }
2764}
2765
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002766TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2767{
2768 // It's simpler to parse an empty statement as a constant expression rather than having a
2769 // different type of node just for empty statements, that will be pruned from the AST anyway.
2770 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2771 node->setLine(location);
2772 return node;
2773}
2774
jchen104cdac9e2017-05-08 11:01:20 +08002775void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2776 const TSourceLoc &location)
2777{
2778 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2779 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2780 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2781 {
2782 error(location, "Requires both binding and offset", "layout");
2783 return;
2784 }
2785 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2786}
2787
Olli Etuahocce89652017-06-19 16:04:09 +03002788void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2789 const TPublicType &type,
2790 const TSourceLoc &loc)
2791{
2792 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2793 !getFragmentPrecisionHigh())
2794 {
2795 error(loc, "precision is not supported in fragment shader", "highp");
2796 }
2797
2798 if (!CanSetDefaultPrecisionOnType(type))
2799 {
2800 error(loc, "illegal type argument for default precision qualifier",
2801 getBasicString(type.getBasicType()));
2802 return;
2803 }
2804 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2805}
2806
Shaob5cc1192017-07-06 10:47:20 +08002807bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2808{
2809 switch (typeQualifier.layoutQualifier.primitiveType)
2810 {
2811 case EptLines:
2812 case EptLinesAdjacency:
2813 case EptTriangles:
2814 case EptTrianglesAdjacency:
2815 return typeQualifier.qualifier == EvqGeometryIn;
2816
2817 case EptLineStrip:
2818 case EptTriangleStrip:
2819 return typeQualifier.qualifier == EvqGeometryOut;
2820
2821 case EptPoints:
2822 return true;
2823
2824 default:
2825 UNREACHABLE();
2826 return false;
2827 }
2828}
2829
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002830void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2831 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002832{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002833 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002834 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02002835 const TSymbol *glPerVertex = symbolTable.findBuiltIn(ImmutableString("gl_PerVertex"), 310);
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002836 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002837 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2838 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002839 mGlInVariableWithArraySize =
Olli Etuahofbb1c792018-01-19 16:26:59 +02002840 new TVariable(&symbolTable, ImmutableString("gl_in"), glInType, SymbolType::BuiltIn,
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002841 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002842 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002843 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002844 {
2845 error(line,
2846 "Array size or input primitive declaration doesn't match the size of earlier sized "
2847 "array inputs.",
2848 "layout");
2849 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002850}
2851
Shaob5cc1192017-07-06 10:47:20 +08002852bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2853{
2854 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2855
2856 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2857
2858 if (layoutQualifier.maxVertices != -1)
2859 {
2860 error(typeQualifier.line,
2861 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2862 return false;
2863 }
2864
2865 // Set mGeometryInputPrimitiveType if exists
2866 if (layoutQualifier.primitiveType != EptUndefined)
2867 {
2868 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2869 {
2870 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2871 return false;
2872 }
2873
2874 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2875 {
2876 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002877 setGeometryShaderInputArraySize(
2878 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2879 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002880 }
2881 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2882 {
2883 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2884 "layout");
2885 return false;
2886 }
2887 }
2888
2889 // Set mGeometryInvocations if exists
2890 if (layoutQualifier.invocations > 0)
2891 {
2892 if (mGeometryShaderInvocations == 0)
2893 {
2894 mGeometryShaderInvocations = layoutQualifier.invocations;
2895 }
2896 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2897 {
2898 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2899 "layout");
2900 return false;
2901 }
2902 }
2903
2904 return true;
2905}
2906
2907bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2908{
2909 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2910
2911 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2912
2913 if (layoutQualifier.invocations > 0)
2914 {
2915 error(typeQualifier.line,
2916 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2917 return false;
2918 }
2919
2920 // Set mGeometryOutputPrimitiveType if exists
2921 if (layoutQualifier.primitiveType != EptUndefined)
2922 {
2923 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2924 {
2925 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2926 return false;
2927 }
2928
2929 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2930 {
2931 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2932 }
2933 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2934 {
2935 error(typeQualifier.line,
2936 "primitive doesn't match earlier output primitive declaration", "layout");
2937 return false;
2938 }
2939 }
2940
2941 // Set mGeometryMaxVertices if exists
2942 if (layoutQualifier.maxVertices > -1)
2943 {
2944 if (mGeometryShaderMaxVertices == -1)
2945 {
2946 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2947 }
2948 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2949 {
2950 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2951 "layout");
2952 return false;
2953 }
2954 }
2955
2956 return true;
2957}
2958
Martin Radev70866b82016-07-22 15:27:42 +03002959void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002960{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002961 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002962 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002963
Martin Radev70866b82016-07-22 15:27:42 +03002964 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2965 typeQualifier.line);
2966
Jamie Madillc2128ff2016-07-04 10:26:17 -04002967 // It should never be the case, but some strange parser errors can send us here.
2968 if (layoutQualifier.isEmpty())
2969 {
2970 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002971 return;
2972 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002973
Martin Radev802abe02016-08-04 17:48:32 +03002974 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002975 {
Olli Etuaho43364892017-02-13 16:00:12 +00002976 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002977 return;
2978 }
2979
Olli Etuaho43364892017-02-13 16:00:12 +00002980 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2981
2982 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002983
2984 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2985
Andrei Volykhina5527072017-03-22 16:46:30 +03002986 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2987
jchen104cdac9e2017-05-08 11:01:20 +08002988 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2989
Qin Jiajiaca68d982017-09-18 16:41:56 +08002990 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2991 typeQualifier.qualifier);
2992
Martin Radev802abe02016-08-04 17:48:32 +03002993 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002994 {
Martin Radev802abe02016-08-04 17:48:32 +03002995 if (mComputeShaderLocalSizeDeclared &&
2996 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2997 {
2998 error(typeQualifier.line, "Work group size does not match the previous declaration",
2999 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003000 return;
3001 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003002
Martin Radev802abe02016-08-04 17:48:32 +03003003 if (mShaderVersion < 310)
3004 {
3005 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003006 return;
3007 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003008
Martin Radev4c4c8e72016-08-04 12:25:34 +03003009 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003010 {
3011 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003012 return;
3013 }
3014
3015 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003016 symbolTable.findBuiltIn(ImmutableString("gl_MaxComputeWorkGroupSize"), mShaderVersion));
Martin Radev802abe02016-08-04 17:48:32 +03003017
3018 const TConstantUnion *maxComputeWorkGroupSizeData =
3019 maxComputeWorkGroupSize->getConstPointer();
3020
3021 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3022 {
3023 if (layoutQualifier.localSize[i] != -1)
3024 {
3025 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3026 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3027 if (mComputeShaderLocalSize[i] < 1 ||
3028 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3029 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003030 std::stringstream reasonStream;
3031 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3032 << maxComputeWorkGroupSizeValue;
3033 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003034
Olli Etuaho4de340a2016-12-16 09:32:03 +00003035 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003036 return;
3037 }
3038 }
3039 }
3040
3041 mComputeShaderLocalSizeDeclared = true;
3042 }
Shaob5cc1192017-07-06 10:47:20 +08003043 else if (typeQualifier.qualifier == EvqGeometryIn)
3044 {
3045 if (mShaderVersion < 310)
3046 {
3047 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3048 return;
3049 }
3050
3051 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3052 {
3053 return;
3054 }
3055 }
3056 else if (typeQualifier.qualifier == EvqGeometryOut)
3057 {
3058 if (mShaderVersion < 310)
3059 {
3060 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3061 "layout");
3062 return;
3063 }
3064
3065 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3066 {
3067 return;
3068 }
3069 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003070 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3071 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003072 {
3073 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3074 // specification.
3075 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3076 {
3077 error(typeQualifier.line, "Number of views does not match the previous declaration",
3078 "layout");
3079 return;
3080 }
3081
3082 if (layoutQualifier.numViews == -1)
3083 {
3084 error(typeQualifier.line, "No num_views specified", "layout");
3085 return;
3086 }
3087
3088 if (layoutQualifier.numViews > mMaxNumViews)
3089 {
3090 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3091 "layout");
3092 return;
3093 }
3094
3095 mNumViews = layoutQualifier.numViews;
3096 }
Martin Radev802abe02016-08-04 17:48:32 +03003097 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003098 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003099 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003100 {
Martin Radev802abe02016-08-04 17:48:32 +03003101 return;
3102 }
3103
Jiajia Qinbc585152017-06-23 15:42:17 +08003104 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003105 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003106 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003107 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003108 return;
3109 }
3110
3111 if (mShaderVersion < 300)
3112 {
3113 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3114 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003115 return;
3116 }
3117
Olli Etuaho09b04a22016-12-15 13:30:26 +00003118 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003119
3120 if (layoutQualifier.matrixPacking != EmpUnspecified)
3121 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003122 if (typeQualifier.qualifier == EvqUniform)
3123 {
3124 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3125 }
3126 else if (typeQualifier.qualifier == EvqBuffer)
3127 {
3128 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3129 }
Martin Radev802abe02016-08-04 17:48:32 +03003130 }
3131
3132 if (layoutQualifier.blockStorage != EbsUnspecified)
3133 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003134 if (typeQualifier.qualifier == EvqUniform)
3135 {
3136 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3137 }
3138 else if (typeQualifier.qualifier == EvqBuffer)
3139 {
3140 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3141 }
Martin Radev802abe02016-08-04 17:48:32 +03003142 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003143 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003144}
3145
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003146TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3147 const TFunction &function,
3148 const TSourceLoc &location,
3149 bool insertParametersToSymbolTable)
3150{
Olli Etuahobed35d72017-12-20 16:36:26 +02003151 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003152
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003153 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003154 prototype->setLine(location);
3155
3156 for (size_t i = 0; i < function.getParamCount(); i++)
3157 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003158 const TVariable *param = function.getParam(i);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003159
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3161 // be used for unused args).
Olli Etuahod4bd9632018-03-08 16:32:44 +02003162 if (param->symbolType() != SymbolType::Empty)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003163 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003164 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003165 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003166 if (!symbolTable.declare(const_cast<TVariable *>(param)))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003167 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003168 error(location, "redefinition", param->name());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003169 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003170 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003171 // Unsized type of a named parameter should have already been checked and sanitized.
Olli Etuahod4bd9632018-03-08 16:32:44 +02003172 ASSERT(!param->getType().isUnsizedArray());
Olli Etuaho55bde912017-10-25 13:41:13 +03003173 }
3174 else
3175 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003176 if (param->getType().isUnsizedArray())
Olli Etuaho55bde912017-10-25 13:41:13 +03003177 {
3178 error(location, "function parameter array must be sized at compile time", "[]");
3179 // We don't need to size the arrays since the parameter is unnamed and hence
3180 // inaccessible.
3181 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003183 }
3184 return prototype;
3185}
3186
Olli Etuaho16c745a2017-01-16 17:02:27 +00003187TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3188 const TFunction &parsedFunction,
3189 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003190{
Olli Etuaho476197f2016-10-11 13:59:08 +01003191 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3192 // first declaration. Either way the instance in the symbol table is used to track whether the
3193 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003194 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003195 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003196 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3197
3198 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003199 {
3200 // ESSL 1.00.17 section 4.2.7.
3201 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3202 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003203 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003204
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 TIntermFunctionPrototype *prototype =
3206 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003207
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003208 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003209
3210 if (!symbolTable.atGlobalLevel())
3211 {
3212 // ESSL 3.00.4 section 4.2.4.
3213 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003214 }
3215
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003216 return prototype;
3217}
3218
Olli Etuaho336b1472016-10-05 16:37:55 +01003219TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003220 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003221 TIntermBlock *functionBody,
3222 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003223{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003224 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003225 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3226 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003227 error(location,
3228 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229 }
3230
Olli Etuahof51fdd22016-10-03 10:03:40 +01003231 if (functionBody == nullptr)
3232 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003233 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003234 functionBody->setLine(location);
3235 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003236 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003237 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003238 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003239
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003240 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003241 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003242}
3243
Olli Etuaho476197f2016-10-11 13:59:08 +01003244void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003245 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003246 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003247{
Olli Etuaho476197f2016-10-11 13:59:08 +01003248 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003249
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003250 bool wasDefined = false;
3251 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3252 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003253 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003254 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003255 }
Jamie Madill185fb402015-06-12 15:48:48 -04003256
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003258 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003260
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003261 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003262 setLoopNestingLevel(0);
3263}
3264
Jamie Madillb98c3a82015-07-23 14:26:04 -04003265TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003266{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003267 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003268 // We don't know at this point whether this is a function definition or a prototype.
3269 // The definition production code will check for redefinitions.
3270 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003271 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303272
Olli Etuahod80f2942017-11-06 12:44:45 +02003273 for (size_t i = 0u; i < function->getParamCount(); ++i)
3274 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003275 const TVariable *param = function->getParam(i);
3276 if (param->getType().isStructSpecifier())
Olli Etuahod80f2942017-11-06 12:44:45 +02003277 {
3278 // ESSL 3.00.6 section 12.10.
3279 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003280 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003281 }
3282 }
3283
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003284 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303285 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003286 const UnmangledBuiltIn *builtIn =
3287 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3288 if (builtIn &&
3289 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3290 {
3291 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3292 // functions. Therefore overloading or redefining builtin functions is an error.
3293 error(location, "Name of a built-in function cannot be redeclared as function",
3294 function->name());
3295 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303296 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003297 else
3298 {
3299 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3300 // this applies to redeclarations as well.
3301 const TSymbol *builtIn =
3302 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3303 if (builtIn)
3304 {
3305 error(location, "built-in functions cannot be redefined", function->name());
3306 }
3307 }
3308
3309 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3310 // here.
3311 const TFunction *prevDec =
3312 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3313 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003314 {
3315 if (prevDec->getReturnType() != function->getReturnType())
3316 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003317 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003318 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003319 }
3320 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3321 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02003322 if (prevDec->getParam(i)->getType().getQualifier() !=
3323 function->getParam(i)->getType().getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003324 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003325 error(location,
3326 "function must have the same parameter qualifiers in all of its declarations",
Olli Etuahod4bd9632018-03-08 16:32:44 +02003327 function->getParam(i)->getType().getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003328 }
3329 }
3330 }
3331
Jamie Madill185fb402015-06-12 15:48:48 -04003332 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003333 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3334 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003335 if (prevSym)
3336 {
3337 if (!prevSym->isFunction())
3338 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003339 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003340 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003341 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003342 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003343 // Parsing is at the inner scope level of the function's arguments and body statement at this
3344 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3345 // scope.
3346 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003347
Olli Etuaho78d13742017-01-18 13:06:10 +00003348 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003349 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003350 {
3351 if (function->getParamCount() > 0)
3352 {
3353 error(location, "function cannot take any parameter(s)", "main");
3354 }
3355 if (function->getReturnType().getBasicType() != EbtVoid)
3356 {
3357 error(location, "main function cannot return a value",
3358 function->getReturnType().getBasicString());
3359 }
3360 }
3361
Jamie Madill185fb402015-06-12 15:48:48 -04003362 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003363 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3364 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003365 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3366 //
3367 return function;
3368}
3369
Olli Etuaho9de84a52016-06-14 17:36:01 +03003370TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003371 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003372 const TSourceLoc &location)
3373{
3374 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3375 {
3376 error(location, "no qualifiers allowed for function return",
3377 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003378 }
3379 if (!type.layoutQualifier.isEmpty())
3380 {
3381 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003382 }
jchen10cc2a10e2017-05-03 14:05:12 +08003383 // make sure an opaque type is not involved as well...
3384 std::string reason(getBasicString(type.getBasicType()));
3385 reason += "s can't be function return values";
3386 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003387 if (mShaderVersion < 300)
3388 {
3389 // Array return values are forbidden, but there's also no valid syntax for declaring array
3390 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003391 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003392
3393 if (type.isStructureContainingArrays())
3394 {
3395 // ESSL 1.00.17 section 6.1 Function Definitions
3396 error(location, "structures containing arrays can't be function return values",
3397 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003398 }
3399 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003400
3401 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003402 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003403}
3404
Olli Etuaho697bf652018-02-16 11:50:54 +02003405TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3406 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003407{
Olli Etuaho697bf652018-02-16 11:50:54 +02003408 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003409}
3410
Olli Etuaho95ed1942018-02-01 14:01:19 +02003411TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003412{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003413 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003414 {
3415 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3416 "[]");
3417 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003418 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003419 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003420 error(publicType.getLine(), "constructor can't be a structure definition",
3421 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003422 }
3423
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003424 TType *type = new TType(publicType);
3425 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003426 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003427 error(publicType.getLine(), "cannot construct this type",
3428 getBasicString(publicType.getBasicType()));
3429 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003430 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003431 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003432}
3433
Olli Etuaho55bde912017-10-25 13:41:13 +03003434void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3435 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003436 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003437 TType *arrayType)
3438{
3439 if (arrayType->isUnsizedArray())
3440 {
3441 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003442 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003443 }
3444}
3445
3446TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003447 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003448 const TSourceLoc &nameLoc)
3449{
Olli Etuaho55bde912017-10-25 13:41:13 +03003450 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003451 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003452 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003453 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003454 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003455 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003456 checkIsNotReserved(nameLoc, name);
3457 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003458 return param;
3459}
3460
Olli Etuaho55bde912017-10-25 13:41:13 +03003461TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003462 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003463 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003464{
Olli Etuaho55bde912017-10-25 13:41:13 +03003465 TType *type = new TType(publicType);
3466 return parseParameterDeclarator(type, name, nameLoc);
3467}
3468
Olli Etuahofbb1c792018-01-19 16:26:59 +02003469TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003470 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003471 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003472 const TSourceLoc &arrayLoc,
3473 TPublicType *elementType)
3474{
3475 checkArrayElementIsNotArray(arrayLoc, *elementType);
3476 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003477 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003478 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003479}
3480
Olli Etuaho95ed1942018-02-01 14:01:19 +02003481bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3482 const TIntermSequence &arguments,
3483 TType type,
3484 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003485{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003486 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003487 {
3488 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3489 return false;
3490 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003491 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003492 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003493 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003494 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003495 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3496 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003497 {
3498 error(line, "constructing from a non-dereferenced array", "constructor");
3499 return false;
3500 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003501 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003502 {
3503 if (dimensionalityFromElement == 1u)
3504 {
3505 error(line, "implicitly sized array of arrays constructor argument is not an array",
3506 "constructor");
3507 }
3508 else
3509 {
3510 error(line,
3511 "implicitly sized array of arrays constructor argument dimensionality is too "
3512 "low",
3513 "constructor");
3514 }
3515 return false;
3516 }
3517 }
3518 return true;
3519}
3520
Jamie Madillb98c3a82015-07-23 14:26:04 -04003521// This function is used to test for the correctness of the parameters passed to various constructor
3522// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003523//
Olli Etuaho856c4972016-08-08 11:38:39 +03003524// 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 +00003525//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003526TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003527{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003528 TType type = fnCall->constructorType();
3529 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003530 if (type.isUnsizedArray())
3531 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003532 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003533 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003534 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003535 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003536 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003537 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003539 if (type.getOutermostArraySize() == 0u)
3540 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003541 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003542 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003543 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003544 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003545 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003546 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003547 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 }
3549 }
3550 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003551 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003552
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003553 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003554 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003555 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003556 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003557
Olli Etuaho95ed1942018-02-01 14:01:19 +02003558 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003559 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003560
Olli Etuaho765924f2018-01-04 12:48:36 +02003561 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003562}
3563
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003564//
3565// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003566// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003567//
Olli Etuaho13389b62016-10-16 11:48:18 +01003568TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003569 const TTypeQualifierBuilder &typeQualifierBuilder,
3570 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003571 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003572 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003573 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003574 const TSourceLoc &instanceLine,
3575 TIntermTyped *arrayIndex,
3576 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003577{
Olli Etuaho856c4972016-08-08 11:38:39 +03003578 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003579
Olli Etuaho77ba4082016-12-16 12:01:18 +00003580 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003581
Jiajia Qinbc585152017-06-23 15:42:17 +08003582 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003584 error(typeQualifier.line,
3585 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3586 "3.10",
3587 getQualifierString(typeQualifier.qualifier));
3588 }
3589 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3590 {
3591 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003592 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593 }
3594
Martin Radev70866b82016-07-22 15:27:42 +03003595 if (typeQualifier.invariant)
3596 {
3597 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3598 }
3599
Jiajia Qinbc585152017-06-23 15:42:17 +08003600 if (typeQualifier.qualifier != EvqBuffer)
3601 {
3602 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3603 }
Olli Etuaho43364892017-02-13 16:00:12 +00003604
jchen10af713a22017-04-19 09:10:56 +08003605 // add array index
3606 unsigned int arraySize = 0;
3607 if (arrayIndex != nullptr)
3608 {
3609 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3610 }
3611
3612 if (mShaderVersion < 310)
3613 {
3614 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3615 }
3616 else
3617 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003618 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3619 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003620 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003621
Andrei Volykhina5527072017-03-22 16:46:30 +03003622 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3623
Jamie Madill099c0f32013-06-20 11:55:52 -04003624 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003625 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003626 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3627 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003628
Jamie Madill099c0f32013-06-20 11:55:52 -04003629 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3630 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003631 if (typeQualifier.qualifier == EvqUniform)
3632 {
3633 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3634 }
3635 else if (typeQualifier.qualifier == EvqBuffer)
3636 {
3637 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3638 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003639 }
3640
Jamie Madill1566ef72013-06-20 11:55:54 -04003641 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3642 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003643 if (typeQualifier.qualifier == EvqUniform)
3644 {
3645 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3646 }
3647 else if (typeQualifier.qualifier == EvqBuffer)
3648 {
3649 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3650 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003651 }
3652
Olli Etuaho856c4972016-08-08 11:38:39 +03003653 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003654
Martin Radev2cc85b32016-08-05 16:22:53 +03003655 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3656
Jamie Madill98493dd2013-07-08 14:39:03 -04003657 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303658 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3659 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003660 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303661 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003662 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303663 {
jchen10cc2a10e2017-05-03 14:05:12 +08003664 std::string reason("unsupported type - ");
3665 reason += fieldType->getBasicString();
3666 reason += " types are not allowed in interface blocks";
3667 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003668 }
3669
Jamie Madill98493dd2013-07-08 14:39:03 -04003670 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003671 switch (qualifier)
3672 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003673 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003674 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003675 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003676 if (typeQualifier.qualifier == EvqBuffer)
3677 {
3678 error(field->line(), "invalid qualifier on shader storage block member",
3679 getQualifierString(qualifier));
3680 }
3681 break;
3682 case EvqBuffer:
3683 if (typeQualifier.qualifier == EvqUniform)
3684 {
3685 error(field->line(), "invalid qualifier on uniform block member",
3686 getQualifierString(qualifier));
3687 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003688 break;
3689 default:
3690 error(field->line(), "invalid qualifier on interface block member",
3691 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003692 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003693 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003694
Martin Radev70866b82016-07-22 15:27:42 +03003695 if (fieldType->isInvariant())
3696 {
3697 error(field->line(), "invalid qualifier on interface block member", "invariant");
3698 }
3699
Jamie Madilla5efff92013-06-06 11:56:47 -04003700 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003701 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003702 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003703 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003704
Jamie Madill98493dd2013-07-08 14:39:03 -04003705 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003706 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003707 error(field->line(), "invalid layout qualifier: cannot be used here",
3708 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003709 }
3710
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003712 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003713 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003714 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003715 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003716 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003717 warning(field->line(),
3718 "extraneous layout qualifier: only has an effect on matrix types",
3719 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003720 }
3721
Jamie Madill98493dd2013-07-08 14:39:03 -04003722 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003723
Olli Etuahoebee5b32017-11-23 12:56:32 +02003724 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3725 typeQualifier.qualifier != EvqBuffer)
3726 {
3727 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3728 checkIsNotUnsizedArray(field->line(),
3729 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003730 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003731 }
3732
Jiajia Qinbc585152017-06-23 15:42:17 +08003733 if (typeQualifier.qualifier == EvqBuffer)
3734 {
3735 // set memory qualifiers
3736 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3737 // qualified with a memory qualifier, it is as if all of its members were declared with
3738 // the same memory qualifier.
3739 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3740 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3741 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3742 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3743 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3744 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3745 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3746 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3747 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3748 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3749 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003750 }
3751
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003752 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003753 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003754 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003755 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003756 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003757 }
3758
Olli Etuahob60d30f2018-01-16 12:31:06 +02003759 TType *interfaceBlockType =
3760 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003761 if (arrayIndex != nullptr)
3762 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003763 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003764 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003765
Olli Etuaho195be942017-12-04 23:40:14 +02003766 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003767 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3768 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003769 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003770 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003771 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003772
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003773 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003774 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003775 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003776 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3777 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003778 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003779 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003780
3781 // set parent pointer of the field variable
3782 fieldType->setInterfaceBlock(interfaceBlock);
3783
Olli Etuahob60d30f2018-01-16 12:31:06 +02003784 fieldType->setQualifier(typeQualifier.qualifier);
3785
Olli Etuaho195be942017-12-04 23:40:14 +02003786 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003787 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003788 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303789 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003790 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003791 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003792 }
3793 }
3794 }
3795 else
3796 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003797 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003798
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003799 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003800 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303801 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003802 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003803 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003804 }
3805
Olli Etuaho195be942017-12-04 23:40:14 +02003806 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3807 blockSymbol->setLine(typeQualifier.line);
3808 TIntermDeclaration *declaration = new TIntermDeclaration();
3809 declaration->appendDeclarator(blockSymbol);
3810 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003811
3812 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003813 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003814}
3815
Olli Etuahofbb1c792018-01-19 16:26:59 +02003816void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3817 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003818{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003819 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003820
3821 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003822 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303823 if (mStructNestingLevel > 1)
3824 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003825 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003826 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003827}
3828
3829void TParseContext::exitStructDeclaration()
3830{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003831 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003832}
3833
Olli Etuaho8a176262016-08-16 14:23:01 +03003834void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003835{
Jamie Madillacb4b812016-11-07 13:50:29 -05003836 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303837 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003838 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003839 }
3840
Arun Patole7e7e68d2015-05-22 12:02:25 +05303841 if (field.type()->getBasicType() != EbtStruct)
3842 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003843 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003844 }
3845
3846 // We're already inside a structure definition at this point, so add
3847 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303848 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3849 {
Jamie Madill41a49272014-03-18 16:10:13 -04003850 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003851 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3852 {
3853 // This may happen in case there are nested struct definitions. While they are also
3854 // invalid GLSL, they don't cause a syntax error.
3855 reasonStream << "Struct nesting";
3856 }
3857 else
3858 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003859 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003860 }
3861 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003862 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003863 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003864 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003865 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003866}
3867
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003868//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003869// Parse an array index expression
3870//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3872 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303873 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003874{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3876 {
3877 if (baseExpression->getAsSymbolNode())
3878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303879 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003880 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003881 }
3882 else
3883 {
3884 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3885 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003886
Olli Etuaho3ec75682017-07-05 17:02:55 +03003887 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003888 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003889
Jiawei Shaod8105a02017-08-08 09:54:36 +08003890 if (baseExpression->getQualifier() == EvqPerVertexIn)
3891 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003892 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003893 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3894 {
3895 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3896 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3897 }
3898 }
3899
Jamie Madill21c1e452014-12-29 11:33:41 -05003900 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3901
Olli Etuaho36b05142015-11-12 13:10:42 +02003902 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3903 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3904 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3905 // index is a constant expression.
3906 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3907 {
3908 if (baseExpression->isInterfaceBlock())
3909 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003910 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003911 switch (baseExpression->getQualifier())
3912 {
3913 case EvqPerVertexIn:
3914 break;
3915 case EvqUniform:
3916 case EvqBuffer:
3917 error(location,
3918 "array indexes for uniform block arrays and shader storage block arrays "
3919 "must be constant integral expressions",
3920 "[");
3921 break;
3922 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003923 // We can reach here only in error cases.
3924 ASSERT(mDiagnostics->numErrors() > 0);
3925 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003926 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003927 }
3928 else if (baseExpression->getQualifier() == EvqFragmentOut)
3929 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003930 error(location,
3931 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003932 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003933 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3934 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003935 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003936 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003937 }
3938
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003939 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003940 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003941 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3942 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3943 // constant fold expressions that are not constant expressions). The most compatible way to
3944 // handle this case is to report a warning instead of an error and force the index to be in
3945 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003946 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003947 int index = 0;
3948 if (indexConstantUnion->getBasicType() == EbtInt)
3949 {
3950 index = indexConstantUnion->getIConst(0);
3951 }
3952 else if (indexConstantUnion->getBasicType() == EbtUInt)
3953 {
3954 index = static_cast<int>(indexConstantUnion->getUConst(0));
3955 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003956
3957 int safeIndex = -1;
3958
Olli Etuahoebee5b32017-11-23 12:56:32 +02003959 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003960 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003961 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3962 safeIndex = 0;
3963 }
3964
3965 if (!baseExpression->getType().isUnsizedArray())
3966 {
3967 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003968 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003969 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003970 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003971 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3972 {
3973 outOfRangeError(outOfRangeIndexIsError, location,
3974 "array index for gl_FragData must be zero when "
3975 "GL_EXT_draw_buffers is disabled",
3976 "[]");
3977 safeIndex = 0;
3978 }
3979 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003980 }
3981 // Only do generic out-of-range check if similar error hasn't already been reported.
3982 if (safeIndex < 0)
3983 {
3984 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02003985 {
3986 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3987 baseExpression->getOutermostArraySize(),
3988 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003989 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003990 else if (baseExpression->isMatrix())
3991 {
3992 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
3993 baseExpression->getType().getCols(),
3994 "matrix field selection out of range");
3995 }
3996 else
3997 {
3998 ASSERT(baseExpression->isVector());
3999 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4000 baseExpression->getType().getNominalSize(),
4001 "vector field selection out of range");
4002 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004003 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004004
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 ASSERT(safeIndex >= 0);
4006 // Data of constant unions can't be changed, because it may be shared with other
4007 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4008 // sanitized object.
4009 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4010 {
4011 TConstantUnion *safeConstantUnion = new TConstantUnion();
4012 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004013 indexExpression = new TIntermConstantUnion(
4014 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4015 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004016 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004017
Olli Etuahoebee5b32017-11-23 12:56:32 +02004018 TIntermBinary *node =
4019 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4020 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004021 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004022 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004023 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004024
4025 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4026 node->setLine(location);
4027 // Indirect indexing can never be constant folded.
4028 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029}
4030
Olli Etuahoebee5b32017-11-23 12:56:32 +02004031int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4032 const TSourceLoc &location,
4033 int index,
4034 int arraySize,
4035 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004036{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004037 // Should not reach here with an unsized / runtime-sized array.
4038 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004039 // A negative index should already have been checked.
4040 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004041 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004042 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004043 std::stringstream reasonStream;
4044 reasonStream << reason << " '" << index << "'";
4045 std::string token = reasonStream.str();
4046 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004047 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004048 }
4049 return index;
4050}
4051
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4053 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004054 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004055 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004056{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004057 if (baseExpression->isArray())
4058 {
4059 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004060 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004061 }
4062
4063 if (baseExpression->isVector())
4064 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004065 TVector<int> fieldOffsets;
4066 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4067 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004068 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004069 fieldOffsets.resize(1);
4070 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004071 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004072 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4073 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074
Olli Etuaho765924f2018-01-04 12:48:36 +02004075 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004077 else if (baseExpression->getBasicType() == EbtStruct)
4078 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304079 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004080 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081 {
4082 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004083 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084 }
4085 else
4086 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004087 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004088 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004089 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004090 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004091 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004092 {
4093 fieldFound = true;
4094 break;
4095 }
4096 }
4097 if (fieldFound)
4098 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004099 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004100 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004101 TIntermBinary *node =
4102 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4103 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004104 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105 }
4106 else
4107 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004108 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004109 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 }
4111 }
4112 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004113 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004114 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304115 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004116 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004117 {
4118 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004119 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004120 }
4121 else
4122 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004123 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004124 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004125 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004126 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004128 {
4129 fieldFound = true;
4130 break;
4131 }
4132 }
4133 if (fieldFound)
4134 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004135 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004136 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004137 TIntermBinary *node =
4138 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4139 node->setLine(dotLocation);
4140 // Indexing interface blocks can never be constant folded.
4141 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004142 }
4143 else
4144 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004145 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004146 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004147 }
4148 }
4149 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004150 else
4151 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004152 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004153 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004154 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004155 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004156 }
4157 else
4158 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304159 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004160 " field selection requires structure, vector, or interface block on left hand "
4161 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004162 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004163 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004164 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004165 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004166}
4167
Olli Etuahofbb1c792018-01-19 16:26:59 +02004168TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004169 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004170{
Jamie Madill2f294c92017-11-20 14:47:26 -05004171 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004172
4173 if (qualifierType == "shared")
4174 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004175 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004176 {
4177 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4178 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004179 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004180 }
4181 else if (qualifierType == "packed")
4182 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004183 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004184 {
4185 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4186 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004187 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004188 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004189 else if (qualifierType == "std430")
4190 {
4191 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4192 qualifier.blockStorage = EbsStd430;
4193 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004194 else if (qualifierType == "std140")
4195 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004196 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004197 }
4198 else if (qualifierType == "row_major")
4199 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004200 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004201 }
4202 else if (qualifierType == "column_major")
4203 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004204 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004205 }
4206 else if (qualifierType == "location")
4207 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004208 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004209 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004210 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004211 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004212 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004213 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4214 {
4215 qualifier.yuv = true;
4216 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004217 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004218 else if (qualifierType == "rgba32f")
4219 {
4220 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4221 qualifier.imageInternalFormat = EiifRGBA32F;
4222 }
4223 else if (qualifierType == "rgba16f")
4224 {
4225 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4226 qualifier.imageInternalFormat = EiifRGBA16F;
4227 }
4228 else if (qualifierType == "r32f")
4229 {
4230 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4231 qualifier.imageInternalFormat = EiifR32F;
4232 }
4233 else if (qualifierType == "rgba8")
4234 {
4235 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4236 qualifier.imageInternalFormat = EiifRGBA8;
4237 }
4238 else if (qualifierType == "rgba8_snorm")
4239 {
4240 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4241 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4242 }
4243 else if (qualifierType == "rgba32i")
4244 {
4245 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4246 qualifier.imageInternalFormat = EiifRGBA32I;
4247 }
4248 else if (qualifierType == "rgba16i")
4249 {
4250 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4251 qualifier.imageInternalFormat = EiifRGBA16I;
4252 }
4253 else if (qualifierType == "rgba8i")
4254 {
4255 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4256 qualifier.imageInternalFormat = EiifRGBA8I;
4257 }
4258 else if (qualifierType == "r32i")
4259 {
4260 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4261 qualifier.imageInternalFormat = EiifR32I;
4262 }
4263 else if (qualifierType == "rgba32ui")
4264 {
4265 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4266 qualifier.imageInternalFormat = EiifRGBA32UI;
4267 }
4268 else if (qualifierType == "rgba16ui")
4269 {
4270 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4271 qualifier.imageInternalFormat = EiifRGBA16UI;
4272 }
4273 else if (qualifierType == "rgba8ui")
4274 {
4275 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4276 qualifier.imageInternalFormat = EiifRGBA8UI;
4277 }
4278 else if (qualifierType == "r32ui")
4279 {
4280 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4281 qualifier.imageInternalFormat = EiifR32UI;
4282 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004283 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4284 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.primitiveType = EptPoints;
4288 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004289 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4290 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.primitiveType = EptLines;
4294 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004295 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4296 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.primitiveType = EptLinesAdjacency;
4300 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004301 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4302 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.primitiveType = EptTriangles;
4306 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004307 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4308 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.primitiveType = EptTrianglesAdjacency;
4312 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004313 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4314 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.primitiveType = EptLineStrip;
4318 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004319 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4320 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.primitiveType = EptTriangleStrip;
4324 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004325
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004326 else
4327 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004328 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004329 }
4330
Jamie Madilla5efff92013-06-06 11:56:47 -04004331 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004332}
4333
Olli Etuahofbb1c792018-01-19 16:26:59 +02004334void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004335 const TSourceLoc &qualifierTypeLine,
4336 int intValue,
4337 const TSourceLoc &intValueLine,
4338 const std::string &intValueString,
4339 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004340 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004341{
Olli Etuaho856c4972016-08-08 11:38:39 +03004342 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004343 if (intValue < 1)
4344 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004345 std::stringstream reasonStream;
4346 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4347 std::string reason = reasonStream.str();
4348 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004349 }
4350 (*localSize)[index] = intValue;
4351}
4352
Olli Etuaho09b04a22016-12-15 13:30:26 +00004353void TParseContext::parseNumViews(int intValue,
4354 const TSourceLoc &intValueLine,
4355 const std::string &intValueString,
4356 int *numViews)
4357{
4358 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4359 // specification.
4360 if (intValue < 1)
4361 {
4362 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4363 }
4364 *numViews = intValue;
4365}
4366
Shaob5cc1192017-07-06 10:47:20 +08004367void TParseContext::parseInvocations(int intValue,
4368 const TSourceLoc &intValueLine,
4369 const std::string &intValueString,
4370 int *numInvocations)
4371{
4372 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4373 // it doesn't make sense to accept invocations <= 0.
4374 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4375 {
4376 error(intValueLine,
4377 "out of range: invocations must be in the range of [1, "
4378 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4379 intValueString.c_str());
4380 }
4381 else
4382 {
4383 *numInvocations = intValue;
4384 }
4385}
4386
4387void TParseContext::parseMaxVertices(int intValue,
4388 const TSourceLoc &intValueLine,
4389 const std::string &intValueString,
4390 int *maxVertices)
4391{
4392 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4393 // it doesn't make sense to accept max_vertices < 0.
4394 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4395 {
4396 error(
4397 intValueLine,
4398 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4399 intValueString.c_str());
4400 }
4401 else
4402 {
4403 *maxVertices = intValue;
4404 }
4405}
4406
Olli Etuahofbb1c792018-01-19 16:26:59 +02004407TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004408 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004409 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304410 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004411{
Jamie Madill2f294c92017-11-20 14:47:26 -05004412 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004413
Martin Radev802abe02016-08-04 17:48:32 +03004414 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004415
Martin Radev802abe02016-08-04 17:48:32 +03004416 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004417 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004418 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004419 if (intValue < 0)
4420 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004421 error(intValueLine, "out of range: location must be non-negative",
4422 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004423 }
4424 else
4425 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004426 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004427 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004428 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004429 }
Olli Etuaho43364892017-02-13 16:00:12 +00004430 else if (qualifierType == "binding")
4431 {
4432 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4433 if (intValue < 0)
4434 {
4435 error(intValueLine, "out of range: binding must be non-negative",
4436 intValueString.c_str());
4437 }
4438 else
4439 {
4440 qualifier.binding = intValue;
4441 }
4442 }
jchen104cdac9e2017-05-08 11:01:20 +08004443 else if (qualifierType == "offset")
4444 {
4445 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4446 if (intValue < 0)
4447 {
4448 error(intValueLine, "out of range: offset must be non-negative",
4449 intValueString.c_str());
4450 }
4451 else
4452 {
4453 qualifier.offset = intValue;
4454 }
4455 }
Martin Radev802abe02016-08-04 17:48:32 +03004456 else if (qualifierType == "local_size_x")
4457 {
4458 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4459 &qualifier.localSize);
4460 }
4461 else if (qualifierType == "local_size_y")
4462 {
4463 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4464 &qualifier.localSize);
4465 }
4466 else if (qualifierType == "local_size_z")
4467 {
4468 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4469 &qualifier.localSize);
4470 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004471 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004472 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004473 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4474 {
4475 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4476 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004477 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004478 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4479 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004480 {
4481 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4482 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004483 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4484 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004485 {
4486 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4487 }
4488
Martin Radev802abe02016-08-04 17:48:32 +03004489 else
4490 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004491 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004492 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004493
Jamie Madilla5efff92013-06-06 11:56:47 -04004494 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004495}
4496
Olli Etuaho613b9592016-09-05 12:05:53 +03004497TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4498{
4499 return new TTypeQualifierBuilder(
4500 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4501 mShaderVersion);
4502}
4503
Olli Etuahocce89652017-06-19 16:04:09 +03004504TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4505 const TSourceLoc &loc)
4506{
4507 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4508 return new TStorageQualifierWrapper(qualifier, loc);
4509}
4510
4511TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4512{
4513 if (getShaderType() == GL_VERTEX_SHADER)
4514 {
4515 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4516 }
4517 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4518}
4519
4520TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4521{
4522 if (declaringFunction())
4523 {
4524 return new TStorageQualifierWrapper(EvqIn, loc);
4525 }
Shaob5cc1192017-07-06 10:47:20 +08004526
4527 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004528 {
Shaob5cc1192017-07-06 10:47:20 +08004529 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004530 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004531 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004532 {
4533 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4534 }
4535 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004536 }
Shaob5cc1192017-07-06 10:47:20 +08004537 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004538 {
Shaob5cc1192017-07-06 10:47:20 +08004539 if (mShaderVersion < 300)
4540 {
4541 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4542 }
4543 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004544 }
Shaob5cc1192017-07-06 10:47:20 +08004545 case GL_COMPUTE_SHADER:
4546 {
4547 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4548 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004549 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004550 {
4551 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4552 }
4553 default:
4554 {
4555 UNREACHABLE();
4556 return new TStorageQualifierWrapper(EvqLast, loc);
4557 }
Olli Etuahocce89652017-06-19 16:04:09 +03004558 }
Olli Etuahocce89652017-06-19 16:04:09 +03004559}
4560
4561TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4562{
4563 if (declaringFunction())
4564 {
4565 return new TStorageQualifierWrapper(EvqOut, loc);
4566 }
Shaob5cc1192017-07-06 10:47:20 +08004567 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004568 {
Shaob5cc1192017-07-06 10:47:20 +08004569 case GL_VERTEX_SHADER:
4570 {
4571 if (mShaderVersion < 300)
4572 {
4573 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4574 }
4575 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4576 }
4577 case GL_FRAGMENT_SHADER:
4578 {
4579 if (mShaderVersion < 300)
4580 {
4581 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4582 }
4583 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4584 }
4585 case GL_COMPUTE_SHADER:
4586 {
4587 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4588 return new TStorageQualifierWrapper(EvqLast, loc);
4589 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004590 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004591 {
4592 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4593 }
4594 default:
4595 {
4596 UNREACHABLE();
4597 return new TStorageQualifierWrapper(EvqLast, loc);
4598 }
Olli Etuahocce89652017-06-19 16:04:09 +03004599 }
Olli Etuahocce89652017-06-19 16:04:09 +03004600}
4601
4602TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4603{
4604 if (!declaringFunction())
4605 {
4606 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4607 }
4608 return new TStorageQualifierWrapper(EvqInOut, loc);
4609}
4610
Jamie Madillb98c3a82015-07-23 14:26:04 -04004611TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004612 TLayoutQualifier rightQualifier,
4613 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004614{
Martin Radevc28888b2016-07-22 15:27:42 +03004615 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004616 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004617}
4618
Olli Etuahofbb1c792018-01-19 16:26:59 +02004619TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4620 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004621{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004622 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004623 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004624}
4625
Olli Etuahofbb1c792018-01-19 16:26:59 +02004626TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004627 const TSourceLoc &loc,
4628 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004629{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004630 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004631 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004632}
4633
Olli Etuaho722bfb52017-10-26 17:00:11 +03004634void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4635 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004636 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004637 const TSourceLoc &location)
4638{
4639 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4640 {
4641 if ((*fieldIter)->name() == name)
4642 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004643 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004644 }
4645 }
4646}
4647
4648TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4649{
4650 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4651 ++fieldIter)
4652 {
4653 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4654 location);
4655 }
4656 return fields;
4657}
4658
Olli Etuaho4de340a2016-12-16 09:32:03 +00004659TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4660 const TFieldList *newlyAddedFields,
4661 const TSourceLoc &location)
4662{
4663 for (TField *field : *newlyAddedFields)
4664 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004665 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4666 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004667 processedFields->push_back(field);
4668 }
4669 return processedFields;
4670}
4671
Martin Radev70866b82016-07-22 15:27:42 +03004672TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4673 const TTypeQualifierBuilder &typeQualifierBuilder,
4674 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004675 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004676{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004677 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004678
Martin Radev70866b82016-07-22 15:27:42 +03004679 typeSpecifier->qualifier = typeQualifier.qualifier;
4680 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004681 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004682 typeSpecifier->invariant = typeQualifier.invariant;
4683 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304684 {
Martin Radev70866b82016-07-22 15:27:42 +03004685 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004686 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004687 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004688}
4689
Jamie Madillb98c3a82015-07-23 14:26:04 -04004690TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004691 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004692{
Martin Radev4a9cd802016-09-01 16:51:51 +03004693 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4694 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004695
Olli Etuahofbb1c792018-01-19 16:26:59 +02004696 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004697 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004698
Martin Radev4a9cd802016-09-01 16:51:51 +03004699 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004700
Olli Etuahod5f44c92017-11-29 17:15:40 +02004701 TFieldList *fieldList = new TFieldList();
4702
4703 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304704 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004705 TType *type = new TType(typeSpecifier);
4706 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304707 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004708 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004709 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004710 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004711 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004712
Olli Etuahod5f44c92017-11-29 17:15:40 +02004713 TField *field = new TField(type, declarator->name(), declarator->line());
4714 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4715 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004716 }
4717
Olli Etuahod5f44c92017-11-29 17:15:40 +02004718 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004719}
4720
Martin Radev4a9cd802016-09-01 16:51:51 +03004721TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4722 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004723 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004724 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004725{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004726 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004727 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004728 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004729 structSymbolType = SymbolType::Empty;
4730 }
4731 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004732
Jamie Madill9b820842015-02-12 10:40:10 -05004733 // Store a bool in the struct if we're at global scope, to allow us to
4734 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004735 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004736
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004737 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004738 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004739 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004740 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304741 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004742 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004743 }
4744 }
4745
4746 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004747 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004749 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004750 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004751 switch (qualifier)
4752 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004753 case EvqGlobal:
4754 case EvqTemporary:
4755 break;
4756 default:
4757 error(field.line(), "invalid qualifier on struct member",
4758 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004759 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760 }
Martin Radev70866b82016-07-22 15:27:42 +03004761 if (field.type()->isInvariant())
4762 {
4763 error(field.line(), "invalid qualifier on struct member", "invariant");
4764 }
jchen104cdac9e2017-05-08 11:01:20 +08004765 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4766 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004767 {
4768 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4769 }
4770
Olli Etuahoebee5b32017-11-23 12:56:32 +02004771 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004772 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004773
Olli Etuaho43364892017-02-13 16:00:12 +00004774 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4775
4776 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004777
4778 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 }
4780
Martin Radev4a9cd802016-09-01 16:51:51 +03004781 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004782 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783 exitStructDeclaration();
4784
Martin Radev4a9cd802016-09-01 16:51:51 +03004785 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004786}
4787
Jamie Madillb98c3a82015-07-23 14:26:04 -04004788TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004789 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004790 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004791{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004792 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004793 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004794 init->isVector())
4795 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004796 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4797 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004798 return nullptr;
4799 }
4800
Olli Etuaho923ecef2017-10-11 12:01:38 +03004801 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004802 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004803 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004804 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004805 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004806 }
4807
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004808 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4809 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004810 return node;
4811}
4812
4813TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4814{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004815 if (mSwitchNestingLevel == 0)
4816 {
4817 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004818 return nullptr;
4819 }
4820 if (condition == nullptr)
4821 {
4822 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004823 return nullptr;
4824 }
4825 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004826 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004827 {
4828 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004829 }
4830 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004831 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4832 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4833 // fold in case labels.
4834 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004835 {
4836 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004837 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004838 TIntermCase *node = new TIntermCase(condition);
4839 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004840 return node;
4841}
4842
4843TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4844{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 if (mSwitchNestingLevel == 0)
4846 {
4847 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004848 return nullptr;
4849 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004850 TIntermCase *node = new TIntermCase(nullptr);
4851 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004852 return node;
4853}
4854
Jamie Madillb98c3a82015-07-23 14:26:04 -04004855TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4856 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004857 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004858{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004859 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004860
4861 switch (op)
4862 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004863 case EOpLogicalNot:
4864 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4865 child->isVector())
4866 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004867 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 return nullptr;
4869 }
4870 break;
4871 case EOpBitwiseNot:
4872 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4873 child->isMatrix() || child->isArray())
4874 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004875 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004876 return nullptr;
4877 }
4878 break;
4879 case EOpPostIncrement:
4880 case EOpPreIncrement:
4881 case EOpPostDecrement:
4882 case EOpPreDecrement:
4883 case EOpNegative:
4884 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004885 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4886 child->getBasicType() == EbtBool || child->isArray() ||
4887 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004888 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004889 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004890 return nullptr;
4891 }
Nico Weber41b072b2018-02-09 10:01:32 -05004892 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004893 // Operators for built-ins are already type checked against their prototype.
4894 default:
4895 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004896 }
4897
Jiajia Qinbc585152017-06-23 15:42:17 +08004898 if (child->getMemoryQualifier().writeonly)
4899 {
4900 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4901 return nullptr;
4902 }
4903
Olli Etuahof119a262016-08-19 15:54:22 +03004904 TIntermUnary *node = new TIntermUnary(op, child);
4905 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004906
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004907 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004908}
4909
Olli Etuaho09b22472015-02-11 11:47:26 +02004910TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4911{
Olli Etuahocce89652017-06-19 16:04:09 +03004912 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004913 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004914 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004915 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004916 return child;
4917 }
4918 return node;
4919}
4920
Jamie Madillb98c3a82015-07-23 14:26:04 -04004921TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4922 TIntermTyped *child,
4923 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004924{
Olli Etuaho856c4972016-08-08 11:38:39 +03004925 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004926 return addUnaryMath(op, child, loc);
4927}
4928
Olli Etuaho765924f2018-01-04 12:48:36 +02004929TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4930{
4931 // If we can, we should return the folded version of the expression for subsequent parsing. This
4932 // enables folding the containing expression during parsing as well, instead of the separate
4933 // FoldExpressions() step where folding nested expressions requires multiple full AST
4934 // traversals.
4935
4936 // Even if folding fails the fold() functions return some node representing the expression,
4937 // typically the original node. So "folded" can be assumed to be non-null.
4938 TIntermTyped *folded = expression->fold(mDiagnostics);
4939 ASSERT(folded != nullptr);
4940 if (folded->getQualifier() == expression->getQualifier())
4941 {
4942 // We need this expression to have the correct qualifier when validating the consuming
4943 // expression. So we can only return the folded node from here in case it has the same
4944 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4945 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4946 // 1. (true ? 1.0 : non_constant)
4947 // 2. (non_constant, 1.0)
4948 return folded;
4949 }
4950 return expression;
4951}
4952
Jamie Madillb98c3a82015-07-23 14:26:04 -04004953bool TParseContext::binaryOpCommonCheck(TOperator op,
4954 TIntermTyped *left,
4955 TIntermTyped *right,
4956 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004957{
jchen10b4cf5652017-05-05 18:51:17 +08004958 // Check opaque types are not allowed to be operands in expressions other than array indexing
4959 // and structure member selection.
4960 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4961 {
4962 switch (op)
4963 {
4964 case EOpIndexDirect:
4965 case EOpIndexIndirect:
4966 break;
jchen10b4cf5652017-05-05 18:51:17 +08004967
4968 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004969 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08004970 error(loc, "Invalid operation for variables with an opaque type",
4971 GetOperatorString(op));
4972 return false;
4973 }
4974 }
jchen10cc2a10e2017-05-03 14:05:12 +08004975
Jiajia Qinbc585152017-06-23 15:42:17 +08004976 if (right->getMemoryQualifier().writeonly)
4977 {
4978 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4979 return false;
4980 }
4981
4982 if (left->getMemoryQualifier().writeonly)
4983 {
4984 switch (op)
4985 {
4986 case EOpAssign:
4987 case EOpInitialize:
4988 case EOpIndexDirect:
4989 case EOpIndexIndirect:
4990 case EOpIndexDirectStruct:
4991 case EOpIndexDirectInterfaceBlock:
4992 break;
4993 default:
4994 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4995 return false;
4996 }
4997 }
4998
Olli Etuaho244be012016-08-18 15:26:02 +03004999 if (left->getType().getStruct() || right->getType().getStruct())
5000 {
5001 switch (op)
5002 {
5003 case EOpIndexDirectStruct:
5004 ASSERT(left->getType().getStruct());
5005 break;
5006 case EOpEqual:
5007 case EOpNotEqual:
5008 case EOpAssign:
5009 case EOpInitialize:
5010 if (left->getType() != right->getType())
5011 {
5012 return false;
5013 }
5014 break;
5015 default:
5016 error(loc, "Invalid operation for structs", GetOperatorString(op));
5017 return false;
5018 }
5019 }
5020
Olli Etuaho94050052017-05-08 14:17:44 +03005021 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5022 {
5023 switch (op)
5024 {
5025 case EOpIndexDirectInterfaceBlock:
5026 ASSERT(left->getType().getInterfaceBlock());
5027 break;
5028 default:
5029 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5030 return false;
5031 }
5032 }
5033
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005034 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005035 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005036 error(loc, "array / non-array mismatch", GetOperatorString(op));
5037 return false;
5038 }
5039
5040 if (left->isArray())
5041 {
5042 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005043 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005044 {
5045 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5046 return false;
5047 }
5048
Olli Etuahoe79904c2015-03-18 16:56:42 +02005049 switch (op)
5050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005051 case EOpEqual:
5052 case EOpNotEqual:
5053 case EOpAssign:
5054 case EOpInitialize:
5055 break;
5056 default:
5057 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5058 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005059 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005060 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005061 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005062 {
5063 error(loc, "array size mismatch", GetOperatorString(op));
5064 return false;
5065 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005066 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005067
5068 // Check ops which require integer / ivec parameters
5069 bool isBitShift = false;
5070 switch (op)
5071 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005072 case EOpBitShiftLeft:
5073 case EOpBitShiftRight:
5074 case EOpBitShiftLeftAssign:
5075 case EOpBitShiftRightAssign:
5076 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5077 // check that the basic type is an integer type.
5078 isBitShift = true;
5079 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5080 {
5081 return false;
5082 }
5083 break;
5084 case EOpBitwiseAnd:
5085 case EOpBitwiseXor:
5086 case EOpBitwiseOr:
5087 case EOpBitwiseAndAssign:
5088 case EOpBitwiseXorAssign:
5089 case EOpBitwiseOrAssign:
5090 // It is enough to check the type of only one operand, since later it
5091 // is checked that the operand types match.
5092 if (!IsInteger(left->getBasicType()))
5093 {
5094 return false;
5095 }
5096 break;
5097 default:
5098 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005099 }
5100
5101 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5102 // So the basic type should usually match.
5103 if (!isBitShift && left->getBasicType() != right->getBasicType())
5104 {
5105 return false;
5106 }
5107
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005108 // Check that:
5109 // 1. Type sizes match exactly on ops that require that.
5110 // 2. Restrictions for structs that contain arrays or samplers are respected.
5111 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005112 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005113 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005114 case EOpAssign:
5115 case EOpInitialize:
5116 case EOpEqual:
5117 case EOpNotEqual:
5118 // ESSL 1.00 sections 5.7, 5.8, 5.9
5119 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5120 {
5121 error(loc, "undefined operation for structs containing arrays",
5122 GetOperatorString(op));
5123 return false;
5124 }
5125 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5126 // we interpret the spec so that this extends to structs containing samplers,
5127 // similarly to ESSL 1.00 spec.
5128 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5129 left->getType().isStructureContainingSamplers())
5130 {
5131 error(loc, "undefined operation for structs containing samplers",
5132 GetOperatorString(op));
5133 return false;
5134 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005135
Olli Etuahoe1805592017-01-02 16:41:20 +00005136 if ((left->getNominalSize() != right->getNominalSize()) ||
5137 (left->getSecondarySize() != right->getSecondarySize()))
5138 {
5139 error(loc, "dimension mismatch", GetOperatorString(op));
5140 return false;
5141 }
5142 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005143 case EOpLessThan:
5144 case EOpGreaterThan:
5145 case EOpLessThanEqual:
5146 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005147 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005148 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005149 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005150 return false;
5151 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005152 break;
5153 case EOpAdd:
5154 case EOpSub:
5155 case EOpDiv:
5156 case EOpIMod:
5157 case EOpBitShiftLeft:
5158 case EOpBitShiftRight:
5159 case EOpBitwiseAnd:
5160 case EOpBitwiseXor:
5161 case EOpBitwiseOr:
5162 case EOpAddAssign:
5163 case EOpSubAssign:
5164 case EOpDivAssign:
5165 case EOpIModAssign:
5166 case EOpBitShiftLeftAssign:
5167 case EOpBitShiftRightAssign:
5168 case EOpBitwiseAndAssign:
5169 case EOpBitwiseXorAssign:
5170 case EOpBitwiseOrAssign:
5171 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5172 {
5173 return false;
5174 }
5175
5176 // Are the sizes compatible?
5177 if (left->getNominalSize() != right->getNominalSize() ||
5178 left->getSecondarySize() != right->getSecondarySize())
5179 {
5180 // If the nominal sizes of operands do not match:
5181 // One of them must be a scalar.
5182 if (!left->isScalar() && !right->isScalar())
5183 return false;
5184
5185 // In the case of compound assignment other than multiply-assign,
5186 // the right side needs to be a scalar. Otherwise a vector/matrix
5187 // would be assigned to a scalar. A scalar can't be shifted by a
5188 // vector either.
5189 if (!right->isScalar() &&
5190 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5191 return false;
5192 }
5193 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005194 default:
5195 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005196 }
5197
Olli Etuahod6b14282015-03-17 14:31:35 +02005198 return true;
5199}
5200
Olli Etuaho1dded802016-08-18 18:13:13 +03005201bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5202 const TType &left,
5203 const TType &right)
5204{
5205 switch (op)
5206 {
5207 case EOpMul:
5208 case EOpMulAssign:
5209 return left.getNominalSize() == right.getNominalSize() &&
5210 left.getSecondarySize() == right.getSecondarySize();
5211 case EOpVectorTimesScalar:
5212 return true;
5213 case EOpVectorTimesScalarAssign:
5214 ASSERT(!left.isMatrix() && !right.isMatrix());
5215 return left.isVector() && !right.isVector();
5216 case EOpVectorTimesMatrix:
5217 return left.getNominalSize() == right.getRows();
5218 case EOpVectorTimesMatrixAssign:
5219 ASSERT(!left.isMatrix() && right.isMatrix());
5220 return left.isVector() && left.getNominalSize() == right.getRows() &&
5221 left.getNominalSize() == right.getCols();
5222 case EOpMatrixTimesVector:
5223 return left.getCols() == right.getNominalSize();
5224 case EOpMatrixTimesScalar:
5225 return true;
5226 case EOpMatrixTimesScalarAssign:
5227 ASSERT(left.isMatrix() && !right.isMatrix());
5228 return !right.isVector();
5229 case EOpMatrixTimesMatrix:
5230 return left.getCols() == right.getRows();
5231 case EOpMatrixTimesMatrixAssign:
5232 ASSERT(left.isMatrix() && right.isMatrix());
5233 // We need to check two things:
5234 // 1. The matrix multiplication step is valid.
5235 // 2. The result will have the same number of columns as the lvalue.
5236 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5237
5238 default:
5239 UNREACHABLE();
5240 return false;
5241 }
5242}
5243
Jamie Madillb98c3a82015-07-23 14:26:04 -04005244TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5245 TIntermTyped *left,
5246 TIntermTyped *right,
5247 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005248{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005249 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005250 return nullptr;
5251
Olli Etuahofc1806e2015-03-17 13:03:11 +02005252 switch (op)
5253 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005254 case EOpEqual:
5255 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005256 case EOpLessThan:
5257 case EOpGreaterThan:
5258 case EOpLessThanEqual:
5259 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005260 break;
5261 case EOpLogicalOr:
5262 case EOpLogicalXor:
5263 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005264 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5265 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005266 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005267 {
5268 return nullptr;
5269 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005270 // Basic types matching should have been already checked.
5271 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272 break;
5273 case EOpAdd:
5274 case EOpSub:
5275 case EOpDiv:
5276 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005277 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5278 !right->getType().getStruct());
5279 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005280 {
5281 return nullptr;
5282 }
5283 break;
5284 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005285 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5286 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005287 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005288 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 {
5290 return nullptr;
5291 }
5292 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005293 default:
5294 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005295 }
5296
Olli Etuaho1dded802016-08-18 18:13:13 +03005297 if (op == EOpMul)
5298 {
5299 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5300 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5301 {
5302 return nullptr;
5303 }
5304 }
5305
Olli Etuaho3fdec912016-08-18 15:08:06 +03005306 TIntermBinary *node = new TIntermBinary(op, left, right);
5307 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005308 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005309}
5310
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5312 TIntermTyped *left,
5313 TIntermTyped *right,
5314 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005315{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005316 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005317 if (node == 0)
5318 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005319 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5320 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005321 return left;
5322 }
5323 return node;
5324}
5325
Jamie Madillb98c3a82015-07-23 14:26:04 -04005326TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5327 TIntermTyped *left,
5328 TIntermTyped *right,
5329 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005330{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005331 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005332 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005333 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5335 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005336 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005337 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005338 }
5339 return node;
5340}
5341
Olli Etuaho13389b62016-10-16 11:48:18 +01005342TIntermBinary *TParseContext::createAssign(TOperator op,
5343 TIntermTyped *left,
5344 TIntermTyped *right,
5345 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005346{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005347 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005348 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005349 if (op == EOpMulAssign)
5350 {
5351 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5352 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5353 {
5354 return nullptr;
5355 }
5356 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005357 TIntermBinary *node = new TIntermBinary(op, left, right);
5358 node->setLine(loc);
5359
Olli Etuaho3fdec912016-08-18 15:08:06 +03005360 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005361 }
5362 return nullptr;
5363}
5364
Jamie Madillb98c3a82015-07-23 14:26:04 -04005365TIntermTyped *TParseContext::addAssign(TOperator op,
5366 TIntermTyped *left,
5367 TIntermTyped *right,
5368 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005369{
Olli Etuahocce89652017-06-19 16:04:09 +03005370 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005371 TIntermTyped *node = createAssign(op, left, right, loc);
5372 if (node == nullptr)
5373 {
5374 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005375 return left;
5376 }
5377 return node;
5378}
5379
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005380TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5381 TIntermTyped *right,
5382 const TSourceLoc &loc)
5383{
Corentin Wallez0d959252016-07-12 17:26:32 -04005384 // WebGL2 section 5.26, the following results in an error:
5385 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005386 if (mShaderSpec == SH_WEBGL2_SPEC &&
5387 (left->isArray() || left->getBasicType() == EbtVoid ||
5388 left->getType().isStructureContainingArrays() || right->isArray() ||
5389 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005390 {
5391 error(loc,
5392 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5393 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005394 }
5395
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005396 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005397
5398 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005399}
5400
Olli Etuaho49300862015-02-20 14:54:49 +02005401TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5402{
5403 switch (op)
5404 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005405 case EOpContinue:
5406 if (mLoopNestingLevel <= 0)
5407 {
5408 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005409 }
5410 break;
5411 case EOpBreak:
5412 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5413 {
5414 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005415 }
5416 break;
5417 case EOpReturn:
5418 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5419 {
5420 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005421 }
5422 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005423 case EOpKill:
5424 if (mShaderType != GL_FRAGMENT_SHADER)
5425 {
5426 error(loc, "discard supported in fragment shaders only", "discard");
5427 }
5428 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005429 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005430 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005431 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005432 }
Olli Etuahocce89652017-06-19 16:04:09 +03005433 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005434}
5435
Jamie Madillb98c3a82015-07-23 14:26:04 -04005436TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005437 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005438 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005439{
Olli Etuahocce89652017-06-19 16:04:09 +03005440 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005441 {
Olli Etuahocce89652017-06-19 16:04:09 +03005442 ASSERT(op == EOpReturn);
5443 mFunctionReturnsValue = true;
5444 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5445 {
5446 error(loc, "void function cannot return a value", "return");
5447 }
5448 else if (*mCurrentFunctionType != expression->getType())
5449 {
5450 error(loc, "function return is not matching type:", "return");
5451 }
Olli Etuaho49300862015-02-20 14:54:49 +02005452 }
Olli Etuahocce89652017-06-19 16:04:09 +03005453 TIntermBranch *node = new TIntermBranch(op, expression);
5454 node->setLine(loc);
5455 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005456}
5457
Martin Radev84aa2dc2017-09-11 15:51:02 +03005458void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5459{
5460 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005461 const TFunction *func = functionCall->getFunction();
5462 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005463 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005464 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005465 TIntermNode *componentNode = nullptr;
5466 TIntermSequence *arguments = functionCall->getSequence();
5467 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5468 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5469 ASSERT(sampler != nullptr);
5470 switch (sampler->getBasicType())
5471 {
5472 case EbtSampler2D:
5473 case EbtISampler2D:
5474 case EbtUSampler2D:
5475 case EbtSampler2DArray:
5476 case EbtISampler2DArray:
5477 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005478 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005479 (isTextureGatherOffset && arguments->size() == 4u))
5480 {
5481 componentNode = arguments->back();
5482 }
5483 break;
5484 case EbtSamplerCube:
5485 case EbtISamplerCube:
5486 case EbtUSamplerCube:
5487 ASSERT(!isTextureGatherOffset);
5488 if (arguments->size() == 3u)
5489 {
5490 componentNode = arguments->back();
5491 }
5492 break;
5493 case EbtSampler2DShadow:
5494 case EbtSampler2DArrayShadow:
5495 case EbtSamplerCubeShadow:
5496 break;
5497 default:
5498 UNREACHABLE();
5499 break;
5500 }
5501 if (componentNode)
5502 {
5503 const TIntermConstantUnion *componentConstantUnion =
5504 componentNode->getAsConstantUnion();
5505 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5506 {
5507 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005508 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005509 }
5510 else
5511 {
5512 int component = componentConstantUnion->getIConst(0);
5513 if (component < 0 || component > 3)
5514 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005515 error(functionCall->getLine(), "Component must be in the range [0;3]",
5516 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005517 }
5518 }
5519 }
5520 }
5521}
5522
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005523void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5524{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005525 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005526 const TFunction *func = functionCall->getFunction();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005527 TIntermNode *offset = nullptr;
5528 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005529 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005530 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005531 {
5532 offset = arguments->back();
5533 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005534 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005535 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005536 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005537 ASSERT(arguments->size() >= 3);
5538 offset = (*arguments)[2];
5539 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005540 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005541 {
5542 ASSERT(arguments->size() >= 3u);
5543 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5544 ASSERT(sampler != nullptr);
5545 switch (sampler->getBasicType())
5546 {
5547 case EbtSampler2D:
5548 case EbtISampler2D:
5549 case EbtUSampler2D:
5550 case EbtSampler2DArray:
5551 case EbtISampler2DArray:
5552 case EbtUSampler2DArray:
5553 offset = (*arguments)[2];
5554 break;
5555 case EbtSampler2DShadow:
5556 case EbtSampler2DArrayShadow:
5557 offset = (*arguments)[3];
5558 break;
5559 default:
5560 UNREACHABLE();
5561 break;
5562 }
5563 useTextureGatherOffsetConstraints = true;
5564 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005565 if (offset != nullptr)
5566 {
5567 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5568 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5569 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005570 error(functionCall->getLine(), "Texture offset must be a constant expression",
5571 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005572 }
5573 else
5574 {
5575 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5576 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005577 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005578 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5579 : mMinProgramTexelOffset;
5580 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5581 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005582 for (size_t i = 0u; i < size; ++i)
5583 {
5584 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005585 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005586 {
5587 std::stringstream tokenStream;
5588 tokenStream << offsetValue;
5589 std::string token = tokenStream.str();
5590 error(offset->getLine(), "Texture offset value out of valid range",
5591 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005592 }
5593 }
5594 }
5595 }
5596}
5597
Jiajia Qina3106c52017-11-03 09:39:39 +08005598void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5599{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005600 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005601 const TFunction *func = functionCall->getFunction();
5602 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005603 {
5604 TIntermSequence *arguments = functionCall->getSequence();
5605 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5606
5607 if (IsBufferOrSharedVariable(memNode))
5608 {
5609 return;
5610 }
5611
5612 while (memNode->getAsBinaryNode())
5613 {
5614 memNode = memNode->getAsBinaryNode()->getLeft();
5615 if (IsBufferOrSharedVariable(memNode))
5616 {
5617 return;
5618 }
5619 }
5620
5621 error(memNode->getLine(),
5622 "The value passed to the mem argument of an atomic memory function does not "
5623 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005624 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005625 }
5626}
5627
Martin Radev2cc85b32016-08-05 16:22:53 +03005628// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5629void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5630{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005631 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005632
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005633 const TFunction *func = functionCall->getFunction();
5634
5635 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005636 {
5637 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005638 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005639
Olli Etuaho485eefd2017-02-14 17:40:06 +00005640 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005641
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005642 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005643 {
5644 if (memoryQualifier.readonly)
5645 {
5646 error(imageNode->getLine(),
5647 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005648 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005649 }
5650 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005651 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005652 {
5653 if (memoryQualifier.writeonly)
5654 {
5655 error(imageNode->getLine(),
5656 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005657 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005658 }
5659 }
5660 }
5661}
5662
5663// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5664void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5665 const TFunction *functionDefinition,
5666 const TIntermAggregate *functionCall)
5667{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005668 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005669
5670 const TIntermSequence &arguments = *functionCall->getSequence();
5671
5672 ASSERT(functionDefinition->getParamCount() == arguments.size());
5673
5674 for (size_t i = 0; i < arguments.size(); ++i)
5675 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005676 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5677 const TType &functionArgumentType = typedArgument->getType();
Olli Etuahod4bd9632018-03-08 16:32:44 +02005678 const TType &functionParameterType = functionDefinition->getParam(i)->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005679 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5680
5681 if (IsImage(functionArgumentType.getBasicType()))
5682 {
5683 const TMemoryQualifier &functionArgumentMemoryQualifier =
5684 functionArgumentType.getMemoryQualifier();
5685 const TMemoryQualifier &functionParameterMemoryQualifier =
5686 functionParameterType.getMemoryQualifier();
5687 if (functionArgumentMemoryQualifier.readonly &&
5688 !functionParameterMemoryQualifier.readonly)
5689 {
5690 error(functionCall->getLine(),
5691 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005692 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005693 }
5694
5695 if (functionArgumentMemoryQualifier.writeonly &&
5696 !functionParameterMemoryQualifier.writeonly)
5697 {
5698 error(functionCall->getLine(),
5699 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005700 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005701 }
Martin Radev049edfa2016-11-11 14:35:37 +02005702
5703 if (functionArgumentMemoryQualifier.coherent &&
5704 !functionParameterMemoryQualifier.coherent)
5705 {
5706 error(functionCall->getLine(),
5707 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005708 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005709 }
5710
5711 if (functionArgumentMemoryQualifier.volatileQualifier &&
5712 !functionParameterMemoryQualifier.volatileQualifier)
5713 {
5714 error(functionCall->getLine(),
5715 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005716 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005717 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005718 }
5719 }
5720}
5721
Olli Etuaho95ed1942018-02-01 14:01:19 +02005722TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005723{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005724 if (fnCall->thisNode() != nullptr)
5725 {
5726 return addMethod(fnCall, loc);
5727 }
5728 if (fnCall->isConstructor())
5729 {
5730 return addConstructor(fnCall, loc);
5731 }
5732 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005733}
5734
Olli Etuaho95ed1942018-02-01 14:01:19 +02005735TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005736{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005737 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005738 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5739 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5740 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005741 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005742 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005743 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005744 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005745 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005746 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005747 {
5748 error(loc, "method takes no parameters", "length");
5749 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005750 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751 {
5752 error(loc, "length can only be called on arrays", "length");
5753 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005754 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005755 mGeometryShaderInputPrimitiveType == EptUndefined)
5756 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005757 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005758 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5759 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005760 else
5761 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005762 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005763 node->setLine(loc);
5764 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005765 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005766 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767}
5768
Olli Etuaho95ed1942018-02-01 14:01:19 +02005769TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005770 const TSourceLoc &loc)
5771{
Olli Etuaho697bf652018-02-16 11:50:54 +02005772 // First check whether the function has been hidden by a variable name or struct typename by
5773 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5774 // with a matching argument list.
5775 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005777 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005778 }
5779 else
5780 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005781 // There are no inner functions, so it's enough to look for user-defined functions in the
5782 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005783 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005784 if (symbol != nullptr)
5785 {
5786 // A user-defined function - could be an overloaded built-in as well.
5787 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5788 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5789 TIntermAggregate *callNode =
5790 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5791 callNode->setLine(loc);
5792 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5793 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5794 return callNode;
5795 }
5796
5797 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005798 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005799 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005800 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005801 }
5802 else
5803 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005804 // A built-in function.
5805 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005806 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005807
Olli Etuaho37b697e2018-01-29 12:19:27 +02005808 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005809 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005810 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005811 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005813 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005814 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005815 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005816 if (fnCandidate->getParamCount() == 1)
5817 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005818 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005819 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005820 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005821 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005823 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005824 TIntermAggregate *callNode =
5825 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 callNode->setLine(loc);
5827
Olli Etuahoe80825e2018-02-16 10:24:53 +02005828 // Some built-in functions have out parameters too.
5829 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5830
5831 // See if we can constant fold a built-in. Note that this may be possible
5832 // even if it is not const-qualified.
5833 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005835
5836 // This is a built-in function with no op associated with it.
5837 TIntermAggregate *callNode =
5838 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5839 callNode->setLine(loc);
5840 checkTextureOffsetConst(callNode);
5841 checkTextureGather(callNode);
5842 checkImageMemoryAccessForBuiltinFunctions(callNode);
5843 checkAtomicMemoryBuiltinFunctions(callNode);
5844 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5845 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005846 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848
5849 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005850 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851}
5852
Jamie Madillb98c3a82015-07-23 14:26:04 -04005853TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005854 TIntermTyped *trueExpression,
5855 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005856 const TSourceLoc &loc)
5857{
Olli Etuaho56229f12017-07-10 14:16:33 +03005858 if (!checkIsScalarBool(loc, cond))
5859 {
5860 return falseExpression;
5861 }
Olli Etuaho52901742015-04-15 13:42:45 +03005862
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005863 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005864 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005865 std::stringstream reasonStream;
5866 reasonStream << "mismatching ternary operator operand types '"
5867 << trueExpression->getCompleteString() << " and '"
5868 << falseExpression->getCompleteString() << "'";
5869 std::string reason = reasonStream.str();
5870 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005871 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005872 }
Olli Etuahode318b22016-10-25 16:18:25 +01005873 if (IsOpaqueType(trueExpression->getBasicType()))
5874 {
5875 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005876 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005877 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5878 // Note that structs containing opaque types don't need to be checked as structs are
5879 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005880 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005881 return falseExpression;
5882 }
5883
Jiajia Qinbc585152017-06-23 15:42:17 +08005884 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5885 falseExpression->getMemoryQualifier().writeonly)
5886 {
5887 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5888 return falseExpression;
5889 }
5890
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005891 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005892 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005893 // ESSL 3.00.6 section 5.7:
5894 // Ternary operator support is optional for arrays. No certainty that it works across all
5895 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5896 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005897 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005898 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005899 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005900 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005901 }
Olli Etuaho94050052017-05-08 14:17:44 +03005902 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5903 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005904 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005905 return falseExpression;
5906 }
5907
Corentin Wallez0d959252016-07-12 17:26:32 -04005908 // WebGL2 section 5.26, the following results in an error:
5909 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005910 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005911 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005912 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005913 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005914 }
5915
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005916 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5917 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005918 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005919}
Olli Etuaho49300862015-02-20 14:54:49 +02005920
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005921//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005922// Parse an array of strings using yyparse.
5923//
5924// Returns 0 for success.
5925//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005926int PaParseStrings(size_t count,
5927 const char *const string[],
5928 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305929 TParseContext *context)
5930{
Yunchao He4f285442017-04-21 12:15:49 +08005931 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005932 return 1;
5933
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005934 if (glslang_initialize(context))
5935 return 1;
5936
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005937 int error = glslang_scan(count, string, length, context);
5938 if (!error)
5939 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005940
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005941 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005942
alokp@chromium.org6b495712012-06-29 00:06:58 +00005943 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005944}
Jamie Madill45bcc782016-11-07 13:58:48 -05005945
5946} // namespace sh