blob: 4264f907afe74cd968a59a43f25aa88971ecc023 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Jiajia Qina3106c52017-11-03 09:39:39 +080036const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
37 "atomicAnd", "atomicOr", "atomicXor",
38 "atomicExchange", "atomicCompSwap"}};
39
40bool IsAtomicBuiltin(const TString &name)
41{
42 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
43 {
44 if (name.compare(kAtomicBuiltin[i]) == 0)
45 {
46 return true;
47 }
48 }
49 return false;
50}
51
Olli Etuaho0f684632017-07-13 12:42:15 +030052bool ContainsSampler(const TStructure *structType);
53
Martin Radev2cc85b32016-08-05 16:22:53 +030054bool ContainsSampler(const TType &type)
55{
56 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030057 {
Martin Radev2cc85b32016-08-05 16:22:53 +030058 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030059 }
jchen10cc2a10e2017-05-03 14:05:12 +080060 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030061 {
Olli Etuaho0f684632017-07-13 12:42:15 +030062 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030063 }
64
65 return false;
66}
67
Olli Etuaho0f684632017-07-13 12:42:15 +030068bool ContainsSampler(const TStructure *structType)
69{
70 for (const auto &field : structType->fields())
71 {
72 if (ContainsSampler(*field->type()))
73 return true;
74 }
75 return false;
76}
77
Olli Etuaho485eefd2017-02-14 17:40:06 +000078// Get a token from an image argument to use as an error message token.
79const char *GetImageArgumentToken(TIntermTyped *imageNode)
80{
81 ASSERT(IsImage(imageNode->getBasicType()));
82 while (imageNode->getAsBinaryNode() &&
83 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
84 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
85 {
86 imageNode = imageNode->getAsBinaryNode()->getLeft();
87 }
88 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
89 if (imageSymbol)
90 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020091 return imageSymbol->getName().c_str();
Olli Etuaho485eefd2017-02-14 17:40:06 +000092 }
93 return "image";
94}
95
Olli Etuahocce89652017-06-19 16:04:09 +030096bool CanSetDefaultPrecisionOnType(const TPublicType &type)
97{
98 if (!SupportsPrecision(type.getBasicType()))
99 {
100 return false;
101 }
102 if (type.getBasicType() == EbtUInt)
103 {
104 // ESSL 3.00.4 section 4.5.4
105 return false;
106 }
107 if (type.isAggregate())
108 {
109 // Not allowed to set for aggregate types
110 return false;
111 }
112 return true;
113}
114
Jiawei Shaod8105a02017-08-08 09:54:36 +0800115// Map input primitive types to input array sizes in a geometry shader.
116GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
117{
118 switch (primitiveType)
119 {
120 case EptPoints:
121 return 1u;
122 case EptLines:
123 return 2u;
124 case EptTriangles:
125 return 3u;
126 case EptLinesAdjacency:
127 return 4u;
128 case EptTrianglesAdjacency:
129 return 6u;
130 default:
131 UNREACHABLE();
132 return 0u;
133 }
134}
135
Jiajia Qina3106c52017-11-03 09:39:39 +0800136bool IsBufferOrSharedVariable(TIntermTyped *var)
137{
138 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
139 var->getQualifier() == EvqShared)
140 {
141 return true;
142 }
143 return false;
144}
145
Martin Radev2cc85b32016-08-05 16:22:53 +0300146} // namespace
147
jchen104cdac9e2017-05-08 11:01:20 +0800148// This tracks each binding point's current default offset for inheritance of subsequent
149// variables using the same binding, and keeps offsets unique and non overlapping.
150// See GLSL ES 3.1, section 4.4.6.
151class TParseContext::AtomicCounterBindingState
152{
153 public:
154 AtomicCounterBindingState() : mDefaultOffset(0) {}
155 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
156 // newly inserted span.
157 int insertSpan(int start, size_t length)
158 {
159 gl::RangeI newSpan(start, start + static_cast<int>(length));
160 for (const auto &span : mSpans)
161 {
162 if (newSpan.intersects(span))
163 {
164 return -1;
165 }
166 }
167 mSpans.push_back(newSpan);
168 mDefaultOffset = newSpan.high();
169 return start;
170 }
171 // Inserts a new span starting from the default offset.
172 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
173 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
174
175 private:
176 int mDefaultOffset;
177 std::vector<gl::RangeI> mSpans;
178};
179
Jamie Madillacb4b812016-11-07 13:50:29 -0500180TParseContext::TParseContext(TSymbolTable &symt,
181 TExtensionBehavior &ext,
182 sh::GLenum type,
183 ShShaderSpec spec,
184 ShCompileOptions options,
185 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000186 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500187 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300188 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300189 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500190 mShaderType(type),
191 mShaderSpec(spec),
192 mCompileOptions(options),
193 mShaderVersion(100),
194 mTreeRoot(nullptr),
195 mLoopNestingLevel(0),
196 mStructNestingLevel(0),
197 mSwitchNestingLevel(0),
198 mCurrentFunctionType(nullptr),
199 mFunctionReturnsValue(false),
200 mChecksPrecisionErrors(checksPrecErrors),
201 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mDefaultUniformMatrixPacking(EmpColumnMajor),
203 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
204 mDefaultBufferMatrixPacking(EmpColumnMajor),
205 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000206 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000208 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500209 mShaderVersion,
210 mShaderType,
211 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000212 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 mScanner(nullptr),
214 mUsesFragData(false),
215 mUsesFragColor(false),
216 mUsesSecondaryOutputs(false),
217 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
218 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300219 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
220 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500221 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500222 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000223 mNumViews(-1),
224 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000225 mMaxImageUnits(resources.MaxImageUnits),
226 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000227 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800228 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800229 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800230 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800231 mDeclaringFunction(false),
232 mGeometryShaderInputPrimitiveType(EptUndefined),
233 mGeometryShaderOutputPrimitiveType(EptUndefined),
234 mGeometryShaderInvocations(0),
235 mGeometryShaderMaxVertices(-1),
236 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800237 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Olli Etuahoc74ec1a2018-01-09 15:23:28 +0200238 mGlInVariableWithArraySize(nullptr)
Jamie Madillacb4b812016-11-07 13:50:29 -0500239{
Jamie Madillacb4b812016-11-07 13:50:29 -0500240}
241
jchen104cdac9e2017-05-08 11:01:20 +0800242TParseContext::~TParseContext()
243{
244}
245
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246bool TParseContext::parseVectorFields(const TSourceLoc &line,
247 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 ASSERT(fieldOffsets);
252 size_t fieldCount = compString.size();
253 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000255 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 return false;
257 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 enum
261 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 exyzw,
263 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000264 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
269 switch (compString[i])
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = exyzw;
274 break;
275 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = ergba;
278 break;
279 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = estpq;
282 break;
283 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = exyzw;
286 break;
287 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = ergba;
290 break;
291 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300292 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 fieldSet[i] = estpq;
294 break;
295 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300296 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 fieldSet[i] = exyzw;
298 break;
299 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 fieldSet[i] = ergba;
302 break;
303 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 fieldSet[i] = estpq;
306 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 fieldSet[i] = exyzw;
311 break;
312 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 fieldSet[i] = ergba;
315 break;
316 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300317 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400318 fieldSet[i] = estpq;
319 break;
320 default:
321 error(line, "illegal vector field selection", compString.c_str());
322 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 }
324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300326 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400330 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return false;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334 if (i > 0)
335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400336 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400338 error(line, "illegal - vector component fields not from the same set",
339 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return false;
341 }
342 }
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346}
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348///////////////////////////////////////////////////////////////////////
349//
350// Errors
351//
352////////////////////////////////////////////////////////////////////////
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Used by flex/bison to output all syntax and parsing errors.
356//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000357void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000359 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000364 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000365}
366
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367void TParseContext::outOfRangeError(bool isError,
368 const TSourceLoc &loc,
369 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200371{
372 if (isError)
373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000374 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200375 }
376 else
377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000378 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200379 }
380}
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382//
383// Same error message for all places assignments don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
389 std::string reason = reasonStream.str();
390 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391}
392
393//
394// Same error message for all places unary operations don't work.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 std::stringstream reasonStream;
399 reasonStream << "wrong operand type - no operation '" << op
400 << "' exists that takes an operand of type " << operand
401 << " (or there is no acceptable conversion)";
402 std::string reason = reasonStream.str();
403 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Same error message for all binary operations don't work.
408//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400409void TParseContext::binaryOpError(const TSourceLoc &line,
410 const char *op,
411 TString left,
412 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000414 std::stringstream reasonStream;
415 reasonStream << "wrong operand types - no operation '" << op
416 << "' exists that takes a left-hand operand of type '" << left
417 << "' and a right operand of type '" << right
418 << "' (or there is no acceptable conversion)";
419 std::string reason = reasonStream.str();
420 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Olli Etuaho856c4972016-08-08 11:38:39 +0300423void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
424 TPrecision precision,
425 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400427 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Martin Radev70866b82016-07-22 15:27:42 +0300429
430 if (precision != EbpUndefined && !SupportsPrecision(type))
431 {
432 error(line, "illegal type for precision qualifier", getBasicString(type));
433 }
434
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 switch (type)
438 {
439 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400440 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 case EbtInt:
443 case EbtUInt:
444 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300446 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200447 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800448 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200449 {
jchen10cc2a10e2017-05-03 14:05:12 +0800450 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300451 return;
452 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test and if necessary, spit out an error, to see if the node is really
458// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300459bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TIntermSymbol *symNode = node->getAsSymbolNode();
462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100463 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
464
465 if (swizzleNode)
466 {
467 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
468 if (ok && swizzleNode->hasDuplicateOffsets())
469 {
470 error(line, " l-value of swizzle cannot have duplicate components", op);
471 return false;
472 }
473 return ok;
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (binaryNode)
477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 case EOpIndexDirect:
481 case EOpIndexIndirect:
482 case EOpIndexDirectStruct:
483 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300484 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
jchen10cc2a10e2017-05-03 14:05:12 +0800492 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 switch (node->getQualifier())
494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EvqConst:
496 message = "can't modify a const";
497 break;
498 case EvqConstReadOnly:
499 message = "can't modify a const";
500 break;
501 case EvqAttribute:
502 message = "can't modify an attribute";
503 break;
504 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800506 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800507 case EvqFlatIn:
508 case EvqSmoothIn:
509 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 message = "can't modify an input";
511 break;
512 case EvqUniform:
513 message = "can't modify a uniform";
514 break;
515 case EvqVaryingIn:
516 message = "can't modify a varying";
517 break;
518 case EvqFragCoord:
519 message = "can't modify gl_FragCoord";
520 break;
521 case EvqFrontFacing:
522 message = "can't modify gl_FrontFacing";
523 break;
524 case EvqPointCoord:
525 message = "can't modify gl_PointCoord";
526 break;
Martin Radevb0883602016-08-04 17:48:58 +0300527 case EvqNumWorkGroups:
528 message = "can't modify gl_NumWorkGroups";
529 break;
530 case EvqWorkGroupSize:
531 message = "can't modify gl_WorkGroupSize";
532 break;
533 case EvqWorkGroupID:
534 message = "can't modify gl_WorkGroupID";
535 break;
536 case EvqLocalInvocationID:
537 message = "can't modify gl_LocalInvocationID";
538 break;
539 case EvqGlobalInvocationID:
540 message = "can't modify gl_GlobalInvocationID";
541 break;
542 case EvqLocalInvocationIndex:
543 message = "can't modify gl_LocalInvocationIndex";
544 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300545 case EvqViewIDOVR:
546 message = "can't modify gl_ViewID_OVR";
547 break;
Martin Radev802abe02016-08-04 17:48:32 +0300548 case EvqComputeIn:
549 message = "can't modify work group size variable";
550 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800551 case EvqPerVertexIn:
552 message = "can't modify any member in gl_in";
553 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800554 case EvqPrimitiveIDIn:
555 message = "can't modify gl_PrimitiveIDIn";
556 break;
557 case EvqInvocationID:
558 message = "can't modify gl_InvocationID";
559 break;
560 case EvqPrimitiveID:
561 if (mShaderType == GL_FRAGMENT_SHADER)
562 {
563 message = "can't modify gl_PrimitiveID in a fragment shader";
564 }
565 break;
566 case EvqLayer:
567 if (mShaderType == GL_FRAGMENT_SHADER)
568 {
569 message = "can't modify gl_Layer in a fragment shader";
570 }
571 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 default:
573 //
574 // Type that can't be written to?
575 //
576 if (node->getBasicType() == EbtVoid)
577 {
578 message = "can't modify void";
579 }
jchen10cc2a10e2017-05-03 14:05:12 +0800580 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 {
jchen10cc2a10e2017-05-03 14:05:12 +0800582 message = "can't modify a variable with type ";
583 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300584 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800585 else if (node->getMemoryQualifier().readonly)
586 {
587 message = "can't modify a readonly variable";
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
jchen10cc2a10e2017-05-03 14:05:12 +0800591 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000593 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Everything else is okay, no error.
600 //
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 //
605 // If we get here, we have an error and a message.
606 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (symNode)
608 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200609 // Symbol inside an expression can't be nameless.
610 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
611
612 const char *symbol = symNode->getName().c_str();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000613 std::stringstream reasonStream;
614 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
615 std::string reason = reasonStream.str();
616 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 else
619 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000620 std::stringstream reasonStream;
621 reasonStream << "l-value required (" << message << ")";
622 std::string reason = reasonStream.str();
623 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629// Both test, and if necessary spit out an error, to see if the node is really
630// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300631void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632{
Olli Etuaho383b7912016-08-05 11:22:59 +0300633 if (node->getQualifier() != EvqConst)
634 {
635 error(node->getLine(), "constant expression required", "");
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639// Both test, and if necessary spit out an error, to see if the node is really
640// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300641void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Olli Etuaho383b7912016-08-05 11:22:59 +0300643 if (!node->isScalarInt())
644 {
645 error(node->getLine(), "integer expression required", token);
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649// Both test, and if necessary spit out an error, to see if we are currently
650// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800651bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho856c4972016-08-08 11:38:39 +0300653 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 {
655 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800656 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300657 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300661// ESSL 3.00.5 sections 3.8 and 3.9.
662// If it starts "gl_" or contains two consecutive underscores, it's reserved.
663// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300667 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300669 error(line, reservedErrMsg, "gl_");
670 return false;
671 }
672 if (sh::IsWebGLBasedSpec(mShaderSpec))
673 {
674 if (identifier.compare(0, 6, "webgl_") == 0)
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 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300679 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300682 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 }
684 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300685 if (identifier.find("__") != TString::npos)
686 {
687 error(line,
688 "identifiers containing two consecutive underscores (__) are reserved as "
689 "possible future keywords",
690 identifier.c_str());
691 return false;
692 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694}
695
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300696// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300697bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800701 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300704 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300709 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200710 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300711 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200712 {
jchen10cc2a10e2017-05-03 14:05:12 +0800713 std::string reason("cannot convert a variable with type ");
714 reason += getBasicString(argTyped->getBasicType());
715 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300716 return false;
717 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800718 else if (argTyped->getMemoryQualifier().writeonly)
719 {
720 error(line, "cannot convert a variable with writeonly", "constructor");
721 return false;
722 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 if (argTyped->getBasicType() == EbtVoid)
724 {
725 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300726 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200727 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729
Olli Etuaho856c4972016-08-08 11:38:39 +0300730 if (type.isArray())
731 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 // The size of an unsized constructor should already have been determined.
733 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300734 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300735 {
736 error(line, "array constructor needs one argument per array element", "constructor");
737 return false;
738 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
740 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800741 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 {
743 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300744 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500745 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500747 return false;
748 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300749 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000751 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300752 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 }
754 }
755 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300757 {
758 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300759 if (fields.size() != arguments->size())
760 {
761 error(line,
762 "Number of constructor parameters does not match the number of structure fields",
763 "constructor");
764 return false;
765 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300766
767 for (size_t i = 0; i < fields.size(); i++)
768 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800769 if (i >= arguments->size() ||
770 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 {
772 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000773 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300775 }
776 }
777 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300778 else
779 {
780 // We're constructing a scalar, vector, or matrix.
781
782 // Note: It's okay to have too many components available, but not okay to have unused
783 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
784 // there is an extra argument, so 'overFull' will become true.
785
786 size_t size = 0;
787 bool full = false;
788 bool overFull = false;
789 bool matrixArg = false;
790 for (TIntermNode *arg : *arguments)
791 {
792 const TIntermTyped *argTyped = arg->getAsTyped();
793 ASSERT(argTyped != nullptr);
794
Olli Etuaho487b63a2017-05-23 15:55:09 +0300795 if (argTyped->getBasicType() == EbtStruct)
796 {
797 error(line, "a struct cannot be used as a constructor argument for this type",
798 "constructor");
799 return false;
800 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300801 if (argTyped->getType().isArray())
802 {
803 error(line, "constructing from a non-dereferenced array", "constructor");
804 return false;
805 }
806 if (argTyped->getType().isMatrix())
807 {
808 matrixArg = true;
809 }
810
811 size += argTyped->getType().getObjectSize();
812 if (full)
813 {
814 overFull = true;
815 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300816 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300817 {
818 full = true;
819 }
820 }
821
822 if (type.isMatrix() && matrixArg)
823 {
824 if (arguments->size() != 1)
825 {
826 error(line, "constructing matrix from matrix can only take one argument",
827 "constructor");
828 return false;
829 }
830 }
831 else
832 {
833 if (size != 1 && size < type.getObjectSize())
834 {
835 error(line, "not enough data provided for construction", "constructor");
836 return false;
837 }
838 if (overFull)
839 {
840 error(line, "too many arguments", "constructor");
841 return false;
842 }
843 }
844 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300845
Olli Etuaho8a176262016-08-16 14:23:01 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if a void variable has been declared and raise an error message for
850// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851//
852// returns true in case of an error
853//
Olli Etuaho856c4972016-08-08 11:38:39 +0300854bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400855 const TString &identifier,
856 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 if (type == EbtVoid)
859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho8a176262016-08-16 14:23:01 +0300864 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
Jamie Madillb98c3a82015-07-23 14:26:04 -0400867// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300868// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300869bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300871 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000873 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300874 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
Jamie Madillb98c3a82015-07-23 14:26:04 -0400879// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300880// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300881void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
Martin Radev4a9cd802016-09-01 16:51:51 +0300883 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000885 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
jchen10cc2a10e2017-05-03 14:05:12 +0800889bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
890 const TTypeSpecifierNonArray &pType,
891 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 if (pType.type == EbtStruct)
894 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300895 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530896 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000897 std::stringstream reasonStream;
898 reasonStream << reason << " (structure contains a sampler)";
899 std::string reasonStr = reasonStream.str();
900 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 // only samplers need to be checked from structs, since other opaque types can't be struct
904 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530906 }
jchen10cc2a10e2017-05-03 14:05:12 +0800907 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530908 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000909 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
Olli Etuaho856c4972016-08-08 11:38:39 +0300916void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
917 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400918{
919 if (pType.layoutQualifier.location != -1)
920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "location must only be specified for a single input or output variable",
922 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400923 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400924}
925
Olli Etuaho856c4972016-08-08 11:38:39 +0300926void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
927 const TLayoutQualifier &layoutQualifier)
928{
929 if (layoutQualifier.location != -1)
930 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000931 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
932 if (mShaderVersion >= 310)
933 {
934 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800935 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000936 }
937 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300938 }
939}
940
Qin Jiajiaca68d982017-09-18 16:41:56 +0800941void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
942 const TLayoutBlockStorage &blockStorage,
943 const TQualifier &qualifier)
944{
945 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
946 {
947 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
948 }
949}
950
Martin Radev2cc85b32016-08-05 16:22:53 +0300951void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
952 TQualifier qualifier,
953 const TType &type)
954{
Martin Radev2cc85b32016-08-05 16:22:53 +0300955 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800956 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530957 {
jchen10cc2a10e2017-05-03 14:05:12 +0800958 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300963unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000966
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200967 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
968 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
969 // fold as array size.
970 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000971 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000972 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400977
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 if (constant->getBasicType() == EbtUInt)
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981 }
982 else
983 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400988 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 }
994
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 {
997 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400999 }
1000
1001 // The size of arrays is restricted here to prevent issues further down the
1002 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1003 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1004 const unsigned int sizeLimit = 65536;
1005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 {
1008 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001011
1012 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001016bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1017 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018{
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 if ((elementQualifier.qualifier == EvqAttribute) ||
1020 (elementQualifier.qualifier == EvqVertexIn) ||
1021 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001022 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 TType(elementQualifier).getQualifierString());
1025 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
Olli Etuaho8a176262016-08-16 14:23:01 +03001031// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001032bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1033 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001035 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001036 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001037 error(line, "cannot declare arrays of arrays",
1038 TType(elementType).getCompleteString().c_str());
1039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001041 return true;
1042}
1043
1044// Check if this qualified element type can be formed into an array. This is only called when array
1045// brackets are associated with an identifier in a declaration, like this:
1046// float a[2];
1047// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1048// are associated with the type, like this:
1049// float[2] a;
1050bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1051 const TPublicType &elementType)
1052{
1053 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1054 {
1055 return false;
1056 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001057 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1058 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1059 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001060 // Geometry shader requires each user-defined input be declared as arrays or inside input
1061 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1062 // interface matching, such variables and blocks are treated as though they were not declared
1063 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001064 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001065 sh::IsVarying(elementType.qualifier) &&
1066 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001067 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001068 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001069 TType(elementType).getCompleteString().c_str());
1070 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001071 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001072 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001076void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1077 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001078 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho3739d232015-04-08 12:23:44 +03001080 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001081 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001082 {
1083 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001084 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001085
1086 // Generate informative error messages for ESSL1.
1087 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001088 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001089 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091 "structures containing arrays may not be declared constant since they cannot be "
1092 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001094 }
1095 else
1096 {
1097 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1098 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001100 // This will make the type sized if it isn't sized yet.
1101 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1102 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
Olli Etuaho2935c582015-04-08 14:32:06 +03001105// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106// and update the symbol table.
1107//
Olli Etuaho2935c582015-04-08 14:32:06 +03001108// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110bool TParseContext::declareVariable(const TSourceLoc &line,
1111 const TString &identifier,
1112 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114{
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho195be942017-12-04 23:40:14 +02001117 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1118
Olli Etuaho43364892017-02-13 16:00:12 +00001119 checkBindingIsValid(line, type);
1120
Olli Etuaho856c4972016-08-08 11:38:39 +03001121 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
Olli Etuaho2935c582015-04-08 14:32:06 +03001123 // gl_LastFragData may be redeclared with a new precision qualifier
1124 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1127 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001128 if (type.isArrayOfArrays())
1129 {
1130 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1131 identifier.c_str());
1132 return false;
1133 }
1134 else if (static_cast<int>(type.getOutermostArraySize()) ==
1135 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001137 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001139 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 }
1141 }
1142 else
1143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1145 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
Olli Etuaho195be942017-12-04 23:40:14 +02001153 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 {
1155 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 return false;
1157 }
1158
Olli Etuaho8a176262016-08-16 14:23:01 +03001159 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001160 return false;
1161
1162 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163}
1164
Martin Radev70866b82016-07-22 15:27:42 +03001165void TParseContext::checkIsParameterQualifierValid(
1166 const TSourceLoc &line,
1167 const TTypeQualifierBuilder &typeQualifierBuilder,
1168 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301169{
Olli Etuahocce89652017-06-19 16:04:09 +03001170 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001171 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001172
1173 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301174 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001175 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1176 }
1177
1178 if (!IsImage(type->getBasicType()))
1179 {
Olli Etuaho43364892017-02-13 16:00:12 +00001180 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001181 }
1182 else
1183 {
1184 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
Martin Radev70866b82016-07-22 15:27:42 +03001187 type->setQualifier(typeQualifier.qualifier);
1188
1189 if (typeQualifier.precision != EbpUndefined)
1190 {
1191 type->setPrecision(typeQualifier.precision);
1192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
Olli Etuaho703671e2017-11-08 17:47:18 +02001195template <size_t size>
1196bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1197 const std::array<TExtension, size> &extensions)
1198{
1199 ASSERT(!extensions.empty());
1200 const TExtensionBehavior &extBehavior = extensionBehavior();
1201
1202 bool canUseWithWarning = false;
1203 bool canUseWithoutWarning = false;
1204
1205 const char *errorMsgString = "";
1206 TExtension errorMsgExtension = TExtension::UNDEFINED;
1207
1208 for (TExtension extension : extensions)
1209 {
1210 auto extIter = extBehavior.find(extension);
1211 if (canUseWithWarning)
1212 {
1213 // We already have an extension that we can use, but with a warning.
1214 // See if we can use the alternative extension without a warning.
1215 if (extIter == extBehavior.end())
1216 {
1217 continue;
1218 }
1219 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1220 {
1221 canUseWithoutWarning = true;
1222 break;
1223 }
1224 continue;
1225 }
1226 if (extIter == extBehavior.end())
1227 {
1228 errorMsgString = "extension is not supported";
1229 errorMsgExtension = extension;
1230 }
1231 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1232 {
1233 errorMsgString = "extension is disabled";
1234 errorMsgExtension = extension;
1235 }
1236 else if (extIter->second == EBhWarn)
1237 {
1238 errorMsgExtension = extension;
1239 canUseWithWarning = true;
1240 }
1241 else
1242 {
1243 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1244 canUseWithoutWarning = true;
1245 break;
1246 }
1247 }
1248
1249 if (canUseWithoutWarning)
1250 {
1251 return true;
1252 }
1253 if (canUseWithWarning)
1254 {
1255 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1256 return true;
1257 }
1258 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1259 return false;
1260}
1261
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 1> &extensions);
1265template bool TParseContext::checkCanUseOneOfExtensions(
1266 const TSourceLoc &line,
1267 const std::array<TExtension, 2> &extensions);
1268template bool TParseContext::checkCanUseOneOfExtensions(
1269 const TSourceLoc &line,
1270 const std::array<TExtension, 3> &extensions);
1271
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001272bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001273{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001274 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001275 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276}
1277
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001278// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1279// compile-time or link-time errors are the same whether or not the declaration is empty".
1280// This function implements all the checks that are done on qualifiers regardless of if the
1281// declaration is empty.
1282void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1283 const sh::TLayoutQualifier &layoutQualifier,
1284 const TSourceLoc &location)
1285{
1286 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1287 {
1288 error(location, "Shared memory declarations cannot have layout specified", "layout");
1289 }
1290
1291 if (layoutQualifier.matrixPacking != EmpUnspecified)
1292 {
1293 error(location, "layout qualifier only valid for interface blocks",
1294 getMatrixPackingString(layoutQualifier.matrixPacking));
1295 return;
1296 }
1297
1298 if (layoutQualifier.blockStorage != EbsUnspecified)
1299 {
1300 error(location, "layout qualifier only valid for interface blocks",
1301 getBlockStorageString(layoutQualifier.blockStorage));
1302 return;
1303 }
1304
1305 if (qualifier == EvqFragmentOut)
1306 {
1307 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1308 {
1309 error(location, "invalid layout qualifier combination", "yuv");
1310 return;
1311 }
1312 }
1313 else
1314 {
1315 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1316 }
1317
Olli Etuaho95468d12017-05-04 11:14:34 +03001318 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1319 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001320 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1321 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001322 {
1323 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1324 }
1325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001329 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001330 // We're not checking whether the uniform location is in range here since that depends on
1331 // the type of the variable.
1332 // The type can only be fully determined for non-empty declarations.
1333 }
1334 if (!canHaveLocation)
1335 {
1336 checkLocationIsNotSpecified(location, layoutQualifier);
1337 }
1338}
1339
jchen104cdac9e2017-05-08 11:01:20 +08001340void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1341 const TSourceLoc &location)
1342{
1343 if (publicType.precision != EbpHigh)
1344 {
1345 error(location, "Can only be highp", "atomic counter");
1346 }
1347 // dEQP enforces compile error if location is specified. See uniform_location.test.
1348 if (publicType.layoutQualifier.location != -1)
1349 {
1350 error(location, "location must not be set for atomic_uint", "layout");
1351 }
1352 if (publicType.layoutQualifier.binding == -1)
1353 {
1354 error(location, "no binding specified", "atomic counter");
1355 }
1356}
1357
Olli Etuaho55bde912017-10-25 13:41:13 +03001358void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001359{
Olli Etuaho55bde912017-10-25 13:41:13 +03001360 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001361 {
1362 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1363 // error. It is assumed that this applies to empty declarations as well.
1364 error(location, "empty array declaration needs to specify a size", "");
1365 }
Martin Radevb8b01222016-11-20 23:25:53 +02001366}
1367
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001368// These checks are done for all declarations that are non-empty. They're done for non-empty
1369// declarations starting a declarator list, and declarators that follow an empty declaration.
1370void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1371 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001372{
Olli Etuahofa33d582015-04-09 14:33:12 +03001373 switch (publicType.qualifier)
1374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375 case EvqVaryingIn:
1376 case EvqVaryingOut:
1377 case EvqAttribute:
1378 case EvqVertexIn:
1379 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001380 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001381 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001382 {
1383 error(identifierLocation, "cannot be used with a structure",
1384 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001385 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001386 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001387 break;
1388 case EvqBuffer:
1389 if (publicType.getBasicType() != EbtInterfaceBlock)
1390 {
1391 error(identifierLocation,
1392 "cannot declare buffer variables at global scope(outside a block)",
1393 getQualifierString(publicType.qualifier));
1394 return;
1395 }
1396 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 default:
1398 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001399 }
jchen10cc2a10e2017-05-03 14:05:12 +08001400 std::string reason(getBasicString(publicType.getBasicType()));
1401 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001402 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001403 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001404 {
1405 return;
1406 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001407
Andrei Volykhina5527072017-03-22 16:46:30 +03001408 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1409 publicType.qualifier != EvqConst) &&
1410 publicType.getBasicType() == EbtYuvCscStandardEXT)
1411 {
1412 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1413 getQualifierString(publicType.qualifier));
1414 return;
1415 }
1416
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1418 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001419 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1420 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001421 TType type(publicType);
1422 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001424 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1425 publicType.layoutQualifier);
1426 }
1427 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001428
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001429 // check for layout qualifier issues
1430 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001431
Martin Radev2cc85b32016-08-05 16:22:53 +03001432 if (IsImage(publicType.getBasicType()))
1433 {
1434
1435 switch (layoutQualifier.imageInternalFormat)
1436 {
1437 case EiifRGBA32F:
1438 case EiifRGBA16F:
1439 case EiifR32F:
1440 case EiifRGBA8:
1441 case EiifRGBA8_SNORM:
1442 if (!IsFloatImage(publicType.getBasicType()))
1443 {
1444 error(identifierLocation,
1445 "internal image format requires a floating image type",
1446 getBasicString(publicType.getBasicType()));
1447 return;
1448 }
1449 break;
1450 case EiifRGBA32I:
1451 case EiifRGBA16I:
1452 case EiifRGBA8I:
1453 case EiifR32I:
1454 if (!IsIntegerImage(publicType.getBasicType()))
1455 {
1456 error(identifierLocation,
1457 "internal image format requires an integer image type",
1458 getBasicString(publicType.getBasicType()));
1459 return;
1460 }
1461 break;
1462 case EiifRGBA32UI:
1463 case EiifRGBA16UI:
1464 case EiifRGBA8UI:
1465 case EiifR32UI:
1466 if (!IsUnsignedImage(publicType.getBasicType()))
1467 {
1468 error(identifierLocation,
1469 "internal image format requires an unsigned image type",
1470 getBasicString(publicType.getBasicType()));
1471 return;
1472 }
1473 break;
1474 case EiifUnspecified:
1475 error(identifierLocation, "layout qualifier", "No image internal format specified");
1476 return;
1477 default:
1478 error(identifierLocation, "layout qualifier", "unrecognized token");
1479 return;
1480 }
1481
1482 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1483 switch (layoutQualifier.imageInternalFormat)
1484 {
1485 case EiifR32F:
1486 case EiifR32I:
1487 case EiifR32UI:
1488 break;
1489 default:
1490 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1491 {
1492 error(identifierLocation, "layout qualifier",
1493 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1494 "image variables must be qualified readonly and/or writeonly");
1495 return;
1496 }
1497 break;
1498 }
1499 }
1500 else
1501 {
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001503 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1504 }
jchen104cdac9e2017-05-08 11:01:20 +08001505
1506 if (IsAtomicCounter(publicType.getBasicType()))
1507 {
1508 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1509 }
1510 else
1511 {
1512 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1513 }
Olli Etuaho43364892017-02-13 16:00:12 +00001514}
Martin Radev2cc85b32016-08-05 16:22:53 +03001515
Olli Etuaho43364892017-02-13 16:00:12 +00001516void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1517{
1518 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1520 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1521 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1522 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1523 // when it comes to which shaders are accepted by the compiler.
1524 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001525 if (IsImage(type.getBasicType()))
1526 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001527 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1528 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001529 }
1530 else if (IsSampler(type.getBasicType()))
1531 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1533 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001534 }
jchen104cdac9e2017-05-08 11:01:20 +08001535 else if (IsAtomicCounter(type.getBasicType()))
1536 {
1537 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1538 }
Olli Etuaho43364892017-02-13 16:00:12 +00001539 else
1540 {
1541 ASSERT(!IsOpaqueType(type.getBasicType()));
1542 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001543 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001544}
1545
Olli Etuaho856c4972016-08-08 11:38:39 +03001546void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1547 const TString &layoutQualifierName,
1548 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001549{
1550
1551 if (mShaderVersion < versionRequired)
1552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001553 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001554 }
1555}
1556
Olli Etuaho856c4972016-08-08 11:38:39 +03001557bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1558 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001559{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001560 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001561 for (size_t i = 0u; i < localSize.size(); ++i)
1562 {
1563 if (localSize[i] != -1)
1564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001565 error(location,
1566 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1567 "global layout declaration",
1568 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001569 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001570 }
1571 }
1572
Olli Etuaho8a176262016-08-16 14:23:01 +03001573 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001574}
1575
Olli Etuaho43364892017-02-13 16:00:12 +00001576void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001577 TLayoutImageInternalFormat internalFormat)
1578{
1579 if (internalFormat != EiifUnspecified)
1580 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001581 error(location, "invalid layout qualifier: only valid when used with images",
1582 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001583 }
Olli Etuaho43364892017-02-13 16:00:12 +00001584}
1585
1586void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1587{
1588 if (binding != -1)
1589 {
1590 error(location,
1591 "invalid layout qualifier: only valid when used with opaque types or blocks",
1592 "binding");
1593 }
1594}
1595
jchen104cdac9e2017-05-08 11:01:20 +08001596void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1597{
1598 if (offset != -1)
1599 {
1600 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1601 "offset");
1602 }
1603}
1604
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001605void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1606 int binding,
1607 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001608{
1609 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001611 {
1612 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1613 }
1614}
1615
1616void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1617 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001619{
1620 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001621 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001622 {
1623 error(location, "sampler binding greater than maximum texture units", "binding");
1624 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001625}
1626
Jiajia Qinbc585152017-06-23 15:42:17 +08001627void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1628 const TQualifier &qualifier,
1629 int binding,
1630 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001631{
1632 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001635 if (binding + size > mMaxUniformBufferBindings)
1636 {
1637 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1638 "binding");
1639 }
1640 }
1641 else if (qualifier == EvqBuffer)
1642 {
1643 if (binding + size > mMaxShaderStorageBufferBindings)
1644 {
1645 error(location,
1646 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1647 "binding");
1648 }
jchen10af713a22017-04-19 09:10:56 +08001649 }
1650}
jchen104cdac9e2017-05-08 11:01:20 +08001651void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1652{
1653 if (binding >= mMaxAtomicCounterBindings)
1654 {
1655 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1656 "binding");
1657 }
1658}
jchen10af713a22017-04-19 09:10:56 +08001659
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001660void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1661 int objectLocationCount,
1662 const TLayoutQualifier &layoutQualifier)
1663{
1664 int loc = layoutQualifier.location;
1665 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1666 {
1667 error(location, "Uniform location out of range", "location");
1668 }
1669}
1670
Andrei Volykhina5527072017-03-22 16:46:30 +03001671void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1672{
1673 if (yuv != false)
1674 {
1675 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1676 }
1677}
1678
Jiajia Qinbc585152017-06-23 15:42:17 +08001679void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1680 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001681{
1682 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1683 {
1684 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001685 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1686 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1687 qual == EvqInOut || qual == EvqConstReadOnly))
1688 {
1689 if (argument->getMemoryQualifier().writeonly)
1690 {
1691 error(argument->getLine(),
1692 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001693 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001694 return;
1695 }
1696 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 if (qual == EvqOut || qual == EvqInOut)
1698 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001699 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001701 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001702 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001703 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001704 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001705 }
1706 }
1707 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001708}
1709
Martin Radev70866b82016-07-22 15:27:42 +03001710void TParseContext::checkInvariantVariableQualifier(bool invariant,
1711 const TQualifier qualifier,
1712 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001713{
Martin Radev70866b82016-07-22 15:27:42 +03001714 if (!invariant)
1715 return;
1716
1717 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001718 {
Martin Radev70866b82016-07-22 15:27:42 +03001719 // input variables in the fragment shader can be also qualified as invariant
1720 if (!sh::CanBeInvariantESSL1(qualifier))
1721 {
1722 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1723 }
1724 }
1725 else
1726 {
1727 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1728 {
1729 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1730 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731 }
1732}
1733
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001735{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001736 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001737}
1738
Jamie Madillb98c3a82015-07-23 14:26:04 -04001739void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1740 const char *extName,
1741 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001742{
1743 pp::SourceLocation srcLoc;
1744 srcLoc.file = loc.first_file;
1745 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001746 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001747}
1748
Jamie Madillb98c3a82015-07-23 14:26:04 -04001749void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1750 const char *name,
1751 const char *value,
1752 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001753{
1754 pp::SourceLocation srcLoc;
1755 srcLoc.file = loc.first_file;
1756 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001757 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001758}
1759
Martin Radev4c4c8e72016-08-04 12:25:34 +03001760sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001761{
Jamie Madill2f294c92017-11-20 14:47:26 -05001762 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001763 for (size_t i = 0u; i < result.size(); ++i)
1764 {
1765 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1766 {
1767 result[i] = 1;
1768 }
1769 else
1770 {
1771 result[i] = mComputeShaderLocalSize[i];
1772 }
1773 }
1774 return result;
1775}
1776
Olli Etuaho56229f12017-07-10 14:16:33 +03001777TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1778 const TSourceLoc &line)
1779{
1780 TIntermConstantUnion *node = new TIntermConstantUnion(
1781 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1782 node->setLine(line);
1783 return node;
1784}
1785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786/////////////////////////////////////////////////////////////////////////////////
1787//
1788// Non-Errors.
1789//
1790/////////////////////////////////////////////////////////////////////////////////
1791
Jamie Madill5c097022014-08-20 16:38:32 -04001792const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1793 const TString *name,
1794 const TSymbol *symbol)
1795{
Jamie Madill5c097022014-08-20 16:38:32 -04001796 if (!symbol)
1797 {
1798 error(location, "undeclared identifier", name->c_str());
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 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
1804 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001805 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001806 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001807
1808 const TVariable *variable = static_cast<const TVariable *>(symbol);
1809
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001810 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001811 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001812 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001813 }
1814
Olli Etuaho0f684632017-07-13 12:42:15 +03001815 // Reject shaders using both gl_FragData and gl_FragColor
1816 TQualifier qualifier = variable->getType().getQualifier();
1817 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001818 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 mUsesFragData = true;
1820 }
1821 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1822 {
1823 mUsesFragColor = true;
1824 }
1825 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1826 {
1827 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001828 }
1829
Olli Etuaho0f684632017-07-13 12:42:15 +03001830 // This validation is not quite correct - it's only an error to write to
1831 // both FragData and FragColor. For simplicity, and because users shouldn't
1832 // be rewarded for reading from undefined varaibles, return an error
1833 // if they are both referenced, rather than assigned.
1834 if (mUsesFragData && mUsesFragColor)
1835 {
1836 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1837 if (mUsesSecondaryOutputs)
1838 {
1839 errorMessage =
1840 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1841 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1842 }
1843 error(location, errorMessage, name->c_str());
1844 }
1845
1846 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1847 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1848 qualifier == EvqWorkGroupSize)
1849 {
1850 error(location,
1851 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1852 "gl_WorkGroupSize");
1853 }
Jamie Madill5c097022014-08-20 16:38:32 -04001854 return variable;
1855}
1856
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001857TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1858 const TString *name,
1859 const TSymbol *symbol)
1860{
1861 const TVariable *variable = getNamedVariable(location, name, symbol);
1862
Olli Etuaho0f684632017-07-13 12:42:15 +03001863 if (!variable)
1864 {
1865 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1866 node->setLine(location);
1867 return node;
1868 }
1869
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001871 TIntermTyped *node = nullptr;
1872
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001873 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001874 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001875 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001879 {
1880 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1881 // needs to be added to the AST as a constant and not as a symbol.
1882 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1883 TConstantUnion *constArray = new TConstantUnion[3];
1884 for (size_t i = 0; i < 3; ++i)
1885 {
1886 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1887 }
1888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 ASSERT(variableType.getBasicType() == EbtUInt);
1890 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001894 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001895 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1897 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001898 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001899 ASSERT(mGlInVariableWithArraySize != nullptr);
1900 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Olli Etuaho195be942017-12-04 23:40:14 +02001904 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001917 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuaho376f1b52015-04-13 13:23:41 +03001924 if (type.isUnsizedArray())
1925 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001926 // In case initializer is not an array or type has more dimensions than initializer, this
1927 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1928 // actually is an array or not. Having a non-array initializer for an unsized array will
1929 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001930 auto *arraySizes = initializer->getType().getArraySizes();
1931 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001932 }
Olli Etuaho195be942017-12-04 23:40:14 +02001933
1934 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001935 if (!declareVariable(line, identifier, type, &variable))
1936 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939
Olli Etuahob0c645e2015-05-12 14:25:36 +03001940 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001941 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001942 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001943 {
1944 // Error message does not completely match behavior with ESSL 1.00, but
1945 // we want to steer developers towards only using constant expressions.
1946 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001947 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 }
1949 if (globalInitWarning)
1950 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001951 warning(
1952 line,
1953 "global variable initializers should be constant expressions "
1954 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1955 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001956 }
1957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 //
1959 // identifier must be of type constant, a global, or a temporary
1960 //
1961 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301962 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 error(line, " cannot initialize this type of qualifier ",
1965 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001966 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 }
1968 //
1969 // test for and propagate constant
1970 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
Arun Patole7e7e68d2015-05-22 12:02:25 +05301972 if (qualifier == EvqConst)
1973 {
1974 if (qualifier != initializer->getType().getQualifier())
1975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001976 std::stringstream reasonStream;
1977 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1978 << "'";
1979 std::string reason = reasonStream.str();
1980 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001981 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001983 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (type != initializer->getType())
1985 {
1986 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001987 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001988 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001989 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001990 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001991
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001992 // Save the constant folded value to the variable if possible.
1993 const TConstantUnion *constArray = initializer->getConstantValue();
1994 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001996 variable->shareConstPointer(constArray);
1997 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001998 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001999 ASSERT(*initNode == nullptr);
2000 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002001 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002002 }
2003 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002004
Olli Etuaho195be942017-12-04 23:40:14 +02002005 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002006 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002007 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2008 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002009 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002010 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002011 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002012 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013
Olli Etuaho914b79a2017-06-19 16:03:19 +03002014 return true;
2015}
2016
2017TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2018 const TString &identifier,
2019 TIntermTyped *initializer,
2020 const TSourceLoc &loc)
2021{
2022 checkIsScalarBool(loc, pType);
2023 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002024 TType type(pType);
2025 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002026 {
2027 // The initializer is valid. The init condition needs to have a node - either the
2028 // initializer node, or a constant node in case the initialized variable is const and won't
2029 // be recorded in the AST.
2030 if (initNode == nullptr)
2031 {
2032 return initializer;
2033 }
2034 else
2035 {
2036 TIntermDeclaration *declaration = new TIntermDeclaration();
2037 declaration->appendDeclarator(initNode);
2038 return declaration;
2039 }
2040 }
2041 return nullptr;
2042}
2043
2044TIntermNode *TParseContext::addLoop(TLoopType type,
2045 TIntermNode *init,
2046 TIntermNode *cond,
2047 TIntermTyped *expr,
2048 TIntermNode *body,
2049 const TSourceLoc &line)
2050{
2051 TIntermNode *node = nullptr;
2052 TIntermTyped *typedCond = nullptr;
2053 if (cond)
2054 {
2055 typedCond = cond->getAsTyped();
2056 }
2057 if (cond == nullptr || typedCond)
2058 {
Olli Etuahocce89652017-06-19 16:04:09 +03002059 if (type == ELoopDoWhile)
2060 {
2061 checkIsScalarBool(line, typedCond);
2062 }
2063 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2064 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2065 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2066 !typedCond->isVector()));
2067
Olli Etuaho3ec75682017-07-05 17:02:55 +03002068 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002069 node->setLine(line);
2070 return node;
2071 }
2072
Olli Etuahocce89652017-06-19 16:04:09 +03002073 ASSERT(type != ELoopDoWhile);
2074
Olli Etuaho914b79a2017-06-19 16:03:19 +03002075 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2076 ASSERT(declaration);
2077 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2078 ASSERT(declarator->getLeft()->getAsSymbolNode());
2079
2080 // The condition is a declaration. In the AST representation we don't support declarations as
2081 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2082 // the loop.
2083 TIntermBlock *block = new TIntermBlock();
2084
2085 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2086 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2087 block->appendStatement(declareCondition);
2088
2089 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2090 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002091 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002092 block->appendStatement(loop);
2093 loop->setLine(line);
2094 block->setLine(line);
2095 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096}
2097
Olli Etuahocce89652017-06-19 16:04:09 +03002098TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2099 TIntermNodePair code,
2100 const TSourceLoc &loc)
2101{
Olli Etuaho56229f12017-07-10 14:16:33 +03002102 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002103
2104 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002105 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002106 {
2107 if (cond->getAsConstantUnion()->getBConst(0) == true)
2108 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002109 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002110 }
2111 else
2112 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002113 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002114 }
2115 }
2116
Olli Etuaho3ec75682017-07-05 17:02:55 +03002117 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002118 node->setLine(loc);
2119
2120 return node;
2121}
2122
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002123void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2124{
2125 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2126 typeSpecifier->getBasicType());
2127
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002128 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002129 {
2130 error(typeSpecifier->getLine(), "not supported", "first-class array");
2131 typeSpecifier->clearArrayness();
2132 }
2133}
2134
Martin Radev70866b82016-07-22 15:27:42 +03002135TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302136 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002137{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002138 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002139
Martin Radev70866b82016-07-22 15:27:42 +03002140 TPublicType returnType = typeSpecifier;
2141 returnType.qualifier = typeQualifier.qualifier;
2142 returnType.invariant = typeQualifier.invariant;
2143 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002144 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002145 returnType.precision = typeSpecifier.precision;
2146
2147 if (typeQualifier.precision != EbpUndefined)
2148 {
2149 returnType.precision = typeQualifier.precision;
2150 }
2151
Martin Radev4a9cd802016-09-01 16:51:51 +03002152 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2153 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002154
Martin Radev4a9cd802016-09-01 16:51:51 +03002155 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2156 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002157
Martin Radev4a9cd802016-09-01 16:51:51 +03002158 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002160 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002161 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002162 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002163 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002164 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002165 returnType.clearArrayness();
2166 }
2167
Martin Radev70866b82016-07-22 15:27:42 +03002168 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002169 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002170 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002172 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002173 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002174
Martin Radev70866b82016-07-22 15:27:42 +03002175 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002176 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002177 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002179 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002180 }
2181 }
2182 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002183 {
Martin Radev70866b82016-07-22 15:27:42 +03002184 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002185 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002187 }
Martin Radev70866b82016-07-22 15:27:42 +03002188 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2189 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002190 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002191 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2192 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002193 }
Martin Radev70866b82016-07-22 15:27:42 +03002194 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002195 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002196 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002197 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002198 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002199 }
2200
2201 return returnType;
2202}
2203
Olli Etuaho856c4972016-08-08 11:38:39 +03002204void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2205 const TPublicType &type,
2206 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002207{
2208 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002209 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002210 {
2211 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002212 }
2213
2214 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2215 switch (qualifier)
2216 {
2217 case EvqVertexIn:
2218 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002219 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 {
2221 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002222 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002223 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 return;
2225 case EvqFragmentOut:
2226 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002227 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 {
2229 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002231 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 return;
2233 default:
2234 break;
2235 }
2236
2237 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2238 // restrictions.
2239 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002240 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2241 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002242 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2243 {
2244 error(qualifierLocation, "must use 'flat' interpolation here",
2245 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002246 }
2247
Martin Radev4a9cd802016-09-01 16:51:51 +03002248 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002249 {
2250 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2251 // These restrictions are only implied by the ESSL 3.00 spec, but
2252 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002253 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 {
2255 error(qualifierLocation, "cannot be an array of structures",
2256 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 }
2258 if (type.isStructureContainingArrays())
2259 {
2260 error(qualifierLocation, "cannot be a structure containing an array",
2261 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 }
2263 if (type.isStructureContainingType(EbtStruct))
2264 {
2265 error(qualifierLocation, "cannot be a structure containing a structure",
2266 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002267 }
2268 if (type.isStructureContainingType(EbtBool))
2269 {
2270 error(qualifierLocation, "cannot be a structure containing a bool",
2271 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002272 }
2273 }
2274}
2275
Martin Radev2cc85b32016-08-05 16:22:53 +03002276void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2277{
2278 if (qualifier.getType() == QtStorage)
2279 {
2280 const TStorageQualifierWrapper &storageQualifier =
2281 static_cast<const TStorageQualifierWrapper &>(qualifier);
2282 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2283 !symbolTable.atGlobalLevel())
2284 {
2285 error(storageQualifier.getLine(),
2286 "Local variables can only use the const storage qualifier.",
2287 storageQualifier.getQualifierString().c_str());
2288 }
2289 }
2290}
2291
Olli Etuaho43364892017-02-13 16:00:12 +00002292void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002293 const TSourceLoc &location)
2294{
Jiajia Qinbc585152017-06-23 15:42:17 +08002295 const std::string reason(
2296 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2297 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002298 if (memoryQualifier.readonly)
2299 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002300 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002301 }
2302 if (memoryQualifier.writeonly)
2303 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002304 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002305 }
Martin Radev049edfa2016-11-11 14:35:37 +02002306 if (memoryQualifier.coherent)
2307 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002309 }
2310 if (memoryQualifier.restrictQualifier)
2311 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002312 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002313 }
2314 if (memoryQualifier.volatileQualifier)
2315 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002316 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002317 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002318}
2319
jchen104cdac9e2017-05-08 11:01:20 +08002320// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2321// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002322void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2323 const TSourceLoc &loc,
2324 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002325{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002326 if (!IsAtomicCounter(type->getBasicType()))
2327 {
2328 return;
2329 }
2330
2331 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2332 : kAtomicCounterSize;
2333 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2334 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002335 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002336 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002337 {
2338 offset = bindingState.appendSpan(size);
2339 }
2340 else
2341 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002342 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002343 }
2344 if (offset == -1)
2345 {
2346 error(loc, "Offset overlapping", "atomic counter");
2347 return;
2348 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 layoutQualifier.offset = offset;
2350 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002351}
2352
Olli Etuaho454c34c2017-10-25 16:35:56 +03002353void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2354 const char *token,
2355 TType *type)
2356{
2357 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2358 {
2359 if (type->isArray() && type->getOutermostArraySize() == 0u)
2360 {
2361 // Set size for the unsized geometry shader inputs if they are declared after a valid
2362 // input primitive declaration.
2363 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2364 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002365 ASSERT(mGlInVariableWithArraySize != nullptr);
2366 type->sizeOutermostUnsizedArray(
2367 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002368 }
2369 else
2370 {
2371 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2372 // An input can be declared without an array size if there is a previous layout
2373 // which specifies the size.
2374 error(location,
2375 "Missing a valid input primitive declaration before declaring an unsized "
2376 "array input",
2377 token);
2378 }
2379 }
2380 else if (type->isArray())
2381 {
2382 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2383 }
2384 else
2385 {
2386 error(location, "Geometry shader input variable must be declared as an array", token);
2387 }
2388 }
2389}
2390
Olli Etuaho13389b62016-10-16 11:48:18 +01002391TIntermDeclaration *TParseContext::parseSingleDeclaration(
2392 TPublicType &publicType,
2393 const TSourceLoc &identifierOrTypeLocation,
2394 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002395{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002396 TType type(publicType);
2397 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2398 mDirectiveHandler.pragma().stdgl.invariantAll)
2399 {
2400 TQualifier qualifier = type.getQualifier();
2401
2402 // The directive handler has already taken care of rejecting invalid uses of this pragma
2403 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2404 // affected variable declarations:
2405 //
2406 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2407 // elsewhere, in TranslatorGLSL.)
2408 //
2409 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2410 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2411 // the way this is currently implemented we have to enable this compiler option before
2412 // parsing the shader and determining the shading language version it uses. If this were
2413 // implemented as a post-pass, the workaround could be more targeted.
2414 //
2415 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2416 // the specification, but there are desktop OpenGL drivers that expect that this is the
2417 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2418 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2419 {
2420 type.setInvariant(true);
2421 }
2422 }
2423
Olli Etuaho454c34c2017-10-25 16:35:56 +03002424 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002425
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002426 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2427 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002428
Olli Etuahobab4c082015-04-24 16:38:49 +03002429 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002430 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002431
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002432 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002433 if (emptyDeclaration)
2434 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002435 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002436 // In most cases we don't need to create a symbol node for an empty declaration.
2437 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2438 if (type.getBasicType() == EbtStruct)
2439 {
Olli Etuaho195be942017-12-04 23:40:14 +02002440 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002441 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002442 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 }
jchen104cdac9e2017-05-08 11:01:20 +08002444 else if (IsAtomicCounter(publicType.getBasicType()))
2445 {
2446 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2447 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002448 }
2449 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002450 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002451 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002452
Olli Etuaho55bde912017-10-25 13:41:13 +03002453 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002454
Olli Etuaho55bc9052017-10-25 17:33:06 +03002455 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002456
Olli Etuaho2935c582015-04-08 14:32:06 +03002457 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002458 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002459 {
Olli Etuaho195be942017-12-04 23:40:14 +02002460 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002461 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002462 }
2463
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002464 TIntermDeclaration *declaration = new TIntermDeclaration();
2465 declaration->setLine(identifierOrTypeLocation);
2466 if (symbol)
2467 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002468 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002469 declaration->appendDeclarator(symbol);
2470 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002471 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002472}
2473
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002474TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2475 TPublicType &elementType,
2476 const TSourceLoc &identifierLocation,
2477 const TString &identifier,
2478 const TSourceLoc &indexLocation,
2479 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002480{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002482
Olli Etuaho55bde912017-10-25 13:41:13 +03002483 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002484 identifierLocation);
2485
Olli Etuaho55bde912017-10-25 13:41:13 +03002486 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002487
Olli Etuaho55bde912017-10-25 13:41:13 +03002488 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002491 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002492
Olli Etuaho454c34c2017-10-25 16:35:56 +03002493 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002494
2495 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2496
Olli Etuaho55bc9052017-10-25 17:33:06 +03002497 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002498
Olli Etuaho13389b62016-10-16 11:48:18 +01002499 TIntermDeclaration *declaration = new TIntermDeclaration();
2500 declaration->setLine(identifierLocation);
2501
Olli Etuaho195be942017-12-04 23:40:14 +02002502 TVariable *variable = nullptr;
2503 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 {
Olli Etuaho195be942017-12-04 23:40:14 +02002505 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002506 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002507 declaration->appendDeclarator(symbol);
2508 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002511}
2512
Olli Etuaho13389b62016-10-16 11:48:18 +01002513TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2514 const TSourceLoc &identifierLocation,
2515 const TString &identifier,
2516 const TSourceLoc &initLocation,
2517 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002518{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002519 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002520
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2522 identifierLocation);
2523
2524 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002525
Olli Etuaho13389b62016-10-16 11:48:18 +01002526 TIntermDeclaration *declaration = new TIntermDeclaration();
2527 declaration->setLine(identifierLocation);
2528
2529 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002530 TType type(publicType);
2531 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002532 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002533 if (initNode)
2534 {
2535 declaration->appendDeclarator(initNode);
2536 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002537 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002538 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002539}
2540
Olli Etuaho13389b62016-10-16 11:48:18 +01002541TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002542 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 const TSourceLoc &identifierLocation,
2544 const TString &identifier,
2545 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002546 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 const TSourceLoc &initLocation,
2548 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002549{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002550 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002551
Olli Etuaho55bde912017-10-25 13:41:13 +03002552 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002553 identifierLocation);
2554
Olli Etuaho55bde912017-10-25 13:41:13 +03002555 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002556
Olli Etuaho55bde912017-10-25 13:41:13 +03002557 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002560 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002561
Olli Etuaho13389b62016-10-16 11:48:18 +01002562 TIntermDeclaration *declaration = new TIntermDeclaration();
2563 declaration->setLine(identifierLocation);
2564
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002566 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002567 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002568 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 if (initNode)
2570 {
2571 declaration->appendDeclarator(initNode);
2572 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002573 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002574
2575 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002576}
2577
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002578TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002579 const TTypeQualifierBuilder &typeQualifierBuilder,
2580 const TSourceLoc &identifierLoc,
2581 const TString *identifier,
2582 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002583{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002584 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002585
Martin Radev70866b82016-07-22 15:27:42 +03002586 if (!typeQualifier.invariant)
2587 {
2588 error(identifierLoc, "Expected invariant", identifier->c_str());
2589 return nullptr;
2590 }
2591 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2592 {
2593 return nullptr;
2594 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002595 if (!symbol)
2596 {
2597 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002598 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002599 }
Martin Radev70866b82016-07-22 15:27:42 +03002600 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601 {
Martin Radev70866b82016-07-22 15:27:42 +03002602 error(identifierLoc, "invariant declaration specifies qualifier",
2603 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002604 }
Martin Radev70866b82016-07-22 15:27:42 +03002605 if (typeQualifier.precision != EbpUndefined)
2606 {
2607 error(identifierLoc, "invariant declaration specifies precision",
2608 getPrecisionString(typeQualifier.precision));
2609 }
2610 if (!typeQualifier.layoutQualifier.isEmpty())
2611 {
2612 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2613 }
2614
2615 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002616 if (!variable)
2617 {
2618 return nullptr;
2619 }
Martin Radev70866b82016-07-22 15:27:42 +03002620 const TType &type = variable->getType();
2621
2622 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2623 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002624 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002625
2626 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2627
Olli Etuaho195be942017-12-04 23:40:14 +02002628 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002629 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002630
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002631 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002632}
2633
Olli Etuaho13389b62016-10-16 11:48:18 +01002634void TParseContext::parseDeclarator(TPublicType &publicType,
2635 const TSourceLoc &identifierLocation,
2636 const TString &identifier,
2637 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002638{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002639 // If the declaration starting this declarator list was empty (example: int,), some checks were
2640 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002641 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002642 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002643 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2644 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002645 }
2646
Olli Etuaho856c4972016-08-08 11:38:39 +03002647 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002648
Olli Etuaho43364892017-02-13 16:00:12 +00002649 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002650
2651 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2652
Olli Etuaho55bde912017-10-25 13:41:13 +03002653 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2654
Olli Etuaho55bc9052017-10-25 17:33:06 +03002655 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2656
Olli Etuaho195be942017-12-04 23:40:14 +02002657 TVariable *variable = nullptr;
2658 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002659 {
Olli Etuaho195be942017-12-04 23:40:14 +02002660 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002661 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002662 declarationOut->appendDeclarator(symbol);
2663 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002664}
2665
Olli Etuaho55bde912017-10-25 13:41:13 +03002666void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002667 const TSourceLoc &identifierLocation,
2668 const TString &identifier,
2669 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002670 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002671 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002672{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002673 // If the declaration starting this declarator list was empty (example: int,), some checks were
2674 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002675 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002676 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002677 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002678 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002679 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002680
Olli Etuaho55bde912017-10-25 13:41:13 +03002681 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002682
Olli Etuaho55bde912017-10-25 13:41:13 +03002683 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002684 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002686 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002687
Olli Etuaho454c34c2017-10-25 16:35:56 +03002688 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2691
Olli Etuaho55bc9052017-10-25 17:33:06 +03002692 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002693
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002694 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002695 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002696 {
Olli Etuaho195be942017-12-04 23:40:14 +02002697 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002698 symbol->setLine(identifierLocation);
2699 declarationOut->appendDeclarator(symbol);
2700 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002701 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002702}
2703
Olli Etuaho13389b62016-10-16 11:48:18 +01002704void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2705 const TSourceLoc &identifierLocation,
2706 const TString &identifier,
2707 const TSourceLoc &initLocation,
2708 TIntermTyped *initializer,
2709 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002710{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002711 // If the declaration starting this declarator list was empty (example: int,), some checks were
2712 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002713 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002714 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002715 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2716 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002717 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002718
Olli Etuaho856c4972016-08-08 11:38:39 +03002719 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002720
Olli Etuaho13389b62016-10-16 11:48:18 +01002721 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002722 TType type(publicType);
2723 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002724 {
2725 //
2726 // build the intermediate representation
2727 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002728 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002729 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002730 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002731 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002732 }
2733}
2734
Olli Etuaho55bde912017-10-25 13:41:13 +03002735void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002736 const TSourceLoc &identifierLocation,
2737 const TString &identifier,
2738 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002739 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 const TSourceLoc &initLocation,
2741 TIntermTyped *initializer,
2742 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002743{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002744 // If the declaration starting this declarator list was empty (example: int,), some checks were
2745 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002746 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002747 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002748 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002749 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002750 }
2751
Olli Etuaho55bde912017-10-25 13:41:13 +03002752 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002753
Olli Etuaho55bde912017-10-25 13:41:13 +03002754 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002757 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002758
2759 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002760 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002761 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762 {
2763 if (initNode)
2764 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002765 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002766 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767 }
2768}
2769
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002770TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2771{
2772 // It's simpler to parse an empty statement as a constant expression rather than having a
2773 // different type of node just for empty statements, that will be pruned from the AST anyway.
2774 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2775 node->setLine(location);
2776 return node;
2777}
2778
jchen104cdac9e2017-05-08 11:01:20 +08002779void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2780 const TSourceLoc &location)
2781{
2782 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2783 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2784 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2785 {
2786 error(location, "Requires both binding and offset", "layout");
2787 return;
2788 }
2789 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2790}
2791
Olli Etuahocce89652017-06-19 16:04:09 +03002792void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2793 const TPublicType &type,
2794 const TSourceLoc &loc)
2795{
2796 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2797 !getFragmentPrecisionHigh())
2798 {
2799 error(loc, "precision is not supported in fragment shader", "highp");
2800 }
2801
2802 if (!CanSetDefaultPrecisionOnType(type))
2803 {
2804 error(loc, "illegal type argument for default precision qualifier",
2805 getBasicString(type.getBasicType()));
2806 return;
2807 }
2808 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2809}
2810
Shaob5cc1192017-07-06 10:47:20 +08002811bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2812{
2813 switch (typeQualifier.layoutQualifier.primitiveType)
2814 {
2815 case EptLines:
2816 case EptLinesAdjacency:
2817 case EptTriangles:
2818 case EptTrianglesAdjacency:
2819 return typeQualifier.qualifier == EvqGeometryIn;
2820
2821 case EptLineStrip:
2822 case EptTriangleStrip:
2823 return typeQualifier.qualifier == EvqGeometryOut;
2824
2825 case EptPoints:
2826 return true;
2827
2828 default:
2829 UNREACHABLE();
2830 return false;
2831 }
2832}
2833
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002834void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2835 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002836{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002837 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002838 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002839 TSymbol *glPerVertex = symbolTable.findBuiltIn("gl_PerVertex", 310);
2840 TInterfaceBlock *glPerVertexBlock = static_cast<TInterfaceBlock *>(glPerVertex);
2841 TType glInType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2842 glInType.makeArray(inputArraySize);
2843 mGlInVariableWithArraySize =
2844 new TVariable(&symbolTable, NewPoolTString("gl_in"), glInType, SymbolType::BuiltIn,
2845 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002846 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002847 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002848 {
2849 error(line,
2850 "Array size or input primitive declaration doesn't match the size of earlier sized "
2851 "array inputs.",
2852 "layout");
2853 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002854}
2855
Shaob5cc1192017-07-06 10:47:20 +08002856bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2857{
2858 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2859
2860 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2861
2862 if (layoutQualifier.maxVertices != -1)
2863 {
2864 error(typeQualifier.line,
2865 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2866 return false;
2867 }
2868
2869 // Set mGeometryInputPrimitiveType if exists
2870 if (layoutQualifier.primitiveType != EptUndefined)
2871 {
2872 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2873 {
2874 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2875 return false;
2876 }
2877
2878 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2879 {
2880 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002881 setGeometryShaderInputArraySize(
2882 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2883 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002884 }
2885 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2886 {
2887 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2888 "layout");
2889 return false;
2890 }
2891 }
2892
2893 // Set mGeometryInvocations if exists
2894 if (layoutQualifier.invocations > 0)
2895 {
2896 if (mGeometryShaderInvocations == 0)
2897 {
2898 mGeometryShaderInvocations = layoutQualifier.invocations;
2899 }
2900 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2901 {
2902 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2903 "layout");
2904 return false;
2905 }
2906 }
2907
2908 return true;
2909}
2910
2911bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2912{
2913 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2914
2915 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2916
2917 if (layoutQualifier.invocations > 0)
2918 {
2919 error(typeQualifier.line,
2920 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2921 return false;
2922 }
2923
2924 // Set mGeometryOutputPrimitiveType if exists
2925 if (layoutQualifier.primitiveType != EptUndefined)
2926 {
2927 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2928 {
2929 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2930 return false;
2931 }
2932
2933 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2934 {
2935 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2936 }
2937 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2938 {
2939 error(typeQualifier.line,
2940 "primitive doesn't match earlier output primitive declaration", "layout");
2941 return false;
2942 }
2943 }
2944
2945 // Set mGeometryMaxVertices if exists
2946 if (layoutQualifier.maxVertices > -1)
2947 {
2948 if (mGeometryShaderMaxVertices == -1)
2949 {
2950 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2951 }
2952 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2953 {
2954 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2955 "layout");
2956 return false;
2957 }
2958 }
2959
2960 return true;
2961}
2962
Martin Radev70866b82016-07-22 15:27:42 +03002963void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002964{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002965 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002966 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002967
Martin Radev70866b82016-07-22 15:27:42 +03002968 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2969 typeQualifier.line);
2970
Jamie Madillc2128ff2016-07-04 10:26:17 -04002971 // It should never be the case, but some strange parser errors can send us here.
2972 if (layoutQualifier.isEmpty())
2973 {
2974 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002975 return;
2976 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002977
Martin Radev802abe02016-08-04 17:48:32 +03002978 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002979 {
Olli Etuaho43364892017-02-13 16:00:12 +00002980 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002981 return;
2982 }
2983
Olli Etuaho43364892017-02-13 16:00:12 +00002984 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2985
2986 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002987
2988 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2989
Andrei Volykhina5527072017-03-22 16:46:30 +03002990 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2991
jchen104cdac9e2017-05-08 11:01:20 +08002992 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2993
Qin Jiajiaca68d982017-09-18 16:41:56 +08002994 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2995 typeQualifier.qualifier);
2996
Martin Radev802abe02016-08-04 17:48:32 +03002997 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002998 {
Martin Radev802abe02016-08-04 17:48:32 +03002999 if (mComputeShaderLocalSizeDeclared &&
3000 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3001 {
3002 error(typeQualifier.line, "Work group size does not match the previous declaration",
3003 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003004 return;
3005 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003006
Martin Radev802abe02016-08-04 17:48:32 +03003007 if (mShaderVersion < 310)
3008 {
3009 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003010 return;
3011 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003012
Martin Radev4c4c8e72016-08-04 12:25:34 +03003013 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003014 {
3015 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003016 return;
3017 }
3018
3019 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3020 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3021
3022 const TConstantUnion *maxComputeWorkGroupSizeData =
3023 maxComputeWorkGroupSize->getConstPointer();
3024
3025 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3026 {
3027 if (layoutQualifier.localSize[i] != -1)
3028 {
3029 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3030 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3031 if (mComputeShaderLocalSize[i] < 1 ||
3032 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3033 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003034 std::stringstream reasonStream;
3035 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3036 << maxComputeWorkGroupSizeValue;
3037 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003038
Olli Etuaho4de340a2016-12-16 09:32:03 +00003039 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003040 return;
3041 }
3042 }
3043 }
3044
3045 mComputeShaderLocalSizeDeclared = true;
3046 }
Shaob5cc1192017-07-06 10:47:20 +08003047 else if (typeQualifier.qualifier == EvqGeometryIn)
3048 {
3049 if (mShaderVersion < 310)
3050 {
3051 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3052 return;
3053 }
3054
3055 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3056 {
3057 return;
3058 }
3059 }
3060 else if (typeQualifier.qualifier == EvqGeometryOut)
3061 {
3062 if (mShaderVersion < 310)
3063 {
3064 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3065 "layout");
3066 return;
3067 }
3068
3069 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3070 {
3071 return;
3072 }
3073 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003074 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3075 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003076 {
3077 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3078 // specification.
3079 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3080 {
3081 error(typeQualifier.line, "Number of views does not match the previous declaration",
3082 "layout");
3083 return;
3084 }
3085
3086 if (layoutQualifier.numViews == -1)
3087 {
3088 error(typeQualifier.line, "No num_views specified", "layout");
3089 return;
3090 }
3091
3092 if (layoutQualifier.numViews > mMaxNumViews)
3093 {
3094 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3095 "layout");
3096 return;
3097 }
3098
3099 mNumViews = layoutQualifier.numViews;
3100 }
Martin Radev802abe02016-08-04 17:48:32 +03003101 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003102 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003103 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003104 {
Martin Radev802abe02016-08-04 17:48:32 +03003105 return;
3106 }
3107
Jiajia Qinbc585152017-06-23 15:42:17 +08003108 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003109 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003110 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003111 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003112 return;
3113 }
3114
3115 if (mShaderVersion < 300)
3116 {
3117 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3118 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003119 return;
3120 }
3121
Olli Etuaho09b04a22016-12-15 13:30:26 +00003122 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003123
3124 if (layoutQualifier.matrixPacking != EmpUnspecified)
3125 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003126 if (typeQualifier.qualifier == EvqUniform)
3127 {
3128 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3129 }
3130 else if (typeQualifier.qualifier == EvqBuffer)
3131 {
3132 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3133 }
Martin Radev802abe02016-08-04 17:48:32 +03003134 }
3135
3136 if (layoutQualifier.blockStorage != EbsUnspecified)
3137 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003138 if (typeQualifier.qualifier == EvqUniform)
3139 {
3140 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3141 }
3142 else if (typeQualifier.qualifier == EvqBuffer)
3143 {
3144 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3145 }
Martin Radev802abe02016-08-04 17:48:32 +03003146 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003147 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003148}
3149
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003150TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3151 const TFunction &function,
3152 const TSourceLoc &location,
3153 bool insertParametersToSymbolTable)
3154{
Olli Etuahobed35d72017-12-20 16:36:26 +02003155 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003156
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003157 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003158 prototype->setLine(location);
3159
3160 for (size_t i = 0; i < function.getParamCount(); i++)
3161 {
3162 const TConstParameter &param = function.getParam(i);
3163
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003164 TIntermSymbol *symbol = nullptr;
3165
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003166 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3167 // be used for unused args).
3168 if (param.name != nullptr)
3169 {
Olli Etuaho195be942017-12-04 23:40:14 +02003170 TVariable *variable =
3171 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3172 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003174 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003175 {
Olli Etuaho195be942017-12-04 23:40:14 +02003176 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003177 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003178 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003179 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003180 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003181 // Unsized type of a named parameter should have already been checked and sanitized.
3182 ASSERT(!param.type->isUnsizedArray());
3183 }
3184 else
3185 {
3186 if (param.type->isUnsizedArray())
3187 {
3188 error(location, "function parameter array must be sized at compile time", "[]");
3189 // We don't need to size the arrays since the parameter is unnamed and hence
3190 // inaccessible.
3191 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003192 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003193 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3196 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003197 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003198 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003199 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003201 symbol->setLine(location);
3202 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003203 }
3204 return prototype;
3205}
3206
Olli Etuaho16c745a2017-01-16 17:02:27 +00003207TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3208 const TFunction &parsedFunction,
3209 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003210{
Olli Etuaho476197f2016-10-11 13:59:08 +01003211 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3212 // first declaration. Either way the instance in the symbol table is used to track whether the
3213 // function is declared multiple times.
3214 TFunction *function = static_cast<TFunction *>(
3215 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3216 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003217 {
3218 // ESSL 1.00.17 section 4.2.7.
3219 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3220 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003221 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003222 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003223
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003224 TIntermFunctionPrototype *prototype =
3225 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003226
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003227 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003228
3229 if (!symbolTable.atGlobalLevel())
3230 {
3231 // ESSL 3.00.4 section 4.2.4.
3232 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003233 }
3234
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003235 return prototype;
3236}
3237
Olli Etuaho336b1472016-10-05 16:37:55 +01003238TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003239 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003240 TIntermBlock *functionBody,
3241 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003242{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003243 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3245 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003246 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003247 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003248 }
3249
Olli Etuahof51fdd22016-10-03 10:03:40 +01003250 if (functionBody == nullptr)
3251 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003252 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003253 functionBody->setLine(location);
3254 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003255 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003256 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003257 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003258
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003260 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261}
3262
Olli Etuaho476197f2016-10-11 13:59:08 +01003263void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3264 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003265 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003266{
Olli Etuaho476197f2016-10-11 13:59:08 +01003267 ASSERT(function);
3268 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003269 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003270 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003271
3272 if (builtIn)
3273 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003274 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003275 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003276 else
Jamie Madill185fb402015-06-12 15:48:48 -04003277 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 TFunction *prevDec = static_cast<TFunction *>(
3279 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3280
3281 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3282 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3283 // occurance.
3284 if (*function != prevDec)
3285 {
3286 // Swap the parameters of the previous declaration to the parameters of the function
3287 // definition (parameter names may differ).
3288 prevDec->swapParameters(**function);
3289
3290 // The function definition will share the same symbol as any previous declaration.
3291 *function = prevDec;
3292 }
3293
3294 if ((*function)->isDefined())
3295 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003296 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003297 }
3298
3299 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003300 }
Jamie Madill185fb402015-06-12 15:48:48 -04003301
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003302 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003303 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003304 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003305
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003306 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003307 setLoopNestingLevel(0);
3308}
3309
Jamie Madillb98c3a82015-07-23 14:26:04 -04003310TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003311{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003312 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003313 // We don't know at this point whether this is a function definition or a prototype.
3314 // The definition production code will check for redefinitions.
3315 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003316 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003317 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3318 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003319 //
3320 TFunction *prevDec =
3321 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303322
Olli Etuahod80f2942017-11-06 12:44:45 +02003323 for (size_t i = 0u; i < function->getParamCount(); ++i)
3324 {
3325 auto &param = function->getParam(i);
3326 if (param.type->isStructSpecifier())
3327 {
3328 // ESSL 3.00.6 section 12.10.
3329 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003330 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003331 }
3332 }
3333
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003334 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003335 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303336 {
Martin Radevda6254b2016-12-14 17:00:36 +02003337 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303338 // Therefore overloading or redefining builtin functions is an error.
3339 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003340 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341 }
3342 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003343 {
3344 if (prevDec->getReturnType() != function->getReturnType())
3345 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003346 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003347 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003348 }
3349 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3350 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351 if (prevDec->getParam(i).type->getQualifier() !=
3352 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003353 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003354 error(location,
3355 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003356 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003357 }
3358 }
3359 }
3360
3361 //
3362 // Check for previously declared variables using the same name.
3363 //
Olli Etuahobed35d72017-12-20 16:36:26 +02003364 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003365 if (prevSym)
3366 {
3367 if (!prevSym->isFunction())
3368 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003369 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 }
3371 }
3372 else
3373 {
3374 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003375 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003376 }
3377
3378 // We're at the inner scope level of the function's arguments and body statement.
3379 // Add the function prototype to the surrounding scope instead.
3380 symbolTable.getOuterLevel()->insert(function);
3381
Olli Etuaho78d13742017-01-18 13:06:10 +00003382 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003383 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003384 {
3385 if (function->getParamCount() > 0)
3386 {
3387 error(location, "function cannot take any parameter(s)", "main");
3388 }
3389 if (function->getReturnType().getBasicType() != EbtVoid)
3390 {
3391 error(location, "main function cannot return a value",
3392 function->getReturnType().getBasicString());
3393 }
3394 }
3395
Jamie Madill185fb402015-06-12 15:48:48 -04003396 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003397 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3398 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003399 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3400 //
3401 return function;
3402}
3403
Olli Etuaho9de84a52016-06-14 17:36:01 +03003404TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3405 const TString *name,
3406 const TSourceLoc &location)
3407{
3408 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3409 {
3410 error(location, "no qualifiers allowed for function return",
3411 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003412 }
3413 if (!type.layoutQualifier.isEmpty())
3414 {
3415 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003416 }
jchen10cc2a10e2017-05-03 14:05:12 +08003417 // make sure an opaque type is not involved as well...
3418 std::string reason(getBasicString(type.getBasicType()));
3419 reason += "s can't be function return values";
3420 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003421 if (mShaderVersion < 300)
3422 {
3423 // Array return values are forbidden, but there's also no valid syntax for declaring array
3424 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003425 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003426
3427 if (type.isStructureContainingArrays())
3428 {
3429 // ESSL 1.00.17 section 6.1 Function Definitions
3430 error(location, "structures containing arrays can't be function return values",
3431 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003432 }
3433 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003434
3435 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003436 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003437}
3438
Olli Etuahocce89652017-06-19 16:04:09 +03003439TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3440{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003441 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003442 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3443 // really only interested in the mangled name of the function to look up the actual function
3444 // from the symbol table. If we just had the name string and the types of the parameters that
3445 // would be enough, but TFunction carries a lot of extra information in addition to that.
3446 // Besides function calls we do have to store constructor calls in the same data structure, for
3447 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003448 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003449}
3450
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003451TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003452{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003453 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003454 {
3455 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3456 "[]");
3457 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003458 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003459 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003460 error(publicType.getLine(), "constructor can't be a structure definition",
3461 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003462 }
3463
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003464 TType *type = new TType(publicType);
3465 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003466 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003467 error(publicType.getLine(), "cannot construct this type",
3468 getBasicString(publicType.getBasicType()));
3469 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003470 }
3471
Olli Etuaho0c371002017-12-13 17:00:25 +04003472 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003473}
3474
Olli Etuaho55bde912017-10-25 13:41:13 +03003475void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3476 const char *errorMessage,
3477 const char *token,
3478 TType *arrayType)
3479{
3480 if (arrayType->isUnsizedArray())
3481 {
3482 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003483 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003484 }
3485}
3486
3487TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003488 const TString *name,
3489 const TSourceLoc &nameLoc)
3490{
Olli Etuaho55bde912017-10-25 13:41:13 +03003491 ASSERT(type);
3492 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3493 type);
3494 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003495 {
3496 error(nameLoc, "illegal use of type 'void'", name->c_str());
3497 }
3498 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003499 TParameter param = {name, type};
3500 return param;
3501}
3502
Olli Etuaho55bde912017-10-25 13:41:13 +03003503TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3504 const TString *name,
3505 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003506{
Olli Etuaho55bde912017-10-25 13:41:13 +03003507 TType *type = new TType(publicType);
3508 return parseParameterDeclarator(type, name, nameLoc);
3509}
3510
3511TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3512 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003513 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003514 const TSourceLoc &arrayLoc,
3515 TPublicType *elementType)
3516{
3517 checkArrayElementIsNotArray(arrayLoc, *elementType);
3518 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003519 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003520 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003521}
3522
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003523bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3524 TType type,
3525 const TSourceLoc &line)
3526{
3527 if (arguments->empty())
3528 {
3529 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3530 return false;
3531 }
3532 for (TIntermNode *arg : *arguments)
3533 {
3534 TIntermTyped *element = arg->getAsTyped();
3535 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003536 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3537 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003538 {
3539 error(line, "constructing from a non-dereferenced array", "constructor");
3540 return false;
3541 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003542 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 {
3544 if (dimensionalityFromElement == 1u)
3545 {
3546 error(line, "implicitly sized array of arrays constructor argument is not an array",
3547 "constructor");
3548 }
3549 else
3550 {
3551 error(line,
3552 "implicitly sized array of arrays constructor argument dimensionality is too "
3553 "low",
3554 "constructor");
3555 }
3556 return false;
3557 }
3558 }
3559 return true;
3560}
3561
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562// This function is used to test for the correctness of the parameters passed to various constructor
3563// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564//
Olli Etuaho856c4972016-08-08 11:38:39 +03003565// 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 +00003566//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003567TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003568 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303569 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003570{
Olli Etuaho856c4972016-08-08 11:38:39 +03003571 if (type.isUnsizedArray())
3572 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003573 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003574 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003575 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003576 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003577 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003578 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3579 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003580 if (type.getOutermostArraySize() == 0u)
3581 {
3582 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3583 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003588 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 }
3590 }
3591 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003592 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003593
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003594 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003595 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003596 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003598
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003599 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003600 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003601
Olli Etuaho765924f2018-01-04 12:48:36 +02003602 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003603}
3604
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003605//
3606// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003607// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003608//
Olli Etuaho13389b62016-10-16 11:48:18 +01003609TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003610 const TTypeQualifierBuilder &typeQualifierBuilder,
3611 const TSourceLoc &nameLine,
3612 const TString &blockName,
3613 TFieldList *fieldList,
3614 const TString *instanceName,
3615 const TSourceLoc &instanceLine,
3616 TIntermTyped *arrayIndex,
3617 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003618{
Olli Etuaho856c4972016-08-08 11:38:39 +03003619 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003620
Olli Etuaho77ba4082016-12-16 12:01:18 +00003621 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003622
Jiajia Qinbc585152017-06-23 15:42:17 +08003623 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003624 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003625 error(typeQualifier.line,
3626 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3627 "3.10",
3628 getQualifierString(typeQualifier.qualifier));
3629 }
3630 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3631 {
3632 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003633 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634 }
3635
Martin Radev70866b82016-07-22 15:27:42 +03003636 if (typeQualifier.invariant)
3637 {
3638 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3639 }
3640
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 if (typeQualifier.qualifier != EvqBuffer)
3642 {
3643 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3644 }
Olli Etuaho43364892017-02-13 16:00:12 +00003645
jchen10af713a22017-04-19 09:10:56 +08003646 // add array index
3647 unsigned int arraySize = 0;
3648 if (arrayIndex != nullptr)
3649 {
3650 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3651 }
3652
3653 if (mShaderVersion < 310)
3654 {
3655 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3656 }
3657 else
3658 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003659 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3660 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003661 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003662
Andrei Volykhina5527072017-03-22 16:46:30 +03003663 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3664
Jamie Madill099c0f32013-06-20 11:55:52 -04003665 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003666 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003667 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3668 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003669
Jamie Madill099c0f32013-06-20 11:55:52 -04003670 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3671 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003672 if (typeQualifier.qualifier == EvqUniform)
3673 {
3674 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3675 }
3676 else if (typeQualifier.qualifier == EvqBuffer)
3677 {
3678 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3679 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003680 }
3681
Jamie Madill1566ef72013-06-20 11:55:54 -04003682 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3683 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003684 if (typeQualifier.qualifier == EvqUniform)
3685 {
3686 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3687 }
3688 else if (typeQualifier.qualifier == EvqBuffer)
3689 {
3690 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3691 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003692 }
3693
Olli Etuaho856c4972016-08-08 11:38:39 +03003694 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003695
Martin Radev2cc85b32016-08-05 16:22:53 +03003696 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3697
Jamie Madill98493dd2013-07-08 14:39:03 -04003698 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303699 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3700 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003701 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303702 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003703 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303704 {
jchen10cc2a10e2017-05-03 14:05:12 +08003705 std::string reason("unsupported type - ");
3706 reason += fieldType->getBasicString();
3707 reason += " types are not allowed in interface blocks";
3708 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003709 }
3710
Jamie Madill98493dd2013-07-08 14:39:03 -04003711 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003712 switch (qualifier)
3713 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003714 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003715 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003716 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003717 if (typeQualifier.qualifier == EvqBuffer)
3718 {
3719 error(field->line(), "invalid qualifier on shader storage block member",
3720 getQualifierString(qualifier));
3721 }
3722 break;
3723 case EvqBuffer:
3724 if (typeQualifier.qualifier == EvqUniform)
3725 {
3726 error(field->line(), "invalid qualifier on uniform block member",
3727 getQualifierString(qualifier));
3728 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 break;
3730 default:
3731 error(field->line(), "invalid qualifier on interface block member",
3732 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003733 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003734 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003735
Martin Radev70866b82016-07-22 15:27:42 +03003736 if (fieldType->isInvariant())
3737 {
3738 error(field->line(), "invalid qualifier on interface block member", "invariant");
3739 }
3740
Jamie Madilla5efff92013-06-06 11:56:47 -04003741 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003742 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003743 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003744 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003745
Jamie Madill98493dd2013-07-08 14:39:03 -04003746 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003748 error(field->line(), "invalid layout qualifier: cannot be used here",
3749 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003750 }
3751
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003753 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003754 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003755 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003756 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003757 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003758 warning(field->line(),
3759 "extraneous layout qualifier: only has an effect on matrix types",
3760 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003761 }
3762
Jamie Madill98493dd2013-07-08 14:39:03 -04003763 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003764
Olli Etuahoebee5b32017-11-23 12:56:32 +02003765 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3766 typeQualifier.qualifier != EvqBuffer)
3767 {
3768 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3769 checkIsNotUnsizedArray(field->line(),
3770 "array members of interface blocks must specify a size",
3771 field->name().c_str(), field->type());
3772 }
3773
Jiajia Qinbc585152017-06-23 15:42:17 +08003774 if (typeQualifier.qualifier == EvqBuffer)
3775 {
3776 // set memory qualifiers
3777 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3778 // qualified with a memory qualifier, it is as if all of its members were declared with
3779 // the same memory qualifier.
3780 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3781 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3782 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3783 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3784 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3785 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3786 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3787 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3788 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3789 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3790 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003791 }
3792
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003793 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3794 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003795 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3796 {
3797 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3798 }
3799
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003800 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3801 if (arrayIndex != nullptr)
3802 {
3803 interfaceBlockType.makeArray(arraySize);
3804 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003805
Olli Etuaho195be942017-12-04 23:40:14 +02003806 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003807 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3808 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003809 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003810 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3811 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003812
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003813 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003814 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003816 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3817 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003818 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303819 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003820
3821 // set parent pointer of the field variable
3822 fieldType->setInterfaceBlock(interfaceBlock);
3823
Olli Etuaho195be942017-12-04 23:40:14 +02003824 TVariable *fieldVariable =
3825 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3826 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003827 {
3828 fieldVariable->setQualifier(typeQualifier.qualifier);
3829 }
3830 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303831 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003832 error(field->line(), "redefinition of an interface block member name",
3833 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003834 }
3835 }
3836 }
3837 else
3838 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003839 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003840
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003841 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003842 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303843 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003844 error(instanceLine, "redefinition of an interface block instance name",
3845 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003846 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003847 }
3848
Olli Etuaho195be942017-12-04 23:40:14 +02003849 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3850 blockSymbol->setLine(typeQualifier.line);
3851 TIntermDeclaration *declaration = new TIntermDeclaration();
3852 declaration->appendDeclarator(blockSymbol);
3853 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003854
3855 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003856 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857}
3858
Olli Etuaho383b7912016-08-05 11:22:59 +03003859void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003860{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003861 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003862
3863 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003864 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303865 if (mStructNestingLevel > 1)
3866 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003867 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003868 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003869}
3870
3871void TParseContext::exitStructDeclaration()
3872{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003873 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003874}
3875
Olli Etuaho8a176262016-08-16 14:23:01 +03003876void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003877{
Jamie Madillacb4b812016-11-07 13:50:29 -05003878 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303879 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003880 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003881 }
3882
Arun Patole7e7e68d2015-05-22 12:02:25 +05303883 if (field.type()->getBasicType() != EbtStruct)
3884 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003885 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003886 }
3887
3888 // We're already inside a structure definition at this point, so add
3889 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3891 {
Jamie Madill41a49272014-03-18 16:10:13 -04003892 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003893 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3894 {
3895 // This may happen in case there are nested struct definitions. While they are also
3896 // invalid GLSL, they don't cause a syntax error.
3897 reasonStream << "Struct nesting";
3898 }
3899 else
3900 {
3901 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003902 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003903 }
3904 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003905 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003906 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003907 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003908 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003909}
3910
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003911//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003912// Parse an array index expression
3913//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3915 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303916 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3919 {
3920 if (baseExpression->getAsSymbolNode())
3921 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303922 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02003923 baseExpression->getAsSymbolNode()->getName().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924 }
3925 else
3926 {
3927 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3928 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003929
Olli Etuaho3ec75682017-07-05 17:02:55 +03003930 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003931 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003932
Jiawei Shaod8105a02017-08-08 09:54:36 +08003933 if (baseExpression->getQualifier() == EvqPerVertexIn)
3934 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003935 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003936 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3937 {
3938 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3939 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3940 }
3941 }
3942
Jamie Madill21c1e452014-12-29 11:33:41 -05003943 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3944
Olli Etuaho36b05142015-11-12 13:10:42 +02003945 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3946 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3947 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3948 // index is a constant expression.
3949 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3950 {
3951 if (baseExpression->isInterfaceBlock())
3952 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003953 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003954 switch (baseExpression->getQualifier())
3955 {
3956 case EvqPerVertexIn:
3957 break;
3958 case EvqUniform:
3959 case EvqBuffer:
3960 error(location,
3961 "array indexes for uniform block arrays and shader storage block arrays "
3962 "must be constant integral expressions",
3963 "[");
3964 break;
3965 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003966 // We can reach here only in error cases.
3967 ASSERT(mDiagnostics->numErrors() > 0);
3968 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003969 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003970 }
3971 else if (baseExpression->getQualifier() == EvqFragmentOut)
3972 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003973 error(location,
3974 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003975 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003976 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3977 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003978 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003979 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003980 }
3981
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003982 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003983 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003984 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3985 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3986 // constant fold expressions that are not constant expressions). The most compatible way to
3987 // handle this case is to report a warning instead of an error and force the index to be in
3988 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003989 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003990 int index = 0;
3991 if (indexConstantUnion->getBasicType() == EbtInt)
3992 {
3993 index = indexConstantUnion->getIConst(0);
3994 }
3995 else if (indexConstantUnion->getBasicType() == EbtUInt)
3996 {
3997 index = static_cast<int>(indexConstantUnion->getUConst(0));
3998 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003999
4000 int safeIndex = -1;
4001
Olli Etuahoebee5b32017-11-23 12:56:32 +02004002 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004003 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004004 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4005 safeIndex = 0;
4006 }
4007
4008 if (!baseExpression->getType().isUnsizedArray())
4009 {
4010 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004011 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004012 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004013 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004014 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4015 {
4016 outOfRangeError(outOfRangeIndexIsError, location,
4017 "array index for gl_FragData must be zero when "
4018 "GL_EXT_draw_buffers is disabled",
4019 "[]");
4020 safeIndex = 0;
4021 }
4022 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004023 }
4024 // Only do generic out-of-range check if similar error hasn't already been reported.
4025 if (safeIndex < 0)
4026 {
4027 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004028 {
4029 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4030 baseExpression->getOutermostArraySize(),
4031 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004032 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004033 else if (baseExpression->isMatrix())
4034 {
4035 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4036 baseExpression->getType().getCols(),
4037 "matrix field selection out of range");
4038 }
4039 else
4040 {
4041 ASSERT(baseExpression->isVector());
4042 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4043 baseExpression->getType().getNominalSize(),
4044 "vector field selection out of range");
4045 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004046 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004047
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048 ASSERT(safeIndex >= 0);
4049 // Data of constant unions can't be changed, because it may be shared with other
4050 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4051 // sanitized object.
4052 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4053 {
4054 TConstantUnion *safeConstantUnion = new TConstantUnion();
4055 safeConstantUnion->setIConst(safeIndex);
4056 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4057 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4058 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004059
Olli Etuahoebee5b32017-11-23 12:56:32 +02004060 TIntermBinary *node =
4061 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4062 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004063 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004064 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004065 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004066
4067 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4068 node->setLine(location);
4069 // Indirect indexing can never be constant folded.
4070 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004071}
4072
Olli Etuahoebee5b32017-11-23 12:56:32 +02004073int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4074 const TSourceLoc &location,
4075 int index,
4076 int arraySize,
4077 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004078{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004079 // Should not reach here with an unsized / runtime-sized array.
4080 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004081 // A negative index should already have been checked.
4082 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004083 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004084 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004085 std::stringstream reasonStream;
4086 reasonStream << reason << " '" << index << "'";
4087 std::string token = reasonStream.str();
4088 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004089 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004090 }
4091 return index;
4092}
4093
Jamie Madillb98c3a82015-07-23 14:26:04 -04004094TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4095 const TSourceLoc &dotLocation,
4096 const TString &fieldString,
4097 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004099 if (baseExpression->isArray())
4100 {
4101 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004102 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004103 }
4104
4105 if (baseExpression->isVector())
4106 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004107 TVector<int> fieldOffsets;
4108 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4109 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004111 fieldOffsets.resize(1);
4112 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004114 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4115 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116
Olli Etuaho765924f2018-01-04 12:48:36 +02004117 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 else if (baseExpression->getBasicType() == EbtStruct)
4120 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304121 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004122 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004123 {
4124 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004125 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004126 }
4127 else
4128 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004129 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004131 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004132 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004133 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 {
4135 fieldFound = true;
4136 break;
4137 }
4138 }
4139 if (fieldFound)
4140 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004141 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004142 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004143 TIntermBinary *node =
4144 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4145 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004146 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004147 }
4148 else
4149 {
4150 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004151 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004152 }
4153 }
4154 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004155 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004156 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304157 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004158 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004159 {
4160 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004161 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004162 }
4163 else
4164 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004165 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004166 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004167 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004168 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004169 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004170 {
4171 fieldFound = true;
4172 break;
4173 }
4174 }
4175 if (fieldFound)
4176 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004177 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004178 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004179 TIntermBinary *node =
4180 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4181 node->setLine(dotLocation);
4182 // Indexing interface blocks can never be constant folded.
4183 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004184 }
4185 else
4186 {
4187 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004188 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004189 }
4190 }
4191 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004192 else
4193 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004194 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004195 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004196 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304197 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004198 }
4199 else
4200 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304201 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004202 " field selection requires structure, vector, or interface block on left hand "
4203 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304204 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004205 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004206 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004207 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004208}
4209
Jamie Madillb98c3a82015-07-23 14:26:04 -04004210TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4211 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004212{
Jamie Madill2f294c92017-11-20 14:47:26 -05004213 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004214
4215 if (qualifierType == "shared")
4216 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004217 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004218 {
4219 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4220 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004221 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004222 }
4223 else if (qualifierType == "packed")
4224 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004225 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004226 {
4227 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4228 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004229 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004230 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004231 else if (qualifierType == "std430")
4232 {
4233 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4234 qualifier.blockStorage = EbsStd430;
4235 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004236 else if (qualifierType == "std140")
4237 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004238 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004239 }
4240 else if (qualifierType == "row_major")
4241 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004242 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004243 }
4244 else if (qualifierType == "column_major")
4245 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004246 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004247 }
4248 else if (qualifierType == "location")
4249 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004250 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4251 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004252 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004253 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004254 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004255 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4256 {
4257 qualifier.yuv = true;
4258 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004259 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004260 else if (qualifierType == "rgba32f")
4261 {
4262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4263 qualifier.imageInternalFormat = EiifRGBA32F;
4264 }
4265 else if (qualifierType == "rgba16f")
4266 {
4267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4268 qualifier.imageInternalFormat = EiifRGBA16F;
4269 }
4270 else if (qualifierType == "r32f")
4271 {
4272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4273 qualifier.imageInternalFormat = EiifR32F;
4274 }
4275 else if (qualifierType == "rgba8")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifRGBA8;
4279 }
4280 else if (qualifierType == "rgba8_snorm")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4284 }
4285 else if (qualifierType == "rgba32i")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifRGBA32I;
4289 }
4290 else if (qualifierType == "rgba16i")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.imageInternalFormat = EiifRGBA16I;
4294 }
4295 else if (qualifierType == "rgba8i")
4296 {
4297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4298 qualifier.imageInternalFormat = EiifRGBA8I;
4299 }
4300 else if (qualifierType == "r32i")
4301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.imageInternalFormat = EiifR32I;
4304 }
4305 else if (qualifierType == "rgba32ui")
4306 {
4307 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4308 qualifier.imageInternalFormat = EiifRGBA32UI;
4309 }
4310 else if (qualifierType == "rgba16ui")
4311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.imageInternalFormat = EiifRGBA16UI;
4314 }
4315 else if (qualifierType == "rgba8ui")
4316 {
4317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4318 qualifier.imageInternalFormat = EiifRGBA8UI;
4319 }
4320 else if (qualifierType == "r32ui")
4321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.imageInternalFormat = EiifR32UI;
4324 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004325 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4326 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.primitiveType = EptPoints;
4330 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004331 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4332 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004333 {
4334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4335 qualifier.primitiveType = EptLines;
4336 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004337 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4338 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.primitiveType = EptLinesAdjacency;
4342 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004343 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4344 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.primitiveType = EptTriangles;
4348 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004349 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4350 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004351 {
4352 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4353 qualifier.primitiveType = EptTrianglesAdjacency;
4354 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004355 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4356 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004357 {
4358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4359 qualifier.primitiveType = EptLineStrip;
4360 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004361 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4362 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004363 {
4364 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4365 qualifier.primitiveType = EptTriangleStrip;
4366 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004367
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004368 else
4369 {
4370 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004371 }
4372
Jamie Madilla5efff92013-06-06 11:56:47 -04004373 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004374}
4375
Martin Radev802abe02016-08-04 17:48:32 +03004376void TParseContext::parseLocalSize(const TString &qualifierType,
4377 const TSourceLoc &qualifierTypeLine,
4378 int intValue,
4379 const TSourceLoc &intValueLine,
4380 const std::string &intValueString,
4381 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004382 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004383{
Olli Etuaho856c4972016-08-08 11:38:39 +03004384 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004385 if (intValue < 1)
4386 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004387 std::stringstream reasonStream;
4388 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4389 std::string reason = reasonStream.str();
4390 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004391 }
4392 (*localSize)[index] = intValue;
4393}
4394
Olli Etuaho09b04a22016-12-15 13:30:26 +00004395void TParseContext::parseNumViews(int intValue,
4396 const TSourceLoc &intValueLine,
4397 const std::string &intValueString,
4398 int *numViews)
4399{
4400 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4401 // specification.
4402 if (intValue < 1)
4403 {
4404 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4405 }
4406 *numViews = intValue;
4407}
4408
Shaob5cc1192017-07-06 10:47:20 +08004409void TParseContext::parseInvocations(int intValue,
4410 const TSourceLoc &intValueLine,
4411 const std::string &intValueString,
4412 int *numInvocations)
4413{
4414 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4415 // it doesn't make sense to accept invocations <= 0.
4416 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4417 {
4418 error(intValueLine,
4419 "out of range: invocations must be in the range of [1, "
4420 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4421 intValueString.c_str());
4422 }
4423 else
4424 {
4425 *numInvocations = intValue;
4426 }
4427}
4428
4429void TParseContext::parseMaxVertices(int intValue,
4430 const TSourceLoc &intValueLine,
4431 const std::string &intValueString,
4432 int *maxVertices)
4433{
4434 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4435 // it doesn't make sense to accept max_vertices < 0.
4436 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4437 {
4438 error(
4439 intValueLine,
4440 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4441 intValueString.c_str());
4442 }
4443 else
4444 {
4445 *maxVertices = intValue;
4446 }
4447}
4448
Jamie Madillb98c3a82015-07-23 14:26:04 -04004449TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4450 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004451 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304452 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453{
Jamie Madill2f294c92017-11-20 14:47:26 -05004454 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004455
Martin Radev802abe02016-08-04 17:48:32 +03004456 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004457
Martin Radev802abe02016-08-04 17:48:32 +03004458 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004459 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004460 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004461 if (intValue < 0)
4462 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004463 error(intValueLine, "out of range: location must be non-negative",
4464 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004465 }
4466 else
4467 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004468 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004469 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004471 }
Olli Etuaho43364892017-02-13 16:00:12 +00004472 else if (qualifierType == "binding")
4473 {
4474 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4475 if (intValue < 0)
4476 {
4477 error(intValueLine, "out of range: binding must be non-negative",
4478 intValueString.c_str());
4479 }
4480 else
4481 {
4482 qualifier.binding = intValue;
4483 }
4484 }
jchen104cdac9e2017-05-08 11:01:20 +08004485 else if (qualifierType == "offset")
4486 {
4487 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4488 if (intValue < 0)
4489 {
4490 error(intValueLine, "out of range: offset must be non-negative",
4491 intValueString.c_str());
4492 }
4493 else
4494 {
4495 qualifier.offset = intValue;
4496 }
4497 }
Martin Radev802abe02016-08-04 17:48:32 +03004498 else if (qualifierType == "local_size_x")
4499 {
4500 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4501 &qualifier.localSize);
4502 }
4503 else if (qualifierType == "local_size_y")
4504 {
4505 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4506 &qualifier.localSize);
4507 }
4508 else if (qualifierType == "local_size_z")
4509 {
4510 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4511 &qualifier.localSize);
4512 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004513 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004514 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004515 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4516 {
4517 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4518 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004519 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004520 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4521 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004522 {
4523 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4524 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004525 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4526 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004527 {
4528 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4529 }
4530
Martin Radev802abe02016-08-04 17:48:32 +03004531 else
4532 {
4533 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004534 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004535
Jamie Madilla5efff92013-06-06 11:56:47 -04004536 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004537}
4538
Olli Etuaho613b9592016-09-05 12:05:53 +03004539TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4540{
4541 return new TTypeQualifierBuilder(
4542 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4543 mShaderVersion);
4544}
4545
Olli Etuahocce89652017-06-19 16:04:09 +03004546TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4547 const TSourceLoc &loc)
4548{
4549 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4550 return new TStorageQualifierWrapper(qualifier, loc);
4551}
4552
4553TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4554{
4555 if (getShaderType() == GL_VERTEX_SHADER)
4556 {
4557 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4558 }
4559 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4560}
4561
4562TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4563{
4564 if (declaringFunction())
4565 {
4566 return new TStorageQualifierWrapper(EvqIn, loc);
4567 }
Shaob5cc1192017-07-06 10:47:20 +08004568
4569 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004570 {
Shaob5cc1192017-07-06 10:47:20 +08004571 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004572 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004573 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004574 {
4575 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4576 }
4577 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004578 }
Shaob5cc1192017-07-06 10:47:20 +08004579 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004580 {
Shaob5cc1192017-07-06 10:47:20 +08004581 if (mShaderVersion < 300)
4582 {
4583 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4584 }
4585 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004586 }
Shaob5cc1192017-07-06 10:47:20 +08004587 case GL_COMPUTE_SHADER:
4588 {
4589 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4590 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004591 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004592 {
4593 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4594 }
4595 default:
4596 {
4597 UNREACHABLE();
4598 return new TStorageQualifierWrapper(EvqLast, loc);
4599 }
Olli Etuahocce89652017-06-19 16:04:09 +03004600 }
Olli Etuahocce89652017-06-19 16:04:09 +03004601}
4602
4603TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4604{
4605 if (declaringFunction())
4606 {
4607 return new TStorageQualifierWrapper(EvqOut, loc);
4608 }
Shaob5cc1192017-07-06 10:47:20 +08004609 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004610 {
Shaob5cc1192017-07-06 10:47:20 +08004611 case GL_VERTEX_SHADER:
4612 {
4613 if (mShaderVersion < 300)
4614 {
4615 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4616 }
4617 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4618 }
4619 case GL_FRAGMENT_SHADER:
4620 {
4621 if (mShaderVersion < 300)
4622 {
4623 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4624 }
4625 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4626 }
4627 case GL_COMPUTE_SHADER:
4628 {
4629 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4630 return new TStorageQualifierWrapper(EvqLast, loc);
4631 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004632 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004633 {
4634 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4635 }
4636 default:
4637 {
4638 UNREACHABLE();
4639 return new TStorageQualifierWrapper(EvqLast, loc);
4640 }
Olli Etuahocce89652017-06-19 16:04:09 +03004641 }
Olli Etuahocce89652017-06-19 16:04:09 +03004642}
4643
4644TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4645{
4646 if (!declaringFunction())
4647 {
4648 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4649 }
4650 return new TStorageQualifierWrapper(EvqInOut, loc);
4651}
4652
Jamie Madillb98c3a82015-07-23 14:26:04 -04004653TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004654 TLayoutQualifier rightQualifier,
4655 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004656{
Martin Radevc28888b2016-07-22 15:27:42 +03004657 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004658 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004659}
4660
Olli Etuahod5f44c92017-11-29 17:15:40 +02004661TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004662{
4663 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004664 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004665}
4666
Olli Etuahod5f44c92017-11-29 17:15:40 +02004667TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4668 const TSourceLoc &loc,
4669 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004670{
4671 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004673}
4674
Olli Etuaho722bfb52017-10-26 17:00:11 +03004675void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4676 const TFieldList::const_iterator end,
4677 const TString &name,
4678 const TSourceLoc &location)
4679{
4680 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4681 {
4682 if ((*fieldIter)->name() == name)
4683 {
4684 error(location, "duplicate field name in structure", name.c_str());
4685 }
4686 }
4687}
4688
4689TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4690{
4691 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4692 ++fieldIter)
4693 {
4694 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4695 location);
4696 }
4697 return fields;
4698}
4699
Olli Etuaho4de340a2016-12-16 09:32:03 +00004700TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4701 const TFieldList *newlyAddedFields,
4702 const TSourceLoc &location)
4703{
4704 for (TField *field : *newlyAddedFields)
4705 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004706 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4707 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004708 processedFields->push_back(field);
4709 }
4710 return processedFields;
4711}
4712
Martin Radev70866b82016-07-22 15:27:42 +03004713TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4714 const TTypeQualifierBuilder &typeQualifierBuilder,
4715 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004716 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004717{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004718 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004719
Martin Radev70866b82016-07-22 15:27:42 +03004720 typeSpecifier->qualifier = typeQualifier.qualifier;
4721 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004722 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004723 typeSpecifier->invariant = typeQualifier.invariant;
4724 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304725 {
Martin Radev70866b82016-07-22 15:27:42 +03004726 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004727 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004728 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004729}
4730
Jamie Madillb98c3a82015-07-23 14:26:04 -04004731TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004732 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004733{
Martin Radev4a9cd802016-09-01 16:51:51 +03004734 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4735 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004736
Olli Etuahod5f44c92017-11-29 17:15:40 +02004737 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004738 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004739
Martin Radev4a9cd802016-09-01 16:51:51 +03004740 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004741
Olli Etuahod5f44c92017-11-29 17:15:40 +02004742 TFieldList *fieldList = new TFieldList();
4743
4744 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304745 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004746 TType *type = new TType(typeSpecifier);
4747 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304748 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004749 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004750 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004751 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004752 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004753
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 TField *field = new TField(type, declarator->name(), declarator->line());
4755 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4756 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757 }
4758
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004760}
4761
Martin Radev4a9cd802016-09-01 16:51:51 +03004762TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4763 const TSourceLoc &nameLine,
4764 const TString *structName,
4765 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004766{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004767 SymbolType structSymbolType = SymbolType::UserDefined;
4768 if (structName == nullptr)
4769 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004770 structSymbolType = SymbolType::Empty;
4771 }
4772 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004773
Jamie Madill9b820842015-02-12 10:40:10 -05004774 // Store a bool in the struct if we're at global scope, to allow us to
4775 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004776 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004777
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004778 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004779 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004780 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004781 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304782 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004783 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 }
4785 }
4786
4787 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004788 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004790 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004791 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004792 switch (qualifier)
4793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004794 case EvqGlobal:
4795 case EvqTemporary:
4796 break;
4797 default:
4798 error(field.line(), "invalid qualifier on struct member",
4799 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004800 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004801 }
Martin Radev70866b82016-07-22 15:27:42 +03004802 if (field.type()->isInvariant())
4803 {
4804 error(field.line(), "invalid qualifier on struct member", "invariant");
4805 }
jchen104cdac9e2017-05-08 11:01:20 +08004806 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4807 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004808 {
4809 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4810 }
4811
Olli Etuahoebee5b32017-11-23 12:56:32 +02004812 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4813 field.name().c_str(), field.type());
4814
Olli Etuaho43364892017-02-13 16:00:12 +00004815 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4816
4817 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004818
4819 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004820 }
4821
Martin Radev4a9cd802016-09-01 16:51:51 +03004822 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004823 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004824 exitStructDeclaration();
4825
Martin Radev4a9cd802016-09-01 16:51:51 +03004826 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004827}
4828
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004830 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004832{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004833 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004834 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004835 init->isVector())
4836 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004837 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4838 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004839 return nullptr;
4840 }
4841
Olli Etuaho923ecef2017-10-11 12:01:38 +03004842 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004843 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004844 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004845 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004846 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004847 }
4848
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004849 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4850 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004851 return node;
4852}
4853
4854TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4855{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004856 if (mSwitchNestingLevel == 0)
4857 {
4858 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004859 return nullptr;
4860 }
4861 if (condition == nullptr)
4862 {
4863 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
4866 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004867 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 {
4869 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004870 }
4871 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004872 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4873 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4874 // fold in case labels.
4875 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004876 {
4877 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004878 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004879 TIntermCase *node = new TIntermCase(condition);
4880 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004881 return node;
4882}
4883
4884TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4885{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004886 if (mSwitchNestingLevel == 0)
4887 {
4888 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004889 return nullptr;
4890 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004891 TIntermCase *node = new TIntermCase(nullptr);
4892 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004893 return node;
4894}
4895
Jamie Madillb98c3a82015-07-23 14:26:04 -04004896TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4897 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004898 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004899{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004900 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004901
4902 switch (op)
4903 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004904 case EOpLogicalNot:
4905 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4906 child->isVector())
4907 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004908 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004909 return nullptr;
4910 }
4911 break;
4912 case EOpBitwiseNot:
4913 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4914 child->isMatrix() || child->isArray())
4915 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004916 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004917 return nullptr;
4918 }
4919 break;
4920 case EOpPostIncrement:
4921 case EOpPreIncrement:
4922 case EOpPostDecrement:
4923 case EOpPreDecrement:
4924 case EOpNegative:
4925 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004926 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4927 child->getBasicType() == EbtBool || child->isArray() ||
4928 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004930 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 return nullptr;
4932 }
4933 // Operators for built-ins are already type checked against their prototype.
4934 default:
4935 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004936 }
4937
Jiajia Qinbc585152017-06-23 15:42:17 +08004938 if (child->getMemoryQualifier().writeonly)
4939 {
4940 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4941 return nullptr;
4942 }
4943
Olli Etuahof119a262016-08-19 15:54:22 +03004944 TIntermUnary *node = new TIntermUnary(op, child);
4945 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004946
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004947 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004948}
4949
Olli Etuaho09b22472015-02-11 11:47:26 +02004950TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4951{
Olli Etuahocce89652017-06-19 16:04:09 +03004952 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004953 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004954 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004955 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 return child;
4957 }
4958 return node;
4959}
4960
Jamie Madillb98c3a82015-07-23 14:26:04 -04004961TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4962 TIntermTyped *child,
4963 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004964{
Olli Etuaho856c4972016-08-08 11:38:39 +03004965 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004966 return addUnaryMath(op, child, loc);
4967}
4968
Olli Etuaho765924f2018-01-04 12:48:36 +02004969TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4970{
4971 // If we can, we should return the folded version of the expression for subsequent parsing. This
4972 // enables folding the containing expression during parsing as well, instead of the separate
4973 // FoldExpressions() step where folding nested expressions requires multiple full AST
4974 // traversals.
4975
4976 // Even if folding fails the fold() functions return some node representing the expression,
4977 // typically the original node. So "folded" can be assumed to be non-null.
4978 TIntermTyped *folded = expression->fold(mDiagnostics);
4979 ASSERT(folded != nullptr);
4980 if (folded->getQualifier() == expression->getQualifier())
4981 {
4982 // We need this expression to have the correct qualifier when validating the consuming
4983 // expression. So we can only return the folded node from here in case it has the same
4984 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4985 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4986 // 1. (true ? 1.0 : non_constant)
4987 // 2. (non_constant, 1.0)
4988 return folded;
4989 }
4990 return expression;
4991}
4992
Jamie Madillb98c3a82015-07-23 14:26:04 -04004993bool TParseContext::binaryOpCommonCheck(TOperator op,
4994 TIntermTyped *left,
4995 TIntermTyped *right,
4996 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004997{
jchen10b4cf5652017-05-05 18:51:17 +08004998 // Check opaque types are not allowed to be operands in expressions other than array indexing
4999 // and structure member selection.
5000 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
5001 {
5002 switch (op)
5003 {
5004 case EOpIndexDirect:
5005 case EOpIndexIndirect:
5006 break;
5007 case EOpIndexDirectStruct:
5008 UNREACHABLE();
5009
5010 default:
5011 error(loc, "Invalid operation for variables with an opaque type",
5012 GetOperatorString(op));
5013 return false;
5014 }
5015 }
jchen10cc2a10e2017-05-03 14:05:12 +08005016
Jiajia Qinbc585152017-06-23 15:42:17 +08005017 if (right->getMemoryQualifier().writeonly)
5018 {
5019 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5020 return false;
5021 }
5022
5023 if (left->getMemoryQualifier().writeonly)
5024 {
5025 switch (op)
5026 {
5027 case EOpAssign:
5028 case EOpInitialize:
5029 case EOpIndexDirect:
5030 case EOpIndexIndirect:
5031 case EOpIndexDirectStruct:
5032 case EOpIndexDirectInterfaceBlock:
5033 break;
5034 default:
5035 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5036 return false;
5037 }
5038 }
5039
Olli Etuaho244be012016-08-18 15:26:02 +03005040 if (left->getType().getStruct() || right->getType().getStruct())
5041 {
5042 switch (op)
5043 {
5044 case EOpIndexDirectStruct:
5045 ASSERT(left->getType().getStruct());
5046 break;
5047 case EOpEqual:
5048 case EOpNotEqual:
5049 case EOpAssign:
5050 case EOpInitialize:
5051 if (left->getType() != right->getType())
5052 {
5053 return false;
5054 }
5055 break;
5056 default:
5057 error(loc, "Invalid operation for structs", GetOperatorString(op));
5058 return false;
5059 }
5060 }
5061
Olli Etuaho94050052017-05-08 14:17:44 +03005062 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5063 {
5064 switch (op)
5065 {
5066 case EOpIndexDirectInterfaceBlock:
5067 ASSERT(left->getType().getInterfaceBlock());
5068 break;
5069 default:
5070 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5071 return false;
5072 }
5073 }
5074
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005075 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005076 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005077 error(loc, "array / non-array mismatch", GetOperatorString(op));
5078 return false;
5079 }
5080
5081 if (left->isArray())
5082 {
5083 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005084 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005085 {
5086 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5087 return false;
5088 }
5089
Olli Etuahoe79904c2015-03-18 16:56:42 +02005090 switch (op)
5091 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005092 case EOpEqual:
5093 case EOpNotEqual:
5094 case EOpAssign:
5095 case EOpInitialize:
5096 break;
5097 default:
5098 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5099 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005100 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005101 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005102 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005103 {
5104 error(loc, "array size mismatch", GetOperatorString(op));
5105 return false;
5106 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005107 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005108
5109 // Check ops which require integer / ivec parameters
5110 bool isBitShift = false;
5111 switch (op)
5112 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005113 case EOpBitShiftLeft:
5114 case EOpBitShiftRight:
5115 case EOpBitShiftLeftAssign:
5116 case EOpBitShiftRightAssign:
5117 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5118 // check that the basic type is an integer type.
5119 isBitShift = true;
5120 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5121 {
5122 return false;
5123 }
5124 break;
5125 case EOpBitwiseAnd:
5126 case EOpBitwiseXor:
5127 case EOpBitwiseOr:
5128 case EOpBitwiseAndAssign:
5129 case EOpBitwiseXorAssign:
5130 case EOpBitwiseOrAssign:
5131 // It is enough to check the type of only one operand, since later it
5132 // is checked that the operand types match.
5133 if (!IsInteger(left->getBasicType()))
5134 {
5135 return false;
5136 }
5137 break;
5138 default:
5139 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005140 }
5141
5142 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5143 // So the basic type should usually match.
5144 if (!isBitShift && left->getBasicType() != right->getBasicType())
5145 {
5146 return false;
5147 }
5148
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005149 // Check that:
5150 // 1. Type sizes match exactly on ops that require that.
5151 // 2. Restrictions for structs that contain arrays or samplers are respected.
5152 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005153 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005154 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005155 case EOpAssign:
5156 case EOpInitialize:
5157 case EOpEqual:
5158 case EOpNotEqual:
5159 // ESSL 1.00 sections 5.7, 5.8, 5.9
5160 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5161 {
5162 error(loc, "undefined operation for structs containing arrays",
5163 GetOperatorString(op));
5164 return false;
5165 }
5166 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5167 // we interpret the spec so that this extends to structs containing samplers,
5168 // similarly to ESSL 1.00 spec.
5169 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5170 left->getType().isStructureContainingSamplers())
5171 {
5172 error(loc, "undefined operation for structs containing samplers",
5173 GetOperatorString(op));
5174 return false;
5175 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005176
Olli Etuahoe1805592017-01-02 16:41:20 +00005177 if ((left->getNominalSize() != right->getNominalSize()) ||
5178 (left->getSecondarySize() != right->getSecondarySize()))
5179 {
5180 error(loc, "dimension mismatch", GetOperatorString(op));
5181 return false;
5182 }
5183 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005184 case EOpLessThan:
5185 case EOpGreaterThan:
5186 case EOpLessThanEqual:
5187 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005188 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005189 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005190 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005191 return false;
5192 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005193 break;
5194 case EOpAdd:
5195 case EOpSub:
5196 case EOpDiv:
5197 case EOpIMod:
5198 case EOpBitShiftLeft:
5199 case EOpBitShiftRight:
5200 case EOpBitwiseAnd:
5201 case EOpBitwiseXor:
5202 case EOpBitwiseOr:
5203 case EOpAddAssign:
5204 case EOpSubAssign:
5205 case EOpDivAssign:
5206 case EOpIModAssign:
5207 case EOpBitShiftLeftAssign:
5208 case EOpBitShiftRightAssign:
5209 case EOpBitwiseAndAssign:
5210 case EOpBitwiseXorAssign:
5211 case EOpBitwiseOrAssign:
5212 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5213 {
5214 return false;
5215 }
5216
5217 // Are the sizes compatible?
5218 if (left->getNominalSize() != right->getNominalSize() ||
5219 left->getSecondarySize() != right->getSecondarySize())
5220 {
5221 // If the nominal sizes of operands do not match:
5222 // One of them must be a scalar.
5223 if (!left->isScalar() && !right->isScalar())
5224 return false;
5225
5226 // In the case of compound assignment other than multiply-assign,
5227 // the right side needs to be a scalar. Otherwise a vector/matrix
5228 // would be assigned to a scalar. A scalar can't be shifted by a
5229 // vector either.
5230 if (!right->isScalar() &&
5231 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5232 return false;
5233 }
5234 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 default:
5236 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005237 }
5238
Olli Etuahod6b14282015-03-17 14:31:35 +02005239 return true;
5240}
5241
Olli Etuaho1dded802016-08-18 18:13:13 +03005242bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5243 const TType &left,
5244 const TType &right)
5245{
5246 switch (op)
5247 {
5248 case EOpMul:
5249 case EOpMulAssign:
5250 return left.getNominalSize() == right.getNominalSize() &&
5251 left.getSecondarySize() == right.getSecondarySize();
5252 case EOpVectorTimesScalar:
5253 return true;
5254 case EOpVectorTimesScalarAssign:
5255 ASSERT(!left.isMatrix() && !right.isMatrix());
5256 return left.isVector() && !right.isVector();
5257 case EOpVectorTimesMatrix:
5258 return left.getNominalSize() == right.getRows();
5259 case EOpVectorTimesMatrixAssign:
5260 ASSERT(!left.isMatrix() && right.isMatrix());
5261 return left.isVector() && left.getNominalSize() == right.getRows() &&
5262 left.getNominalSize() == right.getCols();
5263 case EOpMatrixTimesVector:
5264 return left.getCols() == right.getNominalSize();
5265 case EOpMatrixTimesScalar:
5266 return true;
5267 case EOpMatrixTimesScalarAssign:
5268 ASSERT(left.isMatrix() && !right.isMatrix());
5269 return !right.isVector();
5270 case EOpMatrixTimesMatrix:
5271 return left.getCols() == right.getRows();
5272 case EOpMatrixTimesMatrixAssign:
5273 ASSERT(left.isMatrix() && right.isMatrix());
5274 // We need to check two things:
5275 // 1. The matrix multiplication step is valid.
5276 // 2. The result will have the same number of columns as the lvalue.
5277 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5278
5279 default:
5280 UNREACHABLE();
5281 return false;
5282 }
5283}
5284
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5286 TIntermTyped *left,
5287 TIntermTyped *right,
5288 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005289{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005290 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005291 return nullptr;
5292
Olli Etuahofc1806e2015-03-17 13:03:11 +02005293 switch (op)
5294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 case EOpEqual:
5296 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005297 case EOpLessThan:
5298 case EOpGreaterThan:
5299 case EOpLessThanEqual:
5300 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005301 break;
5302 case EOpLogicalOr:
5303 case EOpLogicalXor:
5304 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005305 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5306 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005307 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005308 {
5309 return nullptr;
5310 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005311 // Basic types matching should have been already checked.
5312 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 break;
5314 case EOpAdd:
5315 case EOpSub:
5316 case EOpDiv:
5317 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005318 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5319 !right->getType().getStruct());
5320 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005321 {
5322 return nullptr;
5323 }
5324 break;
5325 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005326 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5327 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005328 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005329 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005330 {
5331 return nullptr;
5332 }
5333 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334 default:
5335 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336 }
5337
Olli Etuaho1dded802016-08-18 18:13:13 +03005338 if (op == EOpMul)
5339 {
5340 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5341 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5342 {
5343 return nullptr;
5344 }
5345 }
5346
Olli Etuaho3fdec912016-08-18 15:08:06 +03005347 TIntermBinary *node = new TIntermBinary(op, left, right);
5348 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005349 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005350}
5351
Jamie Madillb98c3a82015-07-23 14:26:04 -04005352TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5353 TIntermTyped *left,
5354 TIntermTyped *right,
5355 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005356{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005357 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005358 if (node == 0)
5359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005360 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5361 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005362 return left;
5363 }
5364 return node;
5365}
5366
Jamie Madillb98c3a82015-07-23 14:26:04 -04005367TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5368 TIntermTyped *left,
5369 TIntermTyped *right,
5370 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005371{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005372 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005373 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005375 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5376 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005377 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005378 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005379 }
5380 return node;
5381}
5382
Olli Etuaho13389b62016-10-16 11:48:18 +01005383TIntermBinary *TParseContext::createAssign(TOperator op,
5384 TIntermTyped *left,
5385 TIntermTyped *right,
5386 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005387{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005388 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005389 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005390 if (op == EOpMulAssign)
5391 {
5392 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5393 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5394 {
5395 return nullptr;
5396 }
5397 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005398 TIntermBinary *node = new TIntermBinary(op, left, right);
5399 node->setLine(loc);
5400
Olli Etuaho3fdec912016-08-18 15:08:06 +03005401 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005402 }
5403 return nullptr;
5404}
5405
Jamie Madillb98c3a82015-07-23 14:26:04 -04005406TIntermTyped *TParseContext::addAssign(TOperator op,
5407 TIntermTyped *left,
5408 TIntermTyped *right,
5409 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005410{
Olli Etuahocce89652017-06-19 16:04:09 +03005411 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005412 TIntermTyped *node = createAssign(op, left, right, loc);
5413 if (node == nullptr)
5414 {
5415 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005416 return left;
5417 }
5418 return node;
5419}
5420
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005421TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5422 TIntermTyped *right,
5423 const TSourceLoc &loc)
5424{
Corentin Wallez0d959252016-07-12 17:26:32 -04005425 // WebGL2 section 5.26, the following results in an error:
5426 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005427 if (mShaderSpec == SH_WEBGL2_SPEC &&
5428 (left->isArray() || left->getBasicType() == EbtVoid ||
5429 left->getType().isStructureContainingArrays() || right->isArray() ||
5430 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005431 {
5432 error(loc,
5433 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5434 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005435 }
5436
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005437 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5438 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5439 commaNode->getTypePointer()->setQualifier(resultQualifier);
Olli Etuaho765924f2018-01-04 12:48:36 +02005440
5441 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005442}
5443
Olli Etuaho49300862015-02-20 14:54:49 +02005444TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5445{
5446 switch (op)
5447 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005448 case EOpContinue:
5449 if (mLoopNestingLevel <= 0)
5450 {
5451 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 }
5453 break;
5454 case EOpBreak:
5455 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5456 {
5457 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005458 }
5459 break;
5460 case EOpReturn:
5461 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5462 {
5463 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005464 }
5465 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005466 case EOpKill:
5467 if (mShaderType != GL_FRAGMENT_SHADER)
5468 {
5469 error(loc, "discard supported in fragment shaders only", "discard");
5470 }
5471 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005472 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005473 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005474 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005475 }
Olli Etuahocce89652017-06-19 16:04:09 +03005476 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005477}
5478
Jamie Madillb98c3a82015-07-23 14:26:04 -04005479TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005480 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005481 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005482{
Olli Etuahocce89652017-06-19 16:04:09 +03005483 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005484 {
Olli Etuahocce89652017-06-19 16:04:09 +03005485 ASSERT(op == EOpReturn);
5486 mFunctionReturnsValue = true;
5487 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5488 {
5489 error(loc, "void function cannot return a value", "return");
5490 }
5491 else if (*mCurrentFunctionType != expression->getType())
5492 {
5493 error(loc, "function return is not matching type:", "return");
5494 }
Olli Etuaho49300862015-02-20 14:54:49 +02005495 }
Olli Etuahocce89652017-06-19 16:04:09 +03005496 TIntermBranch *node = new TIntermBranch(op, expression);
5497 node->setLine(loc);
5498 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005499}
5500
Martin Radev84aa2dc2017-09-11 15:51:02 +03005501void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5502{
5503 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005504 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005505 bool isTextureGather = (name == "textureGather");
5506 bool isTextureGatherOffset = (name == "textureGatherOffset");
5507 if (isTextureGather || isTextureGatherOffset)
5508 {
5509 TIntermNode *componentNode = nullptr;
5510 TIntermSequence *arguments = functionCall->getSequence();
5511 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5512 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5513 ASSERT(sampler != nullptr);
5514 switch (sampler->getBasicType())
5515 {
5516 case EbtSampler2D:
5517 case EbtISampler2D:
5518 case EbtUSampler2D:
5519 case EbtSampler2DArray:
5520 case EbtISampler2DArray:
5521 case EbtUSampler2DArray:
5522 if ((isTextureGather && arguments->size() == 3u) ||
5523 (isTextureGatherOffset && arguments->size() == 4u))
5524 {
5525 componentNode = arguments->back();
5526 }
5527 break;
5528 case EbtSamplerCube:
5529 case EbtISamplerCube:
5530 case EbtUSamplerCube:
5531 ASSERT(!isTextureGatherOffset);
5532 if (arguments->size() == 3u)
5533 {
5534 componentNode = arguments->back();
5535 }
5536 break;
5537 case EbtSampler2DShadow:
5538 case EbtSampler2DArrayShadow:
5539 case EbtSamplerCubeShadow:
5540 break;
5541 default:
5542 UNREACHABLE();
5543 break;
5544 }
5545 if (componentNode)
5546 {
5547 const TIntermConstantUnion *componentConstantUnion =
5548 componentNode->getAsConstantUnion();
5549 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5550 {
5551 error(functionCall->getLine(), "Texture component must be a constant expression",
5552 name.c_str());
5553 }
5554 else
5555 {
5556 int component = componentConstantUnion->getIConst(0);
5557 if (component < 0 || component > 3)
5558 {
5559 error(functionCall->getLine(), "Component must be in the range [0;3]",
5560 name.c_str());
5561 }
5562 }
5563 }
5564 }
5565}
5566
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005567void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5568{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005569 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005570 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005571 TIntermNode *offset = nullptr;
5572 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005573 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005574 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5575 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5576 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005577 {
5578 offset = arguments->back();
5579 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005580 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005581 {
5582 // A bias parameter might follow the offset parameter.
5583 ASSERT(arguments->size() >= 3);
5584 offset = (*arguments)[2];
5585 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005586 else if (name == "textureGatherOffset")
5587 {
5588 ASSERT(arguments->size() >= 3u);
5589 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5590 ASSERT(sampler != nullptr);
5591 switch (sampler->getBasicType())
5592 {
5593 case EbtSampler2D:
5594 case EbtISampler2D:
5595 case EbtUSampler2D:
5596 case EbtSampler2DArray:
5597 case EbtISampler2DArray:
5598 case EbtUSampler2DArray:
5599 offset = (*arguments)[2];
5600 break;
5601 case EbtSampler2DShadow:
5602 case EbtSampler2DArrayShadow:
5603 offset = (*arguments)[3];
5604 break;
5605 default:
5606 UNREACHABLE();
5607 break;
5608 }
5609 useTextureGatherOffsetConstraints = true;
5610 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005611 if (offset != nullptr)
5612 {
5613 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5614 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5615 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005617 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 }
5619 else
5620 {
5621 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5622 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005623 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005624 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5625 : mMinProgramTexelOffset;
5626 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5627 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005628 for (size_t i = 0u; i < size; ++i)
5629 {
5630 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005631 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005632 {
5633 std::stringstream tokenStream;
5634 tokenStream << offsetValue;
5635 std::string token = tokenStream.str();
5636 error(offset->getLine(), "Texture offset value out of valid range",
5637 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005638 }
5639 }
5640 }
5641 }
5642}
5643
Jiajia Qina3106c52017-11-03 09:39:39 +08005644void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5645{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005646 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005647 const TString &functionName = functionCall->getFunction()->name();
5648 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005649 {
5650 TIntermSequence *arguments = functionCall->getSequence();
5651 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5652
5653 if (IsBufferOrSharedVariable(memNode))
5654 {
5655 return;
5656 }
5657
5658 while (memNode->getAsBinaryNode())
5659 {
5660 memNode = memNode->getAsBinaryNode()->getLeft();
5661 if (IsBufferOrSharedVariable(memNode))
5662 {
5663 return;
5664 }
5665 }
5666
5667 error(memNode->getLine(),
5668 "The value passed to the mem argument of an atomic memory function does not "
5669 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005670 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005671 }
5672}
5673
Martin Radev2cc85b32016-08-05 16:22:53 +03005674// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5675void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5676{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005677 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005678 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005679
5680 if (name.compare(0, 5, "image") == 0)
5681 {
5682 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005683 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005684
Olli Etuaho485eefd2017-02-14 17:40:06 +00005685 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005686
5687 if (name.compare(5, 5, "Store") == 0)
5688 {
5689 if (memoryQualifier.readonly)
5690 {
5691 error(imageNode->getLine(),
5692 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005693 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005694 }
5695 }
5696 else if (name.compare(5, 4, "Load") == 0)
5697 {
5698 if (memoryQualifier.writeonly)
5699 {
5700 error(imageNode->getLine(),
5701 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005702 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005703 }
5704 }
5705 }
5706}
5707
5708// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5709void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5710 const TFunction *functionDefinition,
5711 const TIntermAggregate *functionCall)
5712{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005713 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005714
5715 const TIntermSequence &arguments = *functionCall->getSequence();
5716
5717 ASSERT(functionDefinition->getParamCount() == arguments.size());
5718
5719 for (size_t i = 0; i < arguments.size(); ++i)
5720 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005721 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5722 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005723 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5724 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5725
5726 if (IsImage(functionArgumentType.getBasicType()))
5727 {
5728 const TMemoryQualifier &functionArgumentMemoryQualifier =
5729 functionArgumentType.getMemoryQualifier();
5730 const TMemoryQualifier &functionParameterMemoryQualifier =
5731 functionParameterType.getMemoryQualifier();
5732 if (functionArgumentMemoryQualifier.readonly &&
5733 !functionParameterMemoryQualifier.readonly)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005738 }
5739
5740 if (functionArgumentMemoryQualifier.writeonly &&
5741 !functionParameterMemoryQualifier.writeonly)
5742 {
5743 error(functionCall->getLine(),
5744 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005746 }
Martin Radev049edfa2016-11-11 14:35:37 +02005747
5748 if (functionArgumentMemoryQualifier.coherent &&
5749 !functionParameterMemoryQualifier.coherent)
5750 {
5751 error(functionCall->getLine(),
5752 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005753 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005754 }
5755
5756 if (functionArgumentMemoryQualifier.volatileQualifier &&
5757 !functionParameterMemoryQualifier.volatileQualifier)
5758 {
5759 error(functionCall->getLine(),
5760 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005761 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005762 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005763 }
5764 }
5765}
5766
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005767TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005768{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005770}
5771
5772TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005774 TIntermNode *thisNode,
5775 const TSourceLoc &loc)
5776{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005777 if (thisNode != nullptr)
5778 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005779 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005780 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005781
5782 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005783 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005784 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005785 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005786 }
5787 else
5788 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005789 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005790 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005791 }
5792}
5793
Olli Etuahobed35d72017-12-20 16:36:26 +02005794TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005795 TIntermSequence *arguments,
5796 TIntermNode *thisNode,
5797 const TSourceLoc &loc)
5798{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 TIntermTyped *typedThis = thisNode->getAsTyped();
5800 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5801 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5802 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005803 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005804 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005806 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807 }
5808 else if (!arguments->empty())
5809 {
5810 error(loc, "method takes no parameters", "length");
5811 }
5812 else if (typedThis == nullptr || !typedThis->isArray())
5813 {
5814 error(loc, "length can only be called on arrays", "length");
5815 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005816 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5817 mGeometryShaderInputPrimitiveType == EptUndefined)
5818 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005819 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005820 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5821 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 else
5823 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005824 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5825 node->setLine(loc);
5826 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005828 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829}
5830
Olli Etuahobed35d72017-12-20 16:36:26 +02005831TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 TIntermSequence *arguments,
5833 const TSourceLoc &loc)
5834{
5835 // First find by unmangled name to check whether the function name has been
5836 // hidden by a variable name or struct typename.
5837 // If a function is found, check for one with a matching argument list.
5838 bool builtIn;
Olli Etuahobed35d72017-12-20 16:36:26 +02005839 const TSymbol *symbol = symbolTable.find(name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005840 if (symbol != nullptr && !symbol->isFunction())
5841 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005842 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005843 }
5844 else
5845 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005846 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005847 mShaderVersion, &builtIn);
5848 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005849 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005850 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 }
5852 else
5853 {
5854 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 //
5856 // A declared function.
5857 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005858 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005859 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005860 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005861 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005862 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005863 if (builtIn && op != EOpNull)
5864 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005866 if (fnCandidate->getParamCount() == 1)
5867 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005868 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005869 TIntermNode *unaryParamNode = arguments->front();
5870 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005871 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005872 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005873 }
5874 else
5875 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005876 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005877 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005879
5880 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005881 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305882
Olli Etuaho765924f2018-01-04 12:48:36 +02005883 // See if we can constant fold a built-in. Note that this may be possible
5884 // even if it is not const-qualified.
5885 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005886 }
5887 }
5888 else
5889 {
5890 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005892
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005893 // If builtIn == false, the function is user defined - could be an overloaded
5894 // built-in as well.
5895 // if builtIn == true, it's a builtIn function with no op associated with it.
5896 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005897 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005898 {
Olli Etuahofe486322017-03-21 09:30:54 +00005899 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005900 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005901 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005902 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005903 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005904 }
5905 else
5906 {
Olli Etuahofe486322017-03-21 09:30:54 +00005907 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005909 }
5910
Jiajia Qinbc585152017-06-23 15:42:17 +08005911 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005912
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005913 callNode->setLine(loc);
5914
5915 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005916 }
5917 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005918 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005919
5920 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005921 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005922}
5923
Jamie Madillb98c3a82015-07-23 14:26:04 -04005924TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005925 TIntermTyped *trueExpression,
5926 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005927 const TSourceLoc &loc)
5928{
Olli Etuaho56229f12017-07-10 14:16:33 +03005929 if (!checkIsScalarBool(loc, cond))
5930 {
5931 return falseExpression;
5932 }
Olli Etuaho52901742015-04-15 13:42:45 +03005933
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005934 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005935 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005936 std::stringstream reasonStream;
5937 reasonStream << "mismatching ternary operator operand types '"
5938 << trueExpression->getCompleteString() << " and '"
5939 << falseExpression->getCompleteString() << "'";
5940 std::string reason = reasonStream.str();
5941 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005942 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005943 }
Olli Etuahode318b22016-10-25 16:18:25 +01005944 if (IsOpaqueType(trueExpression->getBasicType()))
5945 {
5946 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005947 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005948 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5949 // Note that structs containing opaque types don't need to be checked as structs are
5950 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005951 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005952 return falseExpression;
5953 }
5954
Jiajia Qinbc585152017-06-23 15:42:17 +08005955 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5956 falseExpression->getMemoryQualifier().writeonly)
5957 {
5958 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5959 return falseExpression;
5960 }
5961
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005962 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005963 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005964 // ESSL 3.00.6 section 5.7:
5965 // Ternary operator support is optional for arrays. No certainty that it works across all
5966 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5967 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005968 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005969 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005970 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005971 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005972 }
Olli Etuaho94050052017-05-08 14:17:44 +03005973 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5974 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005975 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005976 return falseExpression;
5977 }
5978
Corentin Wallez0d959252016-07-12 17:26:32 -04005979 // WebGL2 section 5.26, the following results in an error:
5980 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005981 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005982 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005983 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005984 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005985 }
5986
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005987 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5988 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005989 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005990}
Olli Etuaho49300862015-02-20 14:54:49 +02005991
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005992//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005993// Parse an array of strings using yyparse.
5994//
5995// Returns 0 for success.
5996//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005997int PaParseStrings(size_t count,
5998 const char *const string[],
5999 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306000 TParseContext *context)
6001{
Yunchao He4f285442017-04-21 12:15:49 +08006002 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006003 return 1;
6004
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006005 if (glslang_initialize(context))
6006 return 1;
6007
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006008 int error = glslang_scan(count, string, length, context);
6009 if (!error)
6010 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006011
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006012 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006013
alokp@chromium.org6b495712012-06-29 00:06:58 +00006014 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006015}
Jamie Madill45bcc782016-11-07 13:58:48 -05006016
6017} // namespace sh