blob: d6700456cf59258e6bf485adb4f19281b4947fe0 [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 {
1678 TQualifier qual = fnCandidate->getParam(i).type->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 {
3158 const TConstParameter &param = function.getParam(i);
3159
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003160 TIntermSymbol *symbol = nullptr;
3161
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003162 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3163 // be used for unused args).
3164 if (param.name != nullptr)
3165 {
Olli Etuaho195be942017-12-04 23:40:14 +02003166 TVariable *variable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003167 new TVariable(&symbolTable, param.name, param.type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003168 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003169 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003171 {
Olli Etuaho437664b2018-02-28 15:38:14 +02003172 if (!symbolTable.declare(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003173 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003174 error(location, "redefinition", param.name);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003175 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003176 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003177 // Unsized type of a named parameter should have already been checked and sanitized.
3178 ASSERT(!param.type->isUnsizedArray());
3179 }
3180 else
3181 {
3182 if (param.type->isUnsizedArray())
3183 {
3184 error(location, "function parameter array must be sized at compile time", "[]");
3185 // We don't need to size the arrays since the parameter is unnamed and hence
3186 // inaccessible.
3187 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003188 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003189 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003190 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003191 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3192 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003193 TVariable *emptyVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003194 new TVariable(&symbolTable, ImmutableString(""), param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003195 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003197 symbol->setLine(location);
3198 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003199 }
3200 return prototype;
3201}
3202
Olli Etuaho16c745a2017-01-16 17:02:27 +00003203TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3204 const TFunction &parsedFunction,
3205 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003206{
Olli Etuaho476197f2016-10-11 13:59:08 +01003207 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3208 // first declaration. Either way the instance in the symbol table is used to track whether the
3209 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003210 bool hadPrototypeDeclaration = false;
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003211 const TFunction *function = symbolTable.markFunctionHasPrototypeDeclaration(
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003212 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3213
3214 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003215 {
3216 // ESSL 1.00.17 section 4.2.7.
3217 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3218 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003219 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003220
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003221 TIntermFunctionPrototype *prototype =
3222 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003223
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003224 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003225
3226 if (!symbolTable.atGlobalLevel())
3227 {
3228 // ESSL 3.00.4 section 4.2.4.
3229 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003230 }
3231
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232 return prototype;
3233}
3234
Olli Etuaho336b1472016-10-05 16:37:55 +01003235TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003236 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003237 TIntermBlock *functionBody,
3238 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003239{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003240 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003241 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3242 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003243 error(location,
3244 "function does not return a value:", functionPrototype->getFunction()->name());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245 }
3246
Olli Etuahof51fdd22016-10-03 10:03:40 +01003247 if (functionBody == nullptr)
3248 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003249 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003250 functionBody->setLine(location);
3251 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003252 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003253 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003254 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003255
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003256 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003257 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003258}
3259
Olli Etuaho476197f2016-10-11 13:59:08 +01003260void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003261 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003262 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003263{
Olli Etuaho476197f2016-10-11 13:59:08 +01003264 ASSERT(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003265
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003266 bool wasDefined = false;
3267 function = symbolTable.setFunctionParameterNamesFromDefinition(function, &wasDefined);
3268 if (wasDefined)
Jamie Madill185fb402015-06-12 15:48:48 -04003269 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003270 error(location, "function already has a body", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003271 }
Jamie Madill185fb402015-06-12 15:48:48 -04003272
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003273 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003274 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003275 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003276
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003277 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003278 setLoopNestingLevel(0);
3279}
3280
Jamie Madillb98c3a82015-07-23 14:26:04 -04003281TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003282{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003283 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003284 // We don't know at this point whether this is a function definition or a prototype.
3285 // The definition production code will check for redefinitions.
3286 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003287 //
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303288
Olli Etuahod80f2942017-11-06 12:44:45 +02003289 for (size_t i = 0u; i < function->getParamCount(); ++i)
3290 {
3291 auto &param = function->getParam(i);
3292 if (param.type->isStructSpecifier())
3293 {
3294 // ESSL 3.00.6 section 12.10.
3295 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003296 function->name());
Olli Etuahod80f2942017-11-06 12:44:45 +02003297 }
3298 }
3299
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003300 if (getShaderVersion() >= 300)
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303301 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003302 const UnmangledBuiltIn *builtIn =
3303 symbolTable.getUnmangledBuiltInForShaderVersion(function->name(), getShaderVersion());
3304 if (builtIn &&
3305 (builtIn->extension == TExtension::UNDEFINED || isExtensionEnabled(builtIn->extension)))
3306 {
3307 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as
3308 // functions. Therefore overloading or redefining builtin functions is an error.
3309 error(location, "Name of a built-in function cannot be redeclared as function",
3310 function->name());
3311 }
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303312 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +02003313 else
3314 {
3315 // ESSL 1.00.17 section 4.2.6: built-ins can be overloaded but not redefined. We assume that
3316 // this applies to redeclarations as well.
3317 const TSymbol *builtIn =
3318 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
3319 if (builtIn)
3320 {
3321 error(location, "built-in functions cannot be redefined", function->name());
3322 }
3323 }
3324
3325 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3326 // here.
3327 const TFunction *prevDec =
3328 static_cast<const TFunction *>(symbolTable.findGlobal(function->getMangledName()));
3329 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003330 {
3331 if (prevDec->getReturnType() != function->getReturnType())
3332 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003333 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003334 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003335 }
3336 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003338 if (prevDec->getParam(i).type->getQualifier() !=
3339 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003340 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003341 error(location,
3342 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003343 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003344 }
3345 }
3346 }
3347
Jamie Madill185fb402015-06-12 15:48:48 -04003348 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003349 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3350 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003351 if (prevSym)
3352 {
3353 if (!prevSym->isFunction())
3354 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003355 error(location, "redefinition of a function", function->name());
Jamie Madill185fb402015-06-12 15:48:48 -04003356 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003357 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003359 // Parsing is at the inner scope level of the function's arguments and body statement at this
3360 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3361 // scope.
3362 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003363
Olli Etuaho78d13742017-01-18 13:06:10 +00003364 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahofbb1c792018-01-19 16:26:59 +02003365 if (function->isMain())
Olli Etuaho78d13742017-01-18 13:06:10 +00003366 {
3367 if (function->getParamCount() > 0)
3368 {
3369 error(location, "function cannot take any parameter(s)", "main");
3370 }
3371 if (function->getReturnType().getBasicType() != EbtVoid)
3372 {
3373 error(location, "main function cannot return a value",
3374 function->getReturnType().getBasicString());
3375 }
3376 }
3377
Jamie Madill185fb402015-06-12 15:48:48 -04003378 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003379 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3380 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003381 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3382 //
3383 return function;
3384}
3385
Olli Etuaho9de84a52016-06-14 17:36:01 +03003386TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003387 const ImmutableString &name,
Olli Etuaho9de84a52016-06-14 17:36:01 +03003388 const TSourceLoc &location)
3389{
3390 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3391 {
3392 error(location, "no qualifiers allowed for function return",
3393 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003394 }
3395 if (!type.layoutQualifier.isEmpty())
3396 {
3397 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003398 }
jchen10cc2a10e2017-05-03 14:05:12 +08003399 // make sure an opaque type is not involved as well...
3400 std::string reason(getBasicString(type.getBasicType()));
3401 reason += "s can't be function return values";
3402 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003403 if (mShaderVersion < 300)
3404 {
3405 // Array return values are forbidden, but there's also no valid syntax for declaring array
3406 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003407 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003408
3409 if (type.isStructureContainingArrays())
3410 {
3411 // ESSL 1.00.17 section 6.1 Function Definitions
3412 error(location, "structures containing arrays can't be function return values",
3413 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003414 }
3415 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003416
3417 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho029e8ca2018-02-16 14:06:49 +02003418 return new TFunction(&symbolTable, name, SymbolType::UserDefined, new TType(type), false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419}
3420
Olli Etuaho697bf652018-02-16 11:50:54 +02003421TFunctionLookup *TParseContext::addNonConstructorFunc(const ImmutableString &name,
3422 const TSymbol *symbol)
Olli Etuahocce89652017-06-19 16:04:09 +03003423{
Olli Etuaho697bf652018-02-16 11:50:54 +02003424 return TFunctionLookup::CreateFunctionCall(name, symbol);
Olli Etuahocce89652017-06-19 16:04:09 +03003425}
3426
Olli Etuaho95ed1942018-02-01 14:01:19 +02003427TFunctionLookup *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003428{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003429 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003430 {
3431 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3432 "[]");
3433 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003434 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003435 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003436 error(publicType.getLine(), "constructor can't be a structure definition",
3437 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003438 }
3439
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003440 TType *type = new TType(publicType);
3441 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003442 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003443 error(publicType.getLine(), "cannot construct this type",
3444 getBasicString(publicType.getBasicType()));
3445 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003446 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003447 return TFunctionLookup::CreateConstructor(type);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003448}
3449
Olli Etuaho55bde912017-10-25 13:41:13 +03003450void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3451 const char *errorMessage,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003452 const ImmutableString &token,
Olli Etuaho55bde912017-10-25 13:41:13 +03003453 TType *arrayType)
3454{
3455 if (arrayType->isUnsizedArray())
3456 {
3457 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003458 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003459 }
3460}
3461
3462TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003463 const ImmutableString &name,
Olli Etuahocce89652017-06-19 16:04:09 +03003464 const TSourceLoc &nameLoc)
3465{
Olli Etuaho55bde912017-10-25 13:41:13 +03003466 ASSERT(type);
Olli Etuahofbb1c792018-01-19 16:26:59 +02003467 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03003468 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003469 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003470 error(nameLoc, "illegal use of type 'void'", name);
Olli Etuahocce89652017-06-19 16:04:09 +03003471 }
Olli Etuahofbb1c792018-01-19 16:26:59 +02003472 checkIsNotReserved(nameLoc, name);
3473 TParameter param = {name.data(), type};
Olli Etuahocce89652017-06-19 16:04:09 +03003474 return param;
3475}
3476
Olli Etuaho55bde912017-10-25 13:41:13 +03003477TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003478 const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003479 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003480{
Olli Etuaho55bde912017-10-25 13:41:13 +03003481 TType *type = new TType(publicType);
3482 return parseParameterDeclarator(type, name, nameLoc);
3483}
3484
Olli Etuahofbb1c792018-01-19 16:26:59 +02003485TParameter TParseContext::parseParameterArrayDeclarator(const ImmutableString &name,
Olli Etuaho55bde912017-10-25 13:41:13 +03003486 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003487 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003488 const TSourceLoc &arrayLoc,
3489 TPublicType *elementType)
3490{
3491 checkArrayElementIsNotArray(arrayLoc, *elementType);
3492 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003493 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003494 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003495}
3496
Olli Etuaho95ed1942018-02-01 14:01:19 +02003497bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(
3498 const TIntermSequence &arguments,
3499 TType type,
3500 const TSourceLoc &line)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003501{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003502 if (arguments.empty())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003503 {
3504 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3505 return false;
3506 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003507 for (TIntermNode *arg : arguments)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003508 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003509 const TIntermTyped *element = arg->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003510 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003511 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3512 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003513 {
3514 error(line, "constructing from a non-dereferenced array", "constructor");
3515 return false;
3516 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003517 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003518 {
3519 if (dimensionalityFromElement == 1u)
3520 {
3521 error(line, "implicitly sized array of arrays constructor argument is not an array",
3522 "constructor");
3523 }
3524 else
3525 {
3526 error(line,
3527 "implicitly sized array of arrays constructor argument dimensionality is too "
3528 "low",
3529 "constructor");
3530 }
3531 return false;
3532 }
3533 }
3534 return true;
3535}
3536
Jamie Madillb98c3a82015-07-23 14:26:04 -04003537// This function is used to test for the correctness of the parameters passed to various constructor
3538// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003539//
Olli Etuaho856c4972016-08-08 11:38:39 +03003540// 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 +00003541//
Olli Etuaho95ed1942018-02-01 14:01:19 +02003542TIntermTyped *TParseContext::addConstructor(TFunctionLookup *fnCall, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003543{
Olli Etuaho95ed1942018-02-01 14:01:19 +02003544 TType type = fnCall->constructorType();
3545 TIntermSequence &arguments = fnCall->arguments();
Olli Etuaho856c4972016-08-08 11:38:39 +03003546 if (type.isUnsizedArray())
3547 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003549 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003550 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003551 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003552 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02003553 TIntermTyped *firstElement = arguments.at(0)->getAsTyped();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003555 if (type.getOutermostArraySize() == 0u)
3556 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02003557 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments.size()));
Olli Etuaho9cd71632017-10-26 14:43:20 +03003558 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003559 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003560 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003561 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003562 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003563 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003564 }
3565 }
3566 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003567 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003568
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003569 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003570 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003571 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003572 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003573
Olli Etuaho95ed1942018-02-01 14:01:19 +02003574 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, &arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003575 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003576
Olli Etuaho765924f2018-01-04 12:48:36 +02003577 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003578}
3579
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003580//
3581// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003582// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583//
Olli Etuaho13389b62016-10-16 11:48:18 +01003584TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003585 const TTypeQualifierBuilder &typeQualifierBuilder,
3586 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003587 const ImmutableString &blockName,
Martin Radev70866b82016-07-22 15:27:42 +03003588 TFieldList *fieldList,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003589 const ImmutableString &instanceName,
Martin Radev70866b82016-07-22 15:27:42 +03003590 const TSourceLoc &instanceLine,
3591 TIntermTyped *arrayIndex,
3592 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593{
Olli Etuaho856c4972016-08-08 11:38:39 +03003594 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003595
Olli Etuaho77ba4082016-12-16 12:01:18 +00003596 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003597
Jiajia Qinbc585152017-06-23 15:42:17 +08003598 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003599 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003600 error(typeQualifier.line,
3601 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3602 "3.10",
3603 getQualifierString(typeQualifier.qualifier));
3604 }
3605 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3606 {
3607 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003608 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609 }
3610
Martin Radev70866b82016-07-22 15:27:42 +03003611 if (typeQualifier.invariant)
3612 {
3613 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3614 }
3615
Jiajia Qinbc585152017-06-23 15:42:17 +08003616 if (typeQualifier.qualifier != EvqBuffer)
3617 {
3618 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3619 }
Olli Etuaho43364892017-02-13 16:00:12 +00003620
jchen10af713a22017-04-19 09:10:56 +08003621 // add array index
3622 unsigned int arraySize = 0;
3623 if (arrayIndex != nullptr)
3624 {
3625 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3626 }
3627
3628 if (mShaderVersion < 310)
3629 {
3630 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3631 }
3632 else
3633 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003634 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3635 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003636 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003637
Andrei Volykhina5527072017-03-22 16:46:30 +03003638 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3639
Jamie Madill099c0f32013-06-20 11:55:52 -04003640 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003641 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003642 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3643 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003644
Jamie Madill099c0f32013-06-20 11:55:52 -04003645 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3646 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003647 if (typeQualifier.qualifier == EvqUniform)
3648 {
3649 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3650 }
3651 else if (typeQualifier.qualifier == EvqBuffer)
3652 {
3653 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3654 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003655 }
3656
Jamie Madill1566ef72013-06-20 11:55:54 -04003657 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3658 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 if (typeQualifier.qualifier == EvqUniform)
3660 {
3661 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3662 }
3663 else if (typeQualifier.qualifier == EvqBuffer)
3664 {
3665 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3666 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003667 }
3668
Olli Etuaho856c4972016-08-08 11:38:39 +03003669 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003670
Martin Radev2cc85b32016-08-05 16:22:53 +03003671 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3672
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303674 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3675 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003676 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303677 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003678 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303679 {
jchen10cc2a10e2017-05-03 14:05:12 +08003680 std::string reason("unsupported type - ");
3681 reason += fieldType->getBasicString();
3682 reason += " types are not allowed in interface blocks";
3683 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003684 }
3685
Jamie Madill98493dd2013-07-08 14:39:03 -04003686 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003687 switch (qualifier)
3688 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003689 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003690 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003691 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003692 if (typeQualifier.qualifier == EvqBuffer)
3693 {
3694 error(field->line(), "invalid qualifier on shader storage block member",
3695 getQualifierString(qualifier));
3696 }
3697 break;
3698 case EvqBuffer:
3699 if (typeQualifier.qualifier == EvqUniform)
3700 {
3701 error(field->line(), "invalid qualifier on uniform block member",
3702 getQualifierString(qualifier));
3703 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003704 break;
3705 default:
3706 error(field->line(), "invalid qualifier on interface block member",
3707 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003708 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003709 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003710
Martin Radev70866b82016-07-22 15:27:42 +03003711 if (fieldType->isInvariant())
3712 {
3713 error(field->line(), "invalid qualifier on interface block member", "invariant");
3714 }
3715
Jamie Madilla5efff92013-06-06 11:56:47 -04003716 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003717 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003718 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003719 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003722 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003723 error(field->line(), "invalid layout qualifier: cannot be used here",
3724 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003725 }
3726
Jamie Madill98493dd2013-07-08 14:39:03 -04003727 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003728 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003729 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003730 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003731 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003732 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003733 warning(field->line(),
3734 "extraneous layout qualifier: only has an effect on matrix types",
3735 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003736 }
3737
Jamie Madill98493dd2013-07-08 14:39:03 -04003738 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003739
Olli Etuahoebee5b32017-11-23 12:56:32 +02003740 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3741 typeQualifier.qualifier != EvqBuffer)
3742 {
3743 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3744 checkIsNotUnsizedArray(field->line(),
3745 "array members of interface blocks must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003746 field->name(), field->type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02003747 }
3748
Jiajia Qinbc585152017-06-23 15:42:17 +08003749 if (typeQualifier.qualifier == EvqBuffer)
3750 {
3751 // set memory qualifiers
3752 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3753 // qualified with a memory qualifier, it is as if all of its members were declared with
3754 // the same memory qualifier.
3755 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3756 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3757 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3758 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3759 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3760 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3761 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3762 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3763 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3764 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3765 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003766 }
3767
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003768 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
Olli Etuahofbb1c792018-01-19 16:26:59 +02003769 &symbolTable, blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003770 if (!symbolTable.declare(interfaceBlock))
Olli Etuaho378c3a52017-12-04 11:32:13 +02003771 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003772 error(nameLine, "redefinition of an interface block name", blockName);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003773 }
3774
Olli Etuahob60d30f2018-01-16 12:31:06 +02003775 TType *interfaceBlockType =
3776 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003777 if (arrayIndex != nullptr)
3778 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003779 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003780 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003781
Olli Etuaho195be942017-12-04 23:40:14 +02003782 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003783 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3784 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003785 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003786 new TVariable(&symbolTable, instanceName, interfaceBlockType,
Olli Etuahofbb1c792018-01-19 16:26:59 +02003787 instanceName.empty() ? SymbolType::Empty : SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003788
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003789 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003790 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003791 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003792 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003794 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003795 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003796
3797 // set parent pointer of the field variable
3798 fieldType->setInterfaceBlock(interfaceBlock);
3799
Olli Etuahob60d30f2018-01-16 12:31:06 +02003800 fieldType->setQualifier(typeQualifier.qualifier);
3801
Olli Etuaho195be942017-12-04 23:40:14 +02003802 TVariable *fieldVariable =
Olli Etuahofbb1c792018-01-19 16:26:59 +02003803 new TVariable(&symbolTable, field->name(), fieldType, SymbolType::UserDefined);
Olli Etuaho437664b2018-02-28 15:38:14 +02003804 if (!symbolTable.declare(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303805 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003806 error(field->line(), "redefinition of an interface block member name",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003807 field->name());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003808 }
3809 }
3810 }
3811 else
3812 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003813 checkIsNotReserved(instanceLine, instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003814
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 // add a symbol for this interface block
Olli Etuaho437664b2018-02-28 15:38:14 +02003816 if (!symbolTable.declare(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303817 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003818 error(instanceLine, "redefinition of an interface block instance name", instanceName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003819 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003820 }
3821
Olli Etuaho195be942017-12-04 23:40:14 +02003822 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3823 blockSymbol->setLine(typeQualifier.line);
3824 TIntermDeclaration *declaration = new TIntermDeclaration();
3825 declaration->appendDeclarator(blockSymbol);
3826 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003827
3828 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003829 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003830}
3831
Olli Etuahofbb1c792018-01-19 16:26:59 +02003832void TParseContext::enterStructDeclaration(const TSourceLoc &line,
3833 const ImmutableString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003834{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003835 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003836
3837 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003838 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303839 if (mStructNestingLevel > 1)
3840 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003841 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003842 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003843}
3844
3845void TParseContext::exitStructDeclaration()
3846{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003847 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003848}
3849
Olli Etuaho8a176262016-08-16 14:23:01 +03003850void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003851{
Jamie Madillacb4b812016-11-07 13:50:29 -05003852 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303853 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003854 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003855 }
3856
Arun Patole7e7e68d2015-05-22 12:02:25 +05303857 if (field.type()->getBasicType() != EbtStruct)
3858 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003859 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003860 }
3861
3862 // We're already inside a structure definition at this point, so add
3863 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3865 {
Jamie Madill41a49272014-03-18 16:10:13 -04003866 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003867 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3868 {
3869 // This may happen in case there are nested struct definitions. While they are also
3870 // invalid GLSL, they don't cause a syntax error.
3871 reasonStream << "Struct nesting";
3872 }
3873 else
3874 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02003875 reasonStream << "Reference of struct type " << field.type()->getStruct()->name();
Olli Etuahof0957992017-12-22 11:10:04 +02003876 }
3877 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003878 std::string reason = reasonStream.str();
Olli Etuahofbb1c792018-01-19 16:26:59 +02003879 error(line, reason.c_str(), field.name());
Olli Etuaho8a176262016-08-16 14:23:01 +03003880 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882}
3883
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003884//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003885// Parse an array index expression
3886//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003887TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3888 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303889 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003890{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003891 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3892 {
3893 if (baseExpression->getAsSymbolNode())
3894 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303895 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuahofbb1c792018-01-19 16:26:59 +02003896 baseExpression->getAsSymbolNode()->getName());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003897 }
3898 else
3899 {
3900 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3901 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003902
Olli Etuaho3ec75682017-07-05 17:02:55 +03003903 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003904 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003905
Jiawei Shaod8105a02017-08-08 09:54:36 +08003906 if (baseExpression->getQualifier() == EvqPerVertexIn)
3907 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003908 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003909 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3910 {
3911 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3912 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3913 }
3914 }
3915
Jamie Madill21c1e452014-12-29 11:33:41 -05003916 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3917
Olli Etuaho36b05142015-11-12 13:10:42 +02003918 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3919 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3920 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3921 // index is a constant expression.
3922 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3923 {
3924 if (baseExpression->isInterfaceBlock())
3925 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003926 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003927 switch (baseExpression->getQualifier())
3928 {
3929 case EvqPerVertexIn:
3930 break;
3931 case EvqUniform:
3932 case EvqBuffer:
3933 error(location,
3934 "array indexes for uniform block arrays and shader storage block arrays "
3935 "must be constant integral expressions",
3936 "[");
3937 break;
3938 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003939 // We can reach here only in error cases.
3940 ASSERT(mDiagnostics->numErrors() > 0);
3941 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003942 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003943 }
3944 else if (baseExpression->getQualifier() == EvqFragmentOut)
3945 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003946 error(location,
3947 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003948 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003949 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3950 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003951 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003952 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003953 }
3954
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003955 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003956 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003957 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3958 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3959 // constant fold expressions that are not constant expressions). The most compatible way to
3960 // handle this case is to report a warning instead of an error and force the index to be in
3961 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003962 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003963 int index = 0;
3964 if (indexConstantUnion->getBasicType() == EbtInt)
3965 {
3966 index = indexConstantUnion->getIConst(0);
3967 }
3968 else if (indexConstantUnion->getBasicType() == EbtUInt)
3969 {
3970 index = static_cast<int>(indexConstantUnion->getUConst(0));
3971 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003972
3973 int safeIndex = -1;
3974
Olli Etuahoebee5b32017-11-23 12:56:32 +02003975 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003976 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003977 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3978 safeIndex = 0;
3979 }
3980
3981 if (!baseExpression->getType().isUnsizedArray())
3982 {
3983 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003984 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003985 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003986 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003987 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3988 {
3989 outOfRangeError(outOfRangeIndexIsError, location,
3990 "array index for gl_FragData must be zero when "
3991 "GL_EXT_draw_buffers is disabled",
3992 "[]");
3993 safeIndex = 0;
3994 }
3995 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02003996 }
3997 // Only do generic out-of-range check if similar error hasn't already been reported.
3998 if (safeIndex < 0)
3999 {
4000 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004001 {
4002 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4003 baseExpression->getOutermostArraySize(),
4004 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004005 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004006 else if (baseExpression->isMatrix())
4007 {
4008 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4009 baseExpression->getType().getCols(),
4010 "matrix field selection out of range");
4011 }
4012 else
4013 {
4014 ASSERT(baseExpression->isVector());
4015 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4016 baseExpression->getType().getNominalSize(),
4017 "vector field selection out of range");
4018 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004020
Olli Etuahoebee5b32017-11-23 12:56:32 +02004021 ASSERT(safeIndex >= 0);
4022 // Data of constant unions can't be changed, because it may be shared with other
4023 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4024 // sanitized object.
4025 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4026 {
4027 TConstantUnion *safeConstantUnion = new TConstantUnion();
4028 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004029 indexExpression = new TIntermConstantUnion(
4030 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4031 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004032 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004033
Olli Etuahoebee5b32017-11-23 12:56:32 +02004034 TIntermBinary *node =
4035 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4036 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004037 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004038 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004039 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004040
4041 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4042 node->setLine(location);
4043 // Indirect indexing can never be constant folded.
4044 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004045}
4046
Olli Etuahoebee5b32017-11-23 12:56:32 +02004047int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4048 const TSourceLoc &location,
4049 int index,
4050 int arraySize,
4051 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004052{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004053 // Should not reach here with an unsized / runtime-sized array.
4054 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004055 // A negative index should already have been checked.
4056 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004057 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004058 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004059 std::stringstream reasonStream;
4060 reasonStream << reason << " '" << index << "'";
4061 std::string token = reasonStream.str();
4062 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004064 }
4065 return index;
4066}
4067
Jamie Madillb98c3a82015-07-23 14:26:04 -04004068TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4069 const TSourceLoc &dotLocation,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004070 const ImmutableString &fieldString,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004071 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004073 if (baseExpression->isArray())
4074 {
4075 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004076 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004077 }
4078
4079 if (baseExpression->isVector())
4080 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004081 TVector<int> fieldOffsets;
4082 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4083 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004084 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004085 fieldOffsets.resize(1);
4086 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004087 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004088 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4089 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004090
Olli Etuaho765924f2018-01-04 12:48:36 +02004091 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004092 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004093 else if (baseExpression->getBasicType() == EbtStruct)
4094 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304095 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004096 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004097 {
4098 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004099 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100 }
4101 else
4102 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004103 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004105 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004106 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004107 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 {
4109 fieldFound = true;
4110 break;
4111 }
4112 }
4113 if (fieldFound)
4114 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004115 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004116 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004117 TIntermBinary *node =
4118 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4119 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004120 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121 }
4122 else
4123 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004124 error(dotLocation, " no such field in structure", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004125 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 }
4127 }
4128 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004130 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304131 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004132 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004133 {
4134 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004135 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004136 }
4137 else
4138 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004139 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004140 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004141 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004142 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004143 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004144 {
4145 fieldFound = true;
4146 break;
4147 }
4148 }
4149 if (fieldFound)
4150 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004151 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004152 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004153 TIntermBinary *node =
4154 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4155 node->setLine(dotLocation);
4156 // Indexing interface blocks can never be constant folded.
4157 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004158 }
4159 else
4160 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004161 error(dotLocation, " no such field in interface block", fieldString);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004163 }
4164 }
4165 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004166 else
4167 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004168 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004170 error(dotLocation, " field selection requires structure or vector on left hand side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004171 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004172 }
4173 else
4174 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304175 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004176 " field selection requires structure, vector, or interface block on left hand "
4177 "side",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004178 fieldString);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004179 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004180 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004181 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004182}
4183
Olli Etuahofbb1c792018-01-19 16:26:59 +02004184TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004186{
Jamie Madill2f294c92017-11-20 14:47:26 -05004187 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004188
4189 if (qualifierType == "shared")
4190 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004191 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004192 {
4193 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4194 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004195 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004196 }
4197 else if (qualifierType == "packed")
4198 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004199 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004200 {
4201 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4202 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004203 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004204 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004205 else if (qualifierType == "std430")
4206 {
4207 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4208 qualifier.blockStorage = EbsStd430;
4209 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004210 else if (qualifierType == "std140")
4211 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004212 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004213 }
4214 else if (qualifierType == "row_major")
4215 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004216 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217 }
4218 else if (qualifierType == "column_major")
4219 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004220 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004221 }
4222 else if (qualifierType == "location")
4223 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004224 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004225 qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004226 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004227 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004228 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004229 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4230 {
4231 qualifier.yuv = true;
4232 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004233 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004234 else if (qualifierType == "rgba32f")
4235 {
4236 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4237 qualifier.imageInternalFormat = EiifRGBA32F;
4238 }
4239 else if (qualifierType == "rgba16f")
4240 {
4241 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4242 qualifier.imageInternalFormat = EiifRGBA16F;
4243 }
4244 else if (qualifierType == "r32f")
4245 {
4246 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4247 qualifier.imageInternalFormat = EiifR32F;
4248 }
4249 else if (qualifierType == "rgba8")
4250 {
4251 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4252 qualifier.imageInternalFormat = EiifRGBA8;
4253 }
4254 else if (qualifierType == "rgba8_snorm")
4255 {
4256 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4257 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4258 }
4259 else if (qualifierType == "rgba32i")
4260 {
4261 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4262 qualifier.imageInternalFormat = EiifRGBA32I;
4263 }
4264 else if (qualifierType == "rgba16i")
4265 {
4266 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4267 qualifier.imageInternalFormat = EiifRGBA16I;
4268 }
4269 else if (qualifierType == "rgba8i")
4270 {
4271 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4272 qualifier.imageInternalFormat = EiifRGBA8I;
4273 }
4274 else if (qualifierType == "r32i")
4275 {
4276 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4277 qualifier.imageInternalFormat = EiifR32I;
4278 }
4279 else if (qualifierType == "rgba32ui")
4280 {
4281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4282 qualifier.imageInternalFormat = EiifRGBA32UI;
4283 }
4284 else if (qualifierType == "rgba16ui")
4285 {
4286 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4287 qualifier.imageInternalFormat = EiifRGBA16UI;
4288 }
4289 else if (qualifierType == "rgba8ui")
4290 {
4291 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4292 qualifier.imageInternalFormat = EiifRGBA8UI;
4293 }
4294 else if (qualifierType == "r32ui")
4295 {
4296 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4297 qualifier.imageInternalFormat = EiifR32UI;
4298 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004299 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4300 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.primitiveType = EptPoints;
4304 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004305 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4306 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.primitiveType = EptLines;
4310 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004311 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4312 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004313 {
4314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4315 qualifier.primitiveType = EptLinesAdjacency;
4316 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004317 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4318 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004319 {
4320 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4321 qualifier.primitiveType = EptTriangles;
4322 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004323 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4324 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004325 {
4326 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4327 qualifier.primitiveType = EptTrianglesAdjacency;
4328 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004329 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4330 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004331 {
4332 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4333 qualifier.primitiveType = EptLineStrip;
4334 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004335 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4336 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004337 {
4338 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4339 qualifier.primitiveType = EptTriangleStrip;
4340 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004341
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004342 else
4343 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004344 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004345 }
4346
Jamie Madilla5efff92013-06-06 11:56:47 -04004347 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004348}
4349
Olli Etuahofbb1c792018-01-19 16:26:59 +02004350void TParseContext::parseLocalSize(const ImmutableString &qualifierType,
Martin Radev802abe02016-08-04 17:48:32 +03004351 const TSourceLoc &qualifierTypeLine,
4352 int intValue,
4353 const TSourceLoc &intValueLine,
4354 const std::string &intValueString,
4355 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004356 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004357{
Olli Etuaho856c4972016-08-08 11:38:39 +03004358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004359 if (intValue < 1)
4360 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004361 std::stringstream reasonStream;
4362 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4363 std::string reason = reasonStream.str();
4364 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004365 }
4366 (*localSize)[index] = intValue;
4367}
4368
Olli Etuaho09b04a22016-12-15 13:30:26 +00004369void TParseContext::parseNumViews(int intValue,
4370 const TSourceLoc &intValueLine,
4371 const std::string &intValueString,
4372 int *numViews)
4373{
4374 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4375 // specification.
4376 if (intValue < 1)
4377 {
4378 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4379 }
4380 *numViews = intValue;
4381}
4382
Shaob5cc1192017-07-06 10:47:20 +08004383void TParseContext::parseInvocations(int intValue,
4384 const TSourceLoc &intValueLine,
4385 const std::string &intValueString,
4386 int *numInvocations)
4387{
4388 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4389 // it doesn't make sense to accept invocations <= 0.
4390 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4391 {
4392 error(intValueLine,
4393 "out of range: invocations must be in the range of [1, "
4394 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4395 intValueString.c_str());
4396 }
4397 else
4398 {
4399 *numInvocations = intValue;
4400 }
4401}
4402
4403void TParseContext::parseMaxVertices(int intValue,
4404 const TSourceLoc &intValueLine,
4405 const std::string &intValueString,
4406 int *maxVertices)
4407{
4408 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4409 // it doesn't make sense to accept max_vertices < 0.
4410 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4411 {
4412 error(
4413 intValueLine,
4414 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4415 intValueString.c_str());
4416 }
4417 else
4418 {
4419 *maxVertices = intValue;
4420 }
4421}
4422
Olli Etuahofbb1c792018-01-19 16:26:59 +02004423TLayoutQualifier TParseContext::parseLayoutQualifier(const ImmutableString &qualifierType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004424 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004425 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304426 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004427{
Jamie Madill2f294c92017-11-20 14:47:26 -05004428 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004429
Martin Radev802abe02016-08-04 17:48:32 +03004430 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004431
Martin Radev802abe02016-08-04 17:48:32 +03004432 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004433 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004434 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004435 if (intValue < 0)
4436 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004437 error(intValueLine, "out of range: location must be non-negative",
4438 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004439 }
4440 else
4441 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004442 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004443 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004444 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004445 }
Olli Etuaho43364892017-02-13 16:00:12 +00004446 else if (qualifierType == "binding")
4447 {
4448 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4449 if (intValue < 0)
4450 {
4451 error(intValueLine, "out of range: binding must be non-negative",
4452 intValueString.c_str());
4453 }
4454 else
4455 {
4456 qualifier.binding = intValue;
4457 }
4458 }
jchen104cdac9e2017-05-08 11:01:20 +08004459 else if (qualifierType == "offset")
4460 {
4461 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4462 if (intValue < 0)
4463 {
4464 error(intValueLine, "out of range: offset must be non-negative",
4465 intValueString.c_str());
4466 }
4467 else
4468 {
4469 qualifier.offset = intValue;
4470 }
4471 }
Martin Radev802abe02016-08-04 17:48:32 +03004472 else if (qualifierType == "local_size_x")
4473 {
4474 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4475 &qualifier.localSize);
4476 }
4477 else if (qualifierType == "local_size_y")
4478 {
4479 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4480 &qualifier.localSize);
4481 }
4482 else if (qualifierType == "local_size_z")
4483 {
4484 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4485 &qualifier.localSize);
4486 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004487 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004488 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004489 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4490 {
4491 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4492 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004493 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004494 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4495 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004496 {
4497 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4498 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004499 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4500 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004501 {
4502 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4503 }
4504
Martin Radev802abe02016-08-04 17:48:32 +03004505 else
4506 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004507 error(qualifierTypeLine, "invalid layout qualifier", qualifierType);
Martin Radev802abe02016-08-04 17:48:32 +03004508 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004509
Jamie Madilla5efff92013-06-06 11:56:47 -04004510 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004511}
4512
Olli Etuaho613b9592016-09-05 12:05:53 +03004513TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4514{
4515 return new TTypeQualifierBuilder(
4516 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4517 mShaderVersion);
4518}
4519
Olli Etuahocce89652017-06-19 16:04:09 +03004520TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4521 const TSourceLoc &loc)
4522{
4523 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4524 return new TStorageQualifierWrapper(qualifier, loc);
4525}
4526
4527TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4528{
4529 if (getShaderType() == GL_VERTEX_SHADER)
4530 {
4531 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4532 }
4533 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4534}
4535
4536TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4537{
4538 if (declaringFunction())
4539 {
4540 return new TStorageQualifierWrapper(EvqIn, loc);
4541 }
Shaob5cc1192017-07-06 10:47:20 +08004542
4543 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004544 {
Shaob5cc1192017-07-06 10:47:20 +08004545 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004546 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004547 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004548 {
4549 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4550 }
4551 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004552 }
Shaob5cc1192017-07-06 10:47:20 +08004553 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004554 {
Shaob5cc1192017-07-06 10:47:20 +08004555 if (mShaderVersion < 300)
4556 {
4557 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4558 }
4559 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004560 }
Shaob5cc1192017-07-06 10:47:20 +08004561 case GL_COMPUTE_SHADER:
4562 {
4563 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4564 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004565 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004566 {
4567 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4568 }
4569 default:
4570 {
4571 UNREACHABLE();
4572 return new TStorageQualifierWrapper(EvqLast, loc);
4573 }
Olli Etuahocce89652017-06-19 16:04:09 +03004574 }
Olli Etuahocce89652017-06-19 16:04:09 +03004575}
4576
4577TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4578{
4579 if (declaringFunction())
4580 {
4581 return new TStorageQualifierWrapper(EvqOut, loc);
4582 }
Shaob5cc1192017-07-06 10:47:20 +08004583 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004584 {
Shaob5cc1192017-07-06 10:47:20 +08004585 case GL_VERTEX_SHADER:
4586 {
4587 if (mShaderVersion < 300)
4588 {
4589 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4590 }
4591 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4592 }
4593 case GL_FRAGMENT_SHADER:
4594 {
4595 if (mShaderVersion < 300)
4596 {
4597 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4598 }
4599 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4600 }
4601 case GL_COMPUTE_SHADER:
4602 {
4603 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4604 return new TStorageQualifierWrapper(EvqLast, loc);
4605 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004606 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004607 {
4608 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4609 }
4610 default:
4611 {
4612 UNREACHABLE();
4613 return new TStorageQualifierWrapper(EvqLast, loc);
4614 }
Olli Etuahocce89652017-06-19 16:04:09 +03004615 }
Olli Etuahocce89652017-06-19 16:04:09 +03004616}
4617
4618TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4619{
4620 if (!declaringFunction())
4621 {
4622 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4623 }
4624 return new TStorageQualifierWrapper(EvqInOut, loc);
4625}
4626
Jamie Madillb98c3a82015-07-23 14:26:04 -04004627TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004628 TLayoutQualifier rightQualifier,
4629 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004630{
Martin Radevc28888b2016-07-22 15:27:42 +03004631 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004632 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004633}
4634
Olli Etuahofbb1c792018-01-19 16:26:59 +02004635TDeclarator *TParseContext::parseStructDeclarator(const ImmutableString &identifier,
4636 const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004637{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004638 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004639 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004640}
4641
Olli Etuahofbb1c792018-01-19 16:26:59 +02004642TDeclarator *TParseContext::parseStructArrayDeclarator(const ImmutableString &identifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004643 const TSourceLoc &loc,
4644 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004645{
Olli Etuahofbb1c792018-01-19 16:26:59 +02004646 checkIsNotReserved(loc, identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004647 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004648}
4649
Olli Etuaho722bfb52017-10-26 17:00:11 +03004650void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4651 const TFieldList::const_iterator end,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004652 const ImmutableString &name,
Olli Etuaho722bfb52017-10-26 17:00:11 +03004653 const TSourceLoc &location)
4654{
4655 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4656 {
4657 if ((*fieldIter)->name() == name)
4658 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004659 error(location, "duplicate field name in structure", name);
Olli Etuaho722bfb52017-10-26 17:00:11 +03004660 }
4661 }
4662}
4663
4664TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4665{
4666 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4667 ++fieldIter)
4668 {
4669 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4670 location);
4671 }
4672 return fields;
4673}
4674
Olli Etuaho4de340a2016-12-16 09:32:03 +00004675TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4676 const TFieldList *newlyAddedFields,
4677 const TSourceLoc &location)
4678{
4679 for (TField *field : *newlyAddedFields)
4680 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004681 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4682 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004683 processedFields->push_back(field);
4684 }
4685 return processedFields;
4686}
4687
Martin Radev70866b82016-07-22 15:27:42 +03004688TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4689 const TTypeQualifierBuilder &typeQualifierBuilder,
4690 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004691 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004692{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004693 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004694
Martin Radev70866b82016-07-22 15:27:42 +03004695 typeSpecifier->qualifier = typeQualifier.qualifier;
4696 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004697 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004698 typeSpecifier->invariant = typeQualifier.invariant;
4699 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304700 {
Martin Radev70866b82016-07-22 15:27:42 +03004701 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004702 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004703 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004704}
4705
Jamie Madillb98c3a82015-07-23 14:26:04 -04004706TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004707 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004708{
Martin Radev4a9cd802016-09-01 16:51:51 +03004709 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4710 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004711
Olli Etuahofbb1c792018-01-19 16:26:59 +02004712 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004713 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004714
Martin Radev4a9cd802016-09-01 16:51:51 +03004715 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004716
Olli Etuahod5f44c92017-11-29 17:15:40 +02004717 TFieldList *fieldList = new TFieldList();
4718
4719 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304720 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004721 TType *type = new TType(typeSpecifier);
4722 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304723 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004724 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004725 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004726 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004727 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004728
Olli Etuahod5f44c92017-11-29 17:15:40 +02004729 TField *field = new TField(type, declarator->name(), declarator->line());
4730 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4731 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004732 }
4733
Olli Etuahod5f44c92017-11-29 17:15:40 +02004734 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004735}
4736
Martin Radev4a9cd802016-09-01 16:51:51 +03004737TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4738 const TSourceLoc &nameLine,
Olli Etuahofbb1c792018-01-19 16:26:59 +02004739 const ImmutableString &structName,
Martin Radev4a9cd802016-09-01 16:51:51 +03004740 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004741{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004742 SymbolType structSymbolType = SymbolType::UserDefined;
Olli Etuahofbb1c792018-01-19 16:26:59 +02004743 if (structName.empty())
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004744 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004745 structSymbolType = SymbolType::Empty;
4746 }
4747 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748
Jamie Madill9b820842015-02-12 10:40:10 -05004749 // Store a bool in the struct if we're at global scope, to allow us to
4750 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004751 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004752
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004753 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004755 checkIsNotReserved(nameLine, structName);
Olli Etuaho437664b2018-02-28 15:38:14 +02004756 if (!symbolTable.declare(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304757 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02004758 error(nameLine, "redefinition of a struct", structName);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004759 }
4760 }
4761
4762 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004763 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004764 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004765 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004766 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 switch (qualifier)
4768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004769 case EvqGlobal:
4770 case EvqTemporary:
4771 break;
4772 default:
4773 error(field.line(), "invalid qualifier on struct member",
4774 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004775 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004776 }
Martin Radev70866b82016-07-22 15:27:42 +03004777 if (field.type()->isInvariant())
4778 {
4779 error(field.line(), "invalid qualifier on struct member", "invariant");
4780 }
jchen104cdac9e2017-05-08 11:01:20 +08004781 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4782 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004783 {
4784 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4785 }
4786
Olli Etuahoebee5b32017-11-23 12:56:32 +02004787 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
Olli Etuahofbb1c792018-01-19 16:26:59 +02004788 field.name(), field.type());
Olli Etuahoebee5b32017-11-23 12:56:32 +02004789
Olli Etuaho43364892017-02-13 16:00:12 +00004790 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4791
4792 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004793
4794 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 }
4796
Martin Radev4a9cd802016-09-01 16:51:51 +03004797 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004798 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004799 exitStructDeclaration();
4800
Martin Radev4a9cd802016-09-01 16:51:51 +03004801 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802}
4803
Jamie Madillb98c3a82015-07-23 14:26:04 -04004804TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004805 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004806 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004807{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004808 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004809 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004810 init->isVector())
4811 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004812 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4813 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004814 return nullptr;
4815 }
4816
Olli Etuaho923ecef2017-10-11 12:01:38 +03004817 ASSERT(statementList);
Olli Etuahod05f9642018-03-05 12:13:26 +02004818 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004819 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004820 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004821 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004822 }
4823
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004824 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4825 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004826 return node;
4827}
4828
4829TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4830{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004831 if (mSwitchNestingLevel == 0)
4832 {
4833 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 return nullptr;
4835 }
4836 if (condition == nullptr)
4837 {
4838 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 return nullptr;
4840 }
4841 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004843 {
4844 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004845 }
4846 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004847 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4848 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4849 // fold in case labels.
4850 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 {
4852 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004853 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004854 TIntermCase *node = new TIntermCase(condition);
4855 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004856 return node;
4857}
4858
4859TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4860{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004861 if (mSwitchNestingLevel == 0)
4862 {
4863 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004866 TIntermCase *node = new TIntermCase(nullptr);
4867 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004868 return node;
4869}
4870
Jamie Madillb98c3a82015-07-23 14:26:04 -04004871TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4872 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004873 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004874{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004875 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004876
4877 switch (op)
4878 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004879 case EOpLogicalNot:
4880 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4881 child->isVector())
4882 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004883 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004884 return nullptr;
4885 }
4886 break;
4887 case EOpBitwiseNot:
4888 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4889 child->isMatrix() || child->isArray())
4890 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004891 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 return nullptr;
4893 }
4894 break;
4895 case EOpPostIncrement:
4896 case EOpPreIncrement:
4897 case EOpPostDecrement:
4898 case EOpPreDecrement:
4899 case EOpNegative:
4900 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004901 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4902 child->getBasicType() == EbtBool || child->isArray() ||
4903 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004906 return nullptr;
4907 }
Nico Weber41b072b2018-02-09 10:01:32 -05004908 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004909 // Operators for built-ins are already type checked against their prototype.
4910 default:
4911 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004912 }
4913
Jiajia Qinbc585152017-06-23 15:42:17 +08004914 if (child->getMemoryQualifier().writeonly)
4915 {
4916 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4917 return nullptr;
4918 }
4919
Olli Etuahof119a262016-08-19 15:54:22 +03004920 TIntermUnary *node = new TIntermUnary(op, child);
4921 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004922
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004923 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004924}
4925
Olli Etuaho09b22472015-02-11 11:47:26 +02004926TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4927{
Olli Etuahocce89652017-06-19 16:04:09 +03004928 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004929 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004930 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004931 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004932 return child;
4933 }
4934 return node;
4935}
4936
Jamie Madillb98c3a82015-07-23 14:26:04 -04004937TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4938 TIntermTyped *child,
4939 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004940{
Olli Etuaho856c4972016-08-08 11:38:39 +03004941 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004942 return addUnaryMath(op, child, loc);
4943}
4944
Olli Etuaho765924f2018-01-04 12:48:36 +02004945TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4946{
4947 // If we can, we should return the folded version of the expression for subsequent parsing. This
4948 // enables folding the containing expression during parsing as well, instead of the separate
4949 // FoldExpressions() step where folding nested expressions requires multiple full AST
4950 // traversals.
4951
4952 // Even if folding fails the fold() functions return some node representing the expression,
4953 // typically the original node. So "folded" can be assumed to be non-null.
4954 TIntermTyped *folded = expression->fold(mDiagnostics);
4955 ASSERT(folded != nullptr);
4956 if (folded->getQualifier() == expression->getQualifier())
4957 {
4958 // We need this expression to have the correct qualifier when validating the consuming
4959 // expression. So we can only return the folded node from here in case it has the same
4960 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4961 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4962 // 1. (true ? 1.0 : non_constant)
4963 // 2. (non_constant, 1.0)
4964 return folded;
4965 }
4966 return expression;
4967}
4968
Jamie Madillb98c3a82015-07-23 14:26:04 -04004969bool TParseContext::binaryOpCommonCheck(TOperator op,
4970 TIntermTyped *left,
4971 TIntermTyped *right,
4972 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004973{
jchen10b4cf5652017-05-05 18:51:17 +08004974 // Check opaque types are not allowed to be operands in expressions other than array indexing
4975 // and structure member selection.
4976 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4977 {
4978 switch (op)
4979 {
4980 case EOpIndexDirect:
4981 case EOpIndexIndirect:
4982 break;
jchen10b4cf5652017-05-05 18:51:17 +08004983
4984 default:
Nico Weberb5db2b42018-02-12 15:31:56 -05004985 ASSERT(op != EOpIndexDirectStruct);
jchen10b4cf5652017-05-05 18:51:17 +08004986 error(loc, "Invalid operation for variables with an opaque type",
4987 GetOperatorString(op));
4988 return false;
4989 }
4990 }
jchen10cc2a10e2017-05-03 14:05:12 +08004991
Jiajia Qinbc585152017-06-23 15:42:17 +08004992 if (right->getMemoryQualifier().writeonly)
4993 {
4994 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4995 return false;
4996 }
4997
4998 if (left->getMemoryQualifier().writeonly)
4999 {
5000 switch (op)
5001 {
5002 case EOpAssign:
5003 case EOpInitialize:
5004 case EOpIndexDirect:
5005 case EOpIndexIndirect:
5006 case EOpIndexDirectStruct:
5007 case EOpIndexDirectInterfaceBlock:
5008 break;
5009 default:
5010 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5011 return false;
5012 }
5013 }
5014
Olli Etuaho244be012016-08-18 15:26:02 +03005015 if (left->getType().getStruct() || right->getType().getStruct())
5016 {
5017 switch (op)
5018 {
5019 case EOpIndexDirectStruct:
5020 ASSERT(left->getType().getStruct());
5021 break;
5022 case EOpEqual:
5023 case EOpNotEqual:
5024 case EOpAssign:
5025 case EOpInitialize:
5026 if (left->getType() != right->getType())
5027 {
5028 return false;
5029 }
5030 break;
5031 default:
5032 error(loc, "Invalid operation for structs", GetOperatorString(op));
5033 return false;
5034 }
5035 }
5036
Olli Etuaho94050052017-05-08 14:17:44 +03005037 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5038 {
5039 switch (op)
5040 {
5041 case EOpIndexDirectInterfaceBlock:
5042 ASSERT(left->getType().getInterfaceBlock());
5043 break;
5044 default:
5045 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5046 return false;
5047 }
5048 }
5049
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005050 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005051 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005052 error(loc, "array / non-array mismatch", GetOperatorString(op));
5053 return false;
5054 }
5055
5056 if (left->isArray())
5057 {
5058 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005059 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005060 {
5061 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5062 return false;
5063 }
5064
Olli Etuahoe79904c2015-03-18 16:56:42 +02005065 switch (op)
5066 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005067 case EOpEqual:
5068 case EOpNotEqual:
5069 case EOpAssign:
5070 case EOpInitialize:
5071 break;
5072 default:
5073 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5074 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005075 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005076 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005077 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005078 {
5079 error(loc, "array size mismatch", GetOperatorString(op));
5080 return false;
5081 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005082 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005083
5084 // Check ops which require integer / ivec parameters
5085 bool isBitShift = false;
5086 switch (op)
5087 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005088 case EOpBitShiftLeft:
5089 case EOpBitShiftRight:
5090 case EOpBitShiftLeftAssign:
5091 case EOpBitShiftRightAssign:
5092 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5093 // check that the basic type is an integer type.
5094 isBitShift = true;
5095 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5096 {
5097 return false;
5098 }
5099 break;
5100 case EOpBitwiseAnd:
5101 case EOpBitwiseXor:
5102 case EOpBitwiseOr:
5103 case EOpBitwiseAndAssign:
5104 case EOpBitwiseXorAssign:
5105 case EOpBitwiseOrAssign:
5106 // It is enough to check the type of only one operand, since later it
5107 // is checked that the operand types match.
5108 if (!IsInteger(left->getBasicType()))
5109 {
5110 return false;
5111 }
5112 break;
5113 default:
5114 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005115 }
5116
5117 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5118 // So the basic type should usually match.
5119 if (!isBitShift && left->getBasicType() != right->getBasicType())
5120 {
5121 return false;
5122 }
5123
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005124 // Check that:
5125 // 1. Type sizes match exactly on ops that require that.
5126 // 2. Restrictions for structs that contain arrays or samplers are respected.
5127 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005128 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005129 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 case EOpAssign:
5131 case EOpInitialize:
5132 case EOpEqual:
5133 case EOpNotEqual:
5134 // ESSL 1.00 sections 5.7, 5.8, 5.9
5135 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5136 {
5137 error(loc, "undefined operation for structs containing arrays",
5138 GetOperatorString(op));
5139 return false;
5140 }
5141 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5142 // we interpret the spec so that this extends to structs containing samplers,
5143 // similarly to ESSL 1.00 spec.
5144 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5145 left->getType().isStructureContainingSamplers())
5146 {
5147 error(loc, "undefined operation for structs containing samplers",
5148 GetOperatorString(op));
5149 return false;
5150 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005151
Olli Etuahoe1805592017-01-02 16:41:20 +00005152 if ((left->getNominalSize() != right->getNominalSize()) ||
5153 (left->getSecondarySize() != right->getSecondarySize()))
5154 {
5155 error(loc, "dimension mismatch", GetOperatorString(op));
5156 return false;
5157 }
5158 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005159 case EOpLessThan:
5160 case EOpGreaterThan:
5161 case EOpLessThanEqual:
5162 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005163 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005164 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 return false;
5167 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005168 break;
5169 case EOpAdd:
5170 case EOpSub:
5171 case EOpDiv:
5172 case EOpIMod:
5173 case EOpBitShiftLeft:
5174 case EOpBitShiftRight:
5175 case EOpBitwiseAnd:
5176 case EOpBitwiseXor:
5177 case EOpBitwiseOr:
5178 case EOpAddAssign:
5179 case EOpSubAssign:
5180 case EOpDivAssign:
5181 case EOpIModAssign:
5182 case EOpBitShiftLeftAssign:
5183 case EOpBitShiftRightAssign:
5184 case EOpBitwiseAndAssign:
5185 case EOpBitwiseXorAssign:
5186 case EOpBitwiseOrAssign:
5187 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5188 {
5189 return false;
5190 }
5191
5192 // Are the sizes compatible?
5193 if (left->getNominalSize() != right->getNominalSize() ||
5194 left->getSecondarySize() != right->getSecondarySize())
5195 {
5196 // If the nominal sizes of operands do not match:
5197 // One of them must be a scalar.
5198 if (!left->isScalar() && !right->isScalar())
5199 return false;
5200
5201 // In the case of compound assignment other than multiply-assign,
5202 // the right side needs to be a scalar. Otherwise a vector/matrix
5203 // would be assigned to a scalar. A scalar can't be shifted by a
5204 // vector either.
5205 if (!right->isScalar() &&
5206 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5207 return false;
5208 }
5209 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005210 default:
5211 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005212 }
5213
Olli Etuahod6b14282015-03-17 14:31:35 +02005214 return true;
5215}
5216
Olli Etuaho1dded802016-08-18 18:13:13 +03005217bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5218 const TType &left,
5219 const TType &right)
5220{
5221 switch (op)
5222 {
5223 case EOpMul:
5224 case EOpMulAssign:
5225 return left.getNominalSize() == right.getNominalSize() &&
5226 left.getSecondarySize() == right.getSecondarySize();
5227 case EOpVectorTimesScalar:
5228 return true;
5229 case EOpVectorTimesScalarAssign:
5230 ASSERT(!left.isMatrix() && !right.isMatrix());
5231 return left.isVector() && !right.isVector();
5232 case EOpVectorTimesMatrix:
5233 return left.getNominalSize() == right.getRows();
5234 case EOpVectorTimesMatrixAssign:
5235 ASSERT(!left.isMatrix() && right.isMatrix());
5236 return left.isVector() && left.getNominalSize() == right.getRows() &&
5237 left.getNominalSize() == right.getCols();
5238 case EOpMatrixTimesVector:
5239 return left.getCols() == right.getNominalSize();
5240 case EOpMatrixTimesScalar:
5241 return true;
5242 case EOpMatrixTimesScalarAssign:
5243 ASSERT(left.isMatrix() && !right.isMatrix());
5244 return !right.isVector();
5245 case EOpMatrixTimesMatrix:
5246 return left.getCols() == right.getRows();
5247 case EOpMatrixTimesMatrixAssign:
5248 ASSERT(left.isMatrix() && right.isMatrix());
5249 // We need to check two things:
5250 // 1. The matrix multiplication step is valid.
5251 // 2. The result will have the same number of columns as the lvalue.
5252 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5253
5254 default:
5255 UNREACHABLE();
5256 return false;
5257 }
5258}
5259
Jamie Madillb98c3a82015-07-23 14:26:04 -04005260TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5261 TIntermTyped *left,
5262 TIntermTyped *right,
5263 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005264{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005265 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005266 return nullptr;
5267
Olli Etuahofc1806e2015-03-17 13:03:11 +02005268 switch (op)
5269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005270 case EOpEqual:
5271 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272 case EOpLessThan:
5273 case EOpGreaterThan:
5274 case EOpLessThanEqual:
5275 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005276 break;
5277 case EOpLogicalOr:
5278 case EOpLogicalXor:
5279 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005280 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5281 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005282 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 {
5284 return nullptr;
5285 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005286 // Basic types matching should have been already checked.
5287 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005288 break;
5289 case EOpAdd:
5290 case EOpSub:
5291 case EOpDiv:
5292 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005293 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5294 !right->getType().getStruct());
5295 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005296 {
5297 return nullptr;
5298 }
5299 break;
5300 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005301 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5302 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005303 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005304 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 {
5306 return nullptr;
5307 }
5308 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 default:
5310 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005311 }
5312
Olli Etuaho1dded802016-08-18 18:13:13 +03005313 if (op == EOpMul)
5314 {
5315 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5316 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5317 {
5318 return nullptr;
5319 }
5320 }
5321
Olli Etuaho3fdec912016-08-18 15:08:06 +03005322 TIntermBinary *node = new TIntermBinary(op, left, right);
5323 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005324 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005325}
5326
Jamie Madillb98c3a82015-07-23 14:26:04 -04005327TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5328 TIntermTyped *left,
5329 TIntermTyped *right,
5330 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005331{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005332 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005333 if (node == 0)
5334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5336 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005337 return left;
5338 }
5339 return node;
5340}
5341
Jamie Madillb98c3a82015-07-23 14:26:04 -04005342TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5343 TIntermTyped *left,
5344 TIntermTyped *right,
5345 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005346{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005347 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005348 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005349 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005350 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5351 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005352 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005353 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005354 }
5355 return node;
5356}
5357
Olli Etuaho13389b62016-10-16 11:48:18 +01005358TIntermBinary *TParseContext::createAssign(TOperator op,
5359 TIntermTyped *left,
5360 TIntermTyped *right,
5361 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005362{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005363 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005364 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005365 if (op == EOpMulAssign)
5366 {
5367 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5368 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5369 {
5370 return nullptr;
5371 }
5372 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005373 TIntermBinary *node = new TIntermBinary(op, left, right);
5374 node->setLine(loc);
5375
Olli Etuaho3fdec912016-08-18 15:08:06 +03005376 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005377 }
5378 return nullptr;
5379}
5380
Jamie Madillb98c3a82015-07-23 14:26:04 -04005381TIntermTyped *TParseContext::addAssign(TOperator op,
5382 TIntermTyped *left,
5383 TIntermTyped *right,
5384 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005385{
Olli Etuahocce89652017-06-19 16:04:09 +03005386 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005387 TIntermTyped *node = createAssign(op, left, right, loc);
5388 if (node == nullptr)
5389 {
5390 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 return left;
5392 }
5393 return node;
5394}
5395
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005396TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5397 TIntermTyped *right,
5398 const TSourceLoc &loc)
5399{
Corentin Wallez0d959252016-07-12 17:26:32 -04005400 // WebGL2 section 5.26, the following results in an error:
5401 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005402 if (mShaderSpec == SH_WEBGL2_SPEC &&
5403 (left->isArray() || left->getBasicType() == EbtVoid ||
5404 left->getType().isStructureContainingArrays() || right->isArray() ||
5405 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005406 {
5407 error(loc,
5408 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5409 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005410 }
5411
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005412 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005413
5414 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005415}
5416
Olli Etuaho49300862015-02-20 14:54:49 +02005417TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5418{
5419 switch (op)
5420 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005421 case EOpContinue:
5422 if (mLoopNestingLevel <= 0)
5423 {
5424 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005425 }
5426 break;
5427 case EOpBreak:
5428 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5429 {
5430 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005431 }
5432 break;
5433 case EOpReturn:
5434 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5435 {
5436 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005437 }
5438 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005439 case EOpKill:
5440 if (mShaderType != GL_FRAGMENT_SHADER)
5441 {
5442 error(loc, "discard supported in fragment shaders only", "discard");
5443 }
5444 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005446 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005447 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005448 }
Olli Etuahocce89652017-06-19 16:04:09 +03005449 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005450}
5451
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005453 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005455{
Olli Etuahocce89652017-06-19 16:04:09 +03005456 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005457 {
Olli Etuahocce89652017-06-19 16:04:09 +03005458 ASSERT(op == EOpReturn);
5459 mFunctionReturnsValue = true;
5460 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5461 {
5462 error(loc, "void function cannot return a value", "return");
5463 }
5464 else if (*mCurrentFunctionType != expression->getType())
5465 {
5466 error(loc, "function return is not matching type:", "return");
5467 }
Olli Etuaho49300862015-02-20 14:54:49 +02005468 }
Olli Etuahocce89652017-06-19 16:04:09 +03005469 TIntermBranch *node = new TIntermBranch(op, expression);
5470 node->setLine(loc);
5471 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005472}
5473
Martin Radev84aa2dc2017-09-11 15:51:02 +03005474void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5475{
5476 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005477 const TFunction *func = functionCall->getFunction();
5478 if (BuiltInGroup::isTextureGather(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005479 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005480 bool isTextureGatherOffset = BuiltInGroup::isTextureGatherOffset(func);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005481 TIntermNode *componentNode = nullptr;
5482 TIntermSequence *arguments = functionCall->getSequence();
5483 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5484 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5485 ASSERT(sampler != nullptr);
5486 switch (sampler->getBasicType())
5487 {
5488 case EbtSampler2D:
5489 case EbtISampler2D:
5490 case EbtUSampler2D:
5491 case EbtSampler2DArray:
5492 case EbtISampler2DArray:
5493 case EbtUSampler2DArray:
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005494 if ((!isTextureGatherOffset && arguments->size() == 3u) ||
Martin Radev84aa2dc2017-09-11 15:51:02 +03005495 (isTextureGatherOffset && arguments->size() == 4u))
5496 {
5497 componentNode = arguments->back();
5498 }
5499 break;
5500 case EbtSamplerCube:
5501 case EbtISamplerCube:
5502 case EbtUSamplerCube:
5503 ASSERT(!isTextureGatherOffset);
5504 if (arguments->size() == 3u)
5505 {
5506 componentNode = arguments->back();
5507 }
5508 break;
5509 case EbtSampler2DShadow:
5510 case EbtSampler2DArrayShadow:
5511 case EbtSamplerCubeShadow:
5512 break;
5513 default:
5514 UNREACHABLE();
5515 break;
5516 }
5517 if (componentNode)
5518 {
5519 const TIntermConstantUnion *componentConstantUnion =
5520 componentNode->getAsConstantUnion();
5521 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5522 {
5523 error(functionCall->getLine(), "Texture component must be a constant expression",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005524 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005525 }
5526 else
5527 {
5528 int component = componentConstantUnion->getIConst(0);
5529 if (component < 0 || component > 3)
5530 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005531 error(functionCall->getLine(), "Component must be in the range [0;3]",
5532 func->name());
Martin Radev84aa2dc2017-09-11 15:51:02 +03005533 }
5534 }
5535 }
5536 }
5537}
5538
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005539void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5540{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005541 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005542 const TFunction *func = functionCall->getFunction();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005543 TIntermNode *offset = nullptr;
5544 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005545 bool useTextureGatherOffsetConstraints = false;
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005546 if (BuiltInGroup::isTextureOffsetNoBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005547 {
5548 offset = arguments->back();
5549 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005550 else if (BuiltInGroup::isTextureOffsetBias(func))
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005551 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005552 // A bias parameter follows the offset parameter.
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005553 ASSERT(arguments->size() >= 3);
5554 offset = (*arguments)[2];
5555 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005556 else if (BuiltInGroup::isTextureGatherOffset(func))
Martin Radev84aa2dc2017-09-11 15:51:02 +03005557 {
5558 ASSERT(arguments->size() >= 3u);
5559 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5560 ASSERT(sampler != nullptr);
5561 switch (sampler->getBasicType())
5562 {
5563 case EbtSampler2D:
5564 case EbtISampler2D:
5565 case EbtUSampler2D:
5566 case EbtSampler2DArray:
5567 case EbtISampler2DArray:
5568 case EbtUSampler2DArray:
5569 offset = (*arguments)[2];
5570 break;
5571 case EbtSampler2DShadow:
5572 case EbtSampler2DArrayShadow:
5573 offset = (*arguments)[3];
5574 break;
5575 default:
5576 UNREACHABLE();
5577 break;
5578 }
5579 useTextureGatherOffsetConstraints = true;
5580 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005581 if (offset != nullptr)
5582 {
5583 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5584 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5585 {
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005586 error(functionCall->getLine(), "Texture offset must be a constant expression",
5587 func->name());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005588 }
5589 else
5590 {
5591 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5592 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005593 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005594 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5595 : mMinProgramTexelOffset;
5596 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5597 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005598 for (size_t i = 0u; i < size; ++i)
5599 {
5600 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005601 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005602 {
5603 std::stringstream tokenStream;
5604 tokenStream << offsetValue;
5605 std::string token = tokenStream.str();
5606 error(offset->getLine(), "Texture offset value out of valid range",
5607 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 }
5609 }
5610 }
5611 }
5612}
5613
Jiajia Qina3106c52017-11-03 09:39:39 +08005614void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5615{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005616 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005617 const TFunction *func = functionCall->getFunction();
5618 if (BuiltInGroup::isAtomicMemory(func))
Jiajia Qina3106c52017-11-03 09:39:39 +08005619 {
5620 TIntermSequence *arguments = functionCall->getSequence();
5621 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5622
5623 if (IsBufferOrSharedVariable(memNode))
5624 {
5625 return;
5626 }
5627
5628 while (memNode->getAsBinaryNode())
5629 {
5630 memNode = memNode->getAsBinaryNode()->getLeft();
5631 if (IsBufferOrSharedVariable(memNode))
5632 {
5633 return;
5634 }
5635 }
5636
5637 error(memNode->getLine(),
5638 "The value passed to the mem argument of an atomic memory function does not "
5639 "correspond to a buffer or shared variable.",
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005640 func->name());
Jiajia Qina3106c52017-11-03 09:39:39 +08005641 }
5642}
5643
Martin Radev2cc85b32016-08-05 16:22:53 +03005644// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5645void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5646{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005647 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005648
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005649 const TFunction *func = functionCall->getFunction();
5650
5651 if (BuiltInGroup::isImage(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005652 {
5653 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005654 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005655
Olli Etuaho485eefd2017-02-14 17:40:06 +00005656 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005657
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005658 if (BuiltInGroup::isImageStore(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005659 {
5660 if (memoryQualifier.readonly)
5661 {
5662 error(imageNode->getLine(),
5663 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005664 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005665 }
5666 }
Olli Etuaho2bfe9f62018-03-02 16:53:29 +02005667 else if (BuiltInGroup::isImageLoad(func))
Martin Radev2cc85b32016-08-05 16:22:53 +03005668 {
5669 if (memoryQualifier.writeonly)
5670 {
5671 error(imageNode->getLine(),
5672 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005673 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005674 }
5675 }
5676 }
5677}
5678
5679// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5680void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5681 const TFunction *functionDefinition,
5682 const TIntermAggregate *functionCall)
5683{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005684 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005685
5686 const TIntermSequence &arguments = *functionCall->getSequence();
5687
5688 ASSERT(functionDefinition->getParamCount() == arguments.size());
5689
5690 for (size_t i = 0; i < arguments.size(); ++i)
5691 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005692 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5693 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005694 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5695 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5696
5697 if (IsImage(functionArgumentType.getBasicType()))
5698 {
5699 const TMemoryQualifier &functionArgumentMemoryQualifier =
5700 functionArgumentType.getMemoryQualifier();
5701 const TMemoryQualifier &functionParameterMemoryQualifier =
5702 functionParameterType.getMemoryQualifier();
5703 if (functionArgumentMemoryQualifier.readonly &&
5704 !functionParameterMemoryQualifier.readonly)
5705 {
5706 error(functionCall->getLine(),
5707 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005708 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005709 }
5710
5711 if (functionArgumentMemoryQualifier.writeonly &&
5712 !functionParameterMemoryQualifier.writeonly)
5713 {
5714 error(functionCall->getLine(),
5715 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005716 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005717 }
Martin Radev049edfa2016-11-11 14:35:37 +02005718
5719 if (functionArgumentMemoryQualifier.coherent &&
5720 !functionParameterMemoryQualifier.coherent)
5721 {
5722 error(functionCall->getLine(),
5723 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005724 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005725 }
5726
5727 if (functionArgumentMemoryQualifier.volatileQualifier &&
5728 !functionParameterMemoryQualifier.volatileQualifier)
5729 {
5730 error(functionCall->getLine(),
5731 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005732 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005733 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005734 }
5735 }
5736}
5737
Olli Etuaho95ed1942018-02-01 14:01:19 +02005738TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005739{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005740 if (fnCall->thisNode() != nullptr)
5741 {
5742 return addMethod(fnCall, loc);
5743 }
5744 if (fnCall->isConstructor())
5745 {
5746 return addConstructor(fnCall, loc);
5747 }
5748 return addNonConstructorFunctionCall(fnCall, loc);
Olli Etuaho72d10202017-01-19 15:58:30 +00005749}
5750
Olli Etuaho95ed1942018-02-01 14:01:19 +02005751TIntermTyped *TParseContext::addMethod(TFunctionLookup *fnCall, const TSourceLoc &loc)
Olli Etuaho72d10202017-01-19 15:58:30 +00005752{
Olli Etuaho95ed1942018-02-01 14:01:19 +02005753 TIntermTyped *thisNode = fnCall->thisNode();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005754 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5755 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5756 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005757 // So accessing fnCall->name() below is safe.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005758 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005759 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005760 error(loc, "invalid method", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005762 else if (!fnCall->arguments().empty())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005763 {
5764 error(loc, "method takes no parameters", "length");
5765 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005766 else if (!thisNode->isArray())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767 {
5768 error(loc, "length can only be called on arrays", "length");
5769 }
Olli Etuaho95ed1942018-02-01 14:01:19 +02005770 else if (thisNode->getQualifier() == EvqPerVertexIn &&
Jiawei Shaod8105a02017-08-08 09:54:36 +08005771 mGeometryShaderInputPrimitiveType == EptUndefined)
5772 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005773 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005774 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5775 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 else
5777 {
Olli Etuaho95ed1942018-02-01 14:01:19 +02005778 TIntermUnary *node = new TIntermUnary(EOpArrayLength, thisNode);
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005779 node->setLine(loc);
5780 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005781 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005782 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783}
5784
Olli Etuaho95ed1942018-02-01 14:01:19 +02005785TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunctionLookup *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005786 const TSourceLoc &loc)
5787{
Olli Etuaho697bf652018-02-16 11:50:54 +02005788 // First check whether the function has been hidden by a variable name or struct typename by
5789 // using the symbol looked up in the lexical phase. If the function is not hidden, look for one
5790 // with a matching argument list.
5791 if (fnCall->symbol() != nullptr && !fnCall->symbol()->isFunction())
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005792 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005793 error(loc, "function name expected", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005794 }
5795 else
5796 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005797 // There are no inner functions, so it's enough to look for user-defined functions in the
5798 // global scope.
Olli Etuaho697bf652018-02-16 11:50:54 +02005799 const TSymbol *symbol = symbolTable.findGlobal(fnCall->getMangledName());
Olli Etuahoe80825e2018-02-16 10:24:53 +02005800 if (symbol != nullptr)
5801 {
5802 // A user-defined function - could be an overloaded built-in as well.
5803 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
5804 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
5805 TIntermAggregate *callNode =
5806 TIntermAggregate::CreateFunctionCall(*fnCandidate, &fnCall->arguments());
5807 callNode->setLine(loc);
5808 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
5809 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5810 return callNode;
5811 }
5812
5813 symbol = symbolTable.findBuiltIn(fnCall->getMangledName(), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005815 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02005816 error(loc, "no matching overloaded function found", fnCall->name());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817 }
5818 else
5819 {
Olli Etuahoe80825e2018-02-16 10:24:53 +02005820 // A built-in function.
5821 ASSERT(symbol->symbolType() == SymbolType::BuiltIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoe80825e2018-02-16 10:24:53 +02005823
Olli Etuaho37b697e2018-01-29 12:19:27 +02005824 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005825 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005826 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005827 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005828 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoe80825e2018-02-16 10:24:53 +02005829 if (op != EOpCallBuiltInFunction)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005830 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005831 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005832 if (fnCandidate->getParamCount() == 1)
5833 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 // Treat it like a built-in unary operator.
Olli Etuaho95ed1942018-02-01 14:01:19 +02005835 TIntermNode *unaryParamNode = fnCall->arguments().front();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005836 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005837 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005838 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005839 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005840 TIntermAggregate *callNode =
5841 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005842 callNode->setLine(loc);
5843
Olli Etuahoe80825e2018-02-16 10:24:53 +02005844 // Some built-in functions have out parameters too.
5845 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5846
5847 // See if we can constant fold a built-in. Note that this may be possible
5848 // even if it is not const-qualified.
5849 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 }
Olli Etuahoe80825e2018-02-16 10:24:53 +02005851
5852 // This is a built-in function with no op associated with it.
5853 TIntermAggregate *callNode =
5854 TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, &fnCall->arguments());
5855 callNode->setLine(loc);
5856 checkTextureOffsetConst(callNode);
5857 checkTextureGather(callNode);
5858 checkImageMemoryAccessForBuiltinFunctions(callNode);
5859 checkAtomicMemoryBuiltinFunctions(callNode);
5860 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
5861 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005862 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005863 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864
5865 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005866 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005867}
5868
Jamie Madillb98c3a82015-07-23 14:26:04 -04005869TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005870 TIntermTyped *trueExpression,
5871 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005872 const TSourceLoc &loc)
5873{
Olli Etuaho56229f12017-07-10 14:16:33 +03005874 if (!checkIsScalarBool(loc, cond))
5875 {
5876 return falseExpression;
5877 }
Olli Etuaho52901742015-04-15 13:42:45 +03005878
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005879 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005880 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005881 std::stringstream reasonStream;
5882 reasonStream << "mismatching ternary operator operand types '"
5883 << trueExpression->getCompleteString() << " and '"
5884 << falseExpression->getCompleteString() << "'";
5885 std::string reason = reasonStream.str();
5886 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005887 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005888 }
Olli Etuahode318b22016-10-25 16:18:25 +01005889 if (IsOpaqueType(trueExpression->getBasicType()))
5890 {
5891 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005892 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005893 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5894 // Note that structs containing opaque types don't need to be checked as structs are
5895 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005896 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005897 return falseExpression;
5898 }
5899
Jiajia Qinbc585152017-06-23 15:42:17 +08005900 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5901 falseExpression->getMemoryQualifier().writeonly)
5902 {
5903 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5904 return falseExpression;
5905 }
5906
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005907 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005908 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005909 // ESSL 3.00.6 section 5.7:
5910 // Ternary operator support is optional for arrays. No certainty that it works across all
5911 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5912 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005913 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005914 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005915 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005916 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005917 }
Olli Etuaho94050052017-05-08 14:17:44 +03005918 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5919 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005920 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005921 return falseExpression;
5922 }
5923
Corentin Wallez0d959252016-07-12 17:26:32 -04005924 // WebGL2 section 5.26, the following results in an error:
5925 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005926 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005927 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005928 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005929 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005930 }
5931
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005932 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5933 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005934 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005935}
Olli Etuaho49300862015-02-20 14:54:49 +02005936
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005937//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005938// Parse an array of strings using yyparse.
5939//
5940// Returns 0 for success.
5941//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005942int PaParseStrings(size_t count,
5943 const char *const string[],
5944 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305945 TParseContext *context)
5946{
Yunchao He4f285442017-04-21 12:15:49 +08005947 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005948 return 1;
5949
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005950 if (glslang_initialize(context))
5951 return 1;
5952
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005953 int error = glslang_scan(count, string, length, context);
5954 if (!error)
5955 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005956
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005957 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005958
alokp@chromium.org6b495712012-06-29 00:06:58 +00005959 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005960}
Jamie Madill45bcc782016-11-07 13:58:48 -05005961
5962} // namespace sh