blob: 5e36b8631a7c7f9709c2a43594ae6e8255591541 [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 {
91 return imageSymbol->getSymbol().c_str();
92 }
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),
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800238 mGeometryShaderInputArraySize(0u)
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 Etuaho4de340a2016-12-16 09:32:03 +0000609 const char *symbol = symNode->getSymbol().c_str();
610 std::stringstream reasonStream;
611 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
612 std::string reason = reasonStream.str();
613 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 else
616 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000617 std::stringstream reasonStream;
618 reasonStream << "l-value required (" << message << ")";
619 std::string reason = reasonStream.str();
620 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000622
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624}
625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626// Both test, and if necessary spit out an error, to see if the node is really
627// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300628void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Olli Etuaho383b7912016-08-05 11:22:59 +0300630 if (node->getQualifier() != EvqConst)
631 {
632 error(node->getLine(), "constant expression required", "");
633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634}
635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636// Both test, and if necessary spit out an error, to see if the node is really
637// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300638void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639{
Olli Etuaho383b7912016-08-05 11:22:59 +0300640 if (!node->isScalarInt())
641 {
642 error(node->getLine(), "integer expression required", token);
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646// Both test, and if necessary spit out an error, to see if we are currently
647// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800648bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuaho856c4972016-08-08 11:38:39 +0300650 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300651 {
652 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800653 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800655 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300658// ESSL 3.00.5 sections 3.8 and 3.9.
659// If it starts "gl_" or contains two consecutive underscores, it's reserved.
660// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300661bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300664 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530665 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300666 error(line, reservedErrMsg, "gl_");
667 return false;
668 }
669 if (sh::IsWebGLBasedSpec(mShaderSpec))
670 {
671 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300673 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300674 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300678 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
681 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300682 if (identifier.find("__") != TString::npos)
683 {
684 error(line,
685 "identifiers containing two consecutive underscores (__) are reserved as "
686 "possible future keywords",
687 identifier.c_str());
688 return false;
689 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300693// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300694bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800695 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300696 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200700 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300704 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530705 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300706 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200707 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300708 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200709 {
jchen10cc2a10e2017-05-03 14:05:12 +0800710 std::string reason("cannot convert a variable with type ");
711 reason += getBasicString(argTyped->getBasicType());
712 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300713 return false;
714 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800715 else if (argTyped->getMemoryQualifier().writeonly)
716 {
717 error(line, "cannot convert a variable with writeonly", "constructor");
718 return false;
719 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200720 if (argTyped->getBasicType() == EbtVoid)
721 {
722 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300723 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200724 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726
Olli Etuaho856c4972016-08-08 11:38:39 +0300727 if (type.isArray())
728 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300729 // The size of an unsized constructor should already have been determined.
730 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300731 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 {
733 error(line, "array constructor needs one argument per array element", "constructor");
734 return false;
735 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300736 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
737 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800738 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 {
740 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300741 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500742 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300743 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500744 return false;
745 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300746 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300747 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000748 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300749 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 }
751 }
752 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300753 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 {
755 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 if (fields.size() != arguments->size())
757 {
758 error(line,
759 "Number of constructor parameters does not match the number of structure fields",
760 "constructor");
761 return false;
762 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300763
764 for (size_t i = 0; i < fields.size(); i++)
765 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800766 if (i >= arguments->size() ||
767 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 {
769 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000770 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 }
773 }
774 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300775 else
776 {
777 // We're constructing a scalar, vector, or matrix.
778
779 // Note: It's okay to have too many components available, but not okay to have unused
780 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
781 // there is an extra argument, so 'overFull' will become true.
782
783 size_t size = 0;
784 bool full = false;
785 bool overFull = false;
786 bool matrixArg = false;
787 for (TIntermNode *arg : *arguments)
788 {
789 const TIntermTyped *argTyped = arg->getAsTyped();
790 ASSERT(argTyped != nullptr);
791
Olli Etuaho487b63a2017-05-23 15:55:09 +0300792 if (argTyped->getBasicType() == EbtStruct)
793 {
794 error(line, "a struct cannot be used as a constructor argument for this type",
795 "constructor");
796 return false;
797 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300798 if (argTyped->getType().isArray())
799 {
800 error(line, "constructing from a non-dereferenced array", "constructor");
801 return false;
802 }
803 if (argTyped->getType().isMatrix())
804 {
805 matrixArg = true;
806 }
807
808 size += argTyped->getType().getObjectSize();
809 if (full)
810 {
811 overFull = true;
812 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300813 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300814 {
815 full = true;
816 }
817 }
818
819 if (type.isMatrix() && matrixArg)
820 {
821 if (arguments->size() != 1)
822 {
823 error(line, "constructing matrix from matrix can only take one argument",
824 "constructor");
825 return false;
826 }
827 }
828 else
829 {
830 if (size != 1 && size < type.getObjectSize())
831 {
832 error(line, "not enough data provided for construction", "constructor");
833 return false;
834 }
835 if (overFull)
836 {
837 error(line, "too many arguments", "constructor");
838 return false;
839 }
840 }
841 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300842
Olli Etuaho8a176262016-08-16 14:23:01 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if a void variable has been declared and raise an error message for
847// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848//
849// returns true in case of an error
850//
Olli Etuaho856c4972016-08-08 11:38:39 +0300851bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400852 const TString &identifier,
853 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300855 if (type == EbtVoid)
856 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000857 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300858 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
Jamie Madillb98c3a82015-07-23 14:26:04 -0400864// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300865// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300866bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300868 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300871 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300873 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300877// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300878void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879{
Martin Radev4a9cd802016-09-01 16:51:51 +0300880 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530881 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000882 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
jchen10cc2a10e2017-05-03 14:05:12 +0800886bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
887 const TTypeSpecifierNonArray &pType,
888 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530890 if (pType.type == EbtStruct)
891 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300892 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000894 std::stringstream reasonStream;
895 reasonStream << reason << " (structure contains a sampler)";
896 std::string reasonStr = reasonStream.str();
897 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300898 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000899 }
jchen10cc2a10e2017-05-03 14:05:12 +0800900 // only samplers need to be checked from structs, since other opaque types can't be struct
901 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300902 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530903 }
jchen10cc2a10e2017-05-03 14:05:12 +0800904 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530905 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000906 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
Olli Etuaho856c4972016-08-08 11:38:39 +0300913void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
914 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400915{
916 if (pType.layoutQualifier.location != -1)
917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400918 error(line, "location must only be specified for a single input or output variable",
919 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400920 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400921}
922
Olli Etuaho856c4972016-08-08 11:38:39 +0300923void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
924 const TLayoutQualifier &layoutQualifier)
925{
926 if (layoutQualifier.location != -1)
927 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000928 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
929 if (mShaderVersion >= 310)
930 {
931 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800932 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000933 }
934 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300935 }
936}
937
Qin Jiajiaca68d982017-09-18 16:41:56 +0800938void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
939 const TLayoutBlockStorage &blockStorage,
940 const TQualifier &qualifier)
941{
942 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
943 {
944 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
945 }
946}
947
Martin Radev2cc85b32016-08-05 16:22:53 +0300948void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
949 TQualifier qualifier,
950 const TType &type)
951{
Martin Radev2cc85b32016-08-05 16:22:53 +0300952 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800953 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 {
jchen10cc2a10e2017-05-03 14:05:12 +0800955 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300960unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000963
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200964 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
965 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
966 // fold as array size.
967 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000968 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000969 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300970 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400974
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000975 if (constant->getBasicType() == EbtUInt)
976 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300977 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 }
979 else
980 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300981 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000982
Olli Etuaho856c4972016-08-08 11:38:39 +0300983 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000984 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400985 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400988
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400990 }
991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 {
994 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 }
997
998 // The size of arrays is restricted here to prevent issues further down the
999 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1000 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1001 const unsigned int sizeLimit = 65536;
1002
Olli Etuaho856c4972016-08-08 11:38:39 +03001003 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001004 {
1005 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001007 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001008
1009 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
1011
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001013bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1014 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015{
Olli Etuaho8a176262016-08-16 14:23:01 +03001016 if ((elementQualifier.qualifier == EvqAttribute) ||
1017 (elementQualifier.qualifier == EvqVertexIn) ||
1018 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001020 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementQualifier).getQualifierString());
1022 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
Olli Etuaho8a176262016-08-16 14:23:01 +03001025 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001029bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1030 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001032 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001033 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 error(line, "cannot declare arrays of arrays",
1035 TType(elementType).getCompleteString().c_str());
1036 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001037 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001038 return true;
1039}
1040
1041// Check if this qualified element type can be formed into an array. This is only called when array
1042// brackets are associated with an identifier in a declaration, like this:
1043// float a[2];
1044// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1045// are associated with the type, like this:
1046// float[2] a;
1047bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1048 const TPublicType &elementType)
1049{
1050 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1051 {
1052 return false;
1053 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001054 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1055 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1056 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001057 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001058 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001059 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001060 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001061 TType(elementType).getCompleteString().c_str());
1062 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001063 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001064 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065}
1066
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001068void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1069 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001070 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071{
Olli Etuaho3739d232015-04-08 12:23:44 +03001072 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001073 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001074 {
1075 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001076 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001077
1078 // Generate informative error messages for ESSL1.
1079 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001080 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001081 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301082 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001083 "structures containing arrays may not be declared constant since they cannot be "
1084 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001086 }
1087 else
1088 {
1089 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1090 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001092 // This will make the type sized if it isn't sized yet.
1093 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1094 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095}
1096
Olli Etuaho2935c582015-04-08 14:32:06 +03001097// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098// and update the symbol table.
1099//
Olli Etuaho2935c582015-04-08 14:32:06 +03001100// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102bool TParseContext::declareVariable(const TSourceLoc &line,
1103 const TString &identifier,
1104 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001105 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106{
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001108
Olli Etuaho43364892017-02-13 16:00:12 +00001109 checkBindingIsValid(line, type);
1110
Olli Etuaho856c4972016-08-08 11:38:39 +03001111 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 // gl_LastFragData may be redeclared with a new precision qualifier
1114 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1115 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001116 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1117 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001118 if (type.isArrayOfArrays())
1119 {
1120 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1121 identifier.c_str());
1122 return false;
1123 }
1124 else if (static_cast<int>(type.getOutermostArraySize()) ==
1125 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001126 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001127 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001129 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 }
1131 }
1132 else
1133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001134 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1135 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 return false;
1137 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
Olli Etuaho8a176262016-08-16 14:23:01 +03001140 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001141 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001142
Olli Etuaho0f684632017-07-13 12:42:15 +03001143 (*variable) = symbolTable.declareVariable(&identifier, type);
1144 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001145 {
1146 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001147 return false;
1148 }
1149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
1152
1153 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154}
1155
Martin Radev70866b82016-07-22 15:27:42 +03001156void TParseContext::checkIsParameterQualifierValid(
1157 const TSourceLoc &line,
1158 const TTypeQualifierBuilder &typeQualifierBuilder,
1159 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301160{
Olli Etuahocce89652017-06-19 16:04:09 +03001161 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001162 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001163
1164 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301165 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001166 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1167 }
1168
1169 if (!IsImage(type->getBasicType()))
1170 {
Olli Etuaho43364892017-02-13 16:00:12 +00001171 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001172 }
1173 else
1174 {
1175 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177
Martin Radev70866b82016-07-22 15:27:42 +03001178 type->setQualifier(typeQualifier.qualifier);
1179
1180 if (typeQualifier.precision != EbpUndefined)
1181 {
1182 type->setPrecision(typeQualifier.precision);
1183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
Olli Etuaho703671e2017-11-08 17:47:18 +02001186template <size_t size>
1187bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1188 const std::array<TExtension, size> &extensions)
1189{
1190 ASSERT(!extensions.empty());
1191 const TExtensionBehavior &extBehavior = extensionBehavior();
1192
1193 bool canUseWithWarning = false;
1194 bool canUseWithoutWarning = false;
1195
1196 const char *errorMsgString = "";
1197 TExtension errorMsgExtension = TExtension::UNDEFINED;
1198
1199 for (TExtension extension : extensions)
1200 {
1201 auto extIter = extBehavior.find(extension);
1202 if (canUseWithWarning)
1203 {
1204 // We already have an extension that we can use, but with a warning.
1205 // See if we can use the alternative extension without a warning.
1206 if (extIter == extBehavior.end())
1207 {
1208 continue;
1209 }
1210 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1211 {
1212 canUseWithoutWarning = true;
1213 break;
1214 }
1215 continue;
1216 }
1217 if (extIter == extBehavior.end())
1218 {
1219 errorMsgString = "extension is not supported";
1220 errorMsgExtension = extension;
1221 }
1222 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1223 {
1224 errorMsgString = "extension is disabled";
1225 errorMsgExtension = extension;
1226 }
1227 else if (extIter->second == EBhWarn)
1228 {
1229 errorMsgExtension = extension;
1230 canUseWithWarning = true;
1231 }
1232 else
1233 {
1234 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1235 canUseWithoutWarning = true;
1236 break;
1237 }
1238 }
1239
1240 if (canUseWithoutWarning)
1241 {
1242 return true;
1243 }
1244 if (canUseWithWarning)
1245 {
1246 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1247 return true;
1248 }
1249 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1250 return false;
1251}
1252
1253template bool TParseContext::checkCanUseOneOfExtensions(
1254 const TSourceLoc &line,
1255 const std::array<TExtension, 1> &extensions);
1256template bool TParseContext::checkCanUseOneOfExtensions(
1257 const TSourceLoc &line,
1258 const std::array<TExtension, 2> &extensions);
1259template bool TParseContext::checkCanUseOneOfExtensions(
1260 const TSourceLoc &line,
1261 const std::array<TExtension, 3> &extensions);
1262
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001263bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001264{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001265 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001266 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001267}
1268
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001269// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1270// compile-time or link-time errors are the same whether or not the declaration is empty".
1271// This function implements all the checks that are done on qualifiers regardless of if the
1272// declaration is empty.
1273void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1274 const sh::TLayoutQualifier &layoutQualifier,
1275 const TSourceLoc &location)
1276{
1277 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1278 {
1279 error(location, "Shared memory declarations cannot have layout specified", "layout");
1280 }
1281
1282 if (layoutQualifier.matrixPacking != EmpUnspecified)
1283 {
1284 error(location, "layout qualifier only valid for interface blocks",
1285 getMatrixPackingString(layoutQualifier.matrixPacking));
1286 return;
1287 }
1288
1289 if (layoutQualifier.blockStorage != EbsUnspecified)
1290 {
1291 error(location, "layout qualifier only valid for interface blocks",
1292 getBlockStorageString(layoutQualifier.blockStorage));
1293 return;
1294 }
1295
1296 if (qualifier == EvqFragmentOut)
1297 {
1298 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1299 {
1300 error(location, "invalid layout qualifier combination", "yuv");
1301 return;
1302 }
1303 }
1304 else
1305 {
1306 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1307 }
1308
Olli Etuaho95468d12017-05-04 11:14:34 +03001309 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1310 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001311 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1312 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001313 {
1314 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1315 }
1316
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001317 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001318 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001319 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001320 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001321 // We're not checking whether the uniform location is in range here since that depends on
1322 // the type of the variable.
1323 // The type can only be fully determined for non-empty declarations.
1324 }
1325 if (!canHaveLocation)
1326 {
1327 checkLocationIsNotSpecified(location, layoutQualifier);
1328 }
1329}
1330
jchen104cdac9e2017-05-08 11:01:20 +08001331void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1332 const TSourceLoc &location)
1333{
1334 if (publicType.precision != EbpHigh)
1335 {
1336 error(location, "Can only be highp", "atomic counter");
1337 }
1338 // dEQP enforces compile error if location is specified. See uniform_location.test.
1339 if (publicType.layoutQualifier.location != -1)
1340 {
1341 error(location, "location must not be set for atomic_uint", "layout");
1342 }
1343 if (publicType.layoutQualifier.binding == -1)
1344 {
1345 error(location, "no binding specified", "atomic counter");
1346 }
1347}
1348
Olli Etuaho55bde912017-10-25 13:41:13 +03001349void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001350{
Olli Etuaho55bde912017-10-25 13:41:13 +03001351 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001352 {
1353 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1354 // error. It is assumed that this applies to empty declarations as well.
1355 error(location, "empty array declaration needs to specify a size", "");
1356 }
Martin Radevb8b01222016-11-20 23:25:53 +02001357}
1358
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001359// These checks are done for all declarations that are non-empty. They're done for non-empty
1360// declarations starting a declarator list, and declarators that follow an empty declaration.
1361void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1362 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001363{
Olli Etuahofa33d582015-04-09 14:33:12 +03001364 switch (publicType.qualifier)
1365 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001366 case EvqVaryingIn:
1367 case EvqVaryingOut:
1368 case EvqAttribute:
1369 case EvqVertexIn:
1370 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001371 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001372 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001373 {
1374 error(identifierLocation, "cannot be used with a structure",
1375 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001376 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001377 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001378 break;
1379 case EvqBuffer:
1380 if (publicType.getBasicType() != EbtInterfaceBlock)
1381 {
1382 error(identifierLocation,
1383 "cannot declare buffer variables at global scope(outside a block)",
1384 getQualifierString(publicType.qualifier));
1385 return;
1386 }
1387 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001388 default:
1389 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001390 }
jchen10cc2a10e2017-05-03 14:05:12 +08001391 std::string reason(getBasicString(publicType.getBasicType()));
1392 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001393 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001394 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001395 {
1396 return;
1397 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001398
Andrei Volykhina5527072017-03-22 16:46:30 +03001399 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1400 publicType.qualifier != EvqConst) &&
1401 publicType.getBasicType() == EbtYuvCscStandardEXT)
1402 {
1403 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1404 getQualifierString(publicType.qualifier));
1405 return;
1406 }
1407
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001408 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1409 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001410 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1411 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001412 TType type(publicType);
1413 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001414 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1416 publicType.layoutQualifier);
1417 }
1418 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001419
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001420 // check for layout qualifier issues
1421 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001422
Martin Radev2cc85b32016-08-05 16:22:53 +03001423 if (IsImage(publicType.getBasicType()))
1424 {
1425
1426 switch (layoutQualifier.imageInternalFormat)
1427 {
1428 case EiifRGBA32F:
1429 case EiifRGBA16F:
1430 case EiifR32F:
1431 case EiifRGBA8:
1432 case EiifRGBA8_SNORM:
1433 if (!IsFloatImage(publicType.getBasicType()))
1434 {
1435 error(identifierLocation,
1436 "internal image format requires a floating image type",
1437 getBasicString(publicType.getBasicType()));
1438 return;
1439 }
1440 break;
1441 case EiifRGBA32I:
1442 case EiifRGBA16I:
1443 case EiifRGBA8I:
1444 case EiifR32I:
1445 if (!IsIntegerImage(publicType.getBasicType()))
1446 {
1447 error(identifierLocation,
1448 "internal image format requires an integer image type",
1449 getBasicString(publicType.getBasicType()));
1450 return;
1451 }
1452 break;
1453 case EiifRGBA32UI:
1454 case EiifRGBA16UI:
1455 case EiifRGBA8UI:
1456 case EiifR32UI:
1457 if (!IsUnsignedImage(publicType.getBasicType()))
1458 {
1459 error(identifierLocation,
1460 "internal image format requires an unsigned image type",
1461 getBasicString(publicType.getBasicType()));
1462 return;
1463 }
1464 break;
1465 case EiifUnspecified:
1466 error(identifierLocation, "layout qualifier", "No image internal format specified");
1467 return;
1468 default:
1469 error(identifierLocation, "layout qualifier", "unrecognized token");
1470 return;
1471 }
1472
1473 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1474 switch (layoutQualifier.imageInternalFormat)
1475 {
1476 case EiifR32F:
1477 case EiifR32I:
1478 case EiifR32UI:
1479 break;
1480 default:
1481 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1482 {
1483 error(identifierLocation, "layout qualifier",
1484 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1485 "image variables must be qualified readonly and/or writeonly");
1486 return;
1487 }
1488 break;
1489 }
1490 }
1491 else
1492 {
Olli Etuaho43364892017-02-13 16:00:12 +00001493 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001494 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1495 }
jchen104cdac9e2017-05-08 11:01:20 +08001496
1497 if (IsAtomicCounter(publicType.getBasicType()))
1498 {
1499 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1500 }
1501 else
1502 {
1503 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1504 }
Olli Etuaho43364892017-02-13 16:00:12 +00001505}
Martin Radev2cc85b32016-08-05 16:22:53 +03001506
Olli Etuaho43364892017-02-13 16:00:12 +00001507void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1508{
1509 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001510 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1511 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1512 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1513 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1514 // when it comes to which shaders are accepted by the compiler.
1515 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001516 if (IsImage(type.getBasicType()))
1517 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001518 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1519 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001520 }
1521 else if (IsSampler(type.getBasicType()))
1522 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001523 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1524 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001525 }
jchen104cdac9e2017-05-08 11:01:20 +08001526 else if (IsAtomicCounter(type.getBasicType()))
1527 {
1528 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1529 }
Olli Etuaho43364892017-02-13 16:00:12 +00001530 else
1531 {
1532 ASSERT(!IsOpaqueType(type.getBasicType()));
1533 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001534 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001535}
1536
Olli Etuaho856c4972016-08-08 11:38:39 +03001537void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1538 const TString &layoutQualifierName,
1539 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001540{
1541
1542 if (mShaderVersion < versionRequired)
1543 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001544 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001545 }
1546}
1547
Olli Etuaho856c4972016-08-08 11:38:39 +03001548bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1549 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001550{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001551 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001552 for (size_t i = 0u; i < localSize.size(); ++i)
1553 {
1554 if (localSize[i] != -1)
1555 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001556 error(location,
1557 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1558 "global layout declaration",
1559 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001560 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001561 }
1562 }
1563
Olli Etuaho8a176262016-08-16 14:23:01 +03001564 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001565}
1566
Olli Etuaho43364892017-02-13 16:00:12 +00001567void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001568 TLayoutImageInternalFormat internalFormat)
1569{
1570 if (internalFormat != EiifUnspecified)
1571 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001572 error(location, "invalid layout qualifier: only valid when used with images",
1573 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001574 }
Olli Etuaho43364892017-02-13 16:00:12 +00001575}
1576
1577void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1578{
1579 if (binding != -1)
1580 {
1581 error(location,
1582 "invalid layout qualifier: only valid when used with opaque types or blocks",
1583 "binding");
1584 }
1585}
1586
jchen104cdac9e2017-05-08 11:01:20 +08001587void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1588{
1589 if (offset != -1)
1590 {
1591 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1592 "offset");
1593 }
1594}
1595
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001596void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1597 int binding,
1598 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001599{
1600 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001601 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001602 {
1603 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1604 }
1605}
1606
1607void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1608 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001609 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001610{
1611 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001612 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001613 {
1614 error(location, "sampler binding greater than maximum texture units", "binding");
1615 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001616}
1617
Jiajia Qinbc585152017-06-23 15:42:17 +08001618void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1619 const TQualifier &qualifier,
1620 int binding,
1621 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001622{
1623 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001624 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001625 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001626 if (binding + size > mMaxUniformBufferBindings)
1627 {
1628 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1629 "binding");
1630 }
1631 }
1632 else if (qualifier == EvqBuffer)
1633 {
1634 if (binding + size > mMaxShaderStorageBufferBindings)
1635 {
1636 error(location,
1637 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1638 "binding");
1639 }
jchen10af713a22017-04-19 09:10:56 +08001640 }
1641}
jchen104cdac9e2017-05-08 11:01:20 +08001642void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1643{
1644 if (binding >= mMaxAtomicCounterBindings)
1645 {
1646 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1647 "binding");
1648 }
1649}
jchen10af713a22017-04-19 09:10:56 +08001650
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001651void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1652 int objectLocationCount,
1653 const TLayoutQualifier &layoutQualifier)
1654{
1655 int loc = layoutQualifier.location;
1656 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1657 {
1658 error(location, "Uniform location out of range", "location");
1659 }
1660}
1661
Andrei Volykhina5527072017-03-22 16:46:30 +03001662void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1663{
1664 if (yuv != false)
1665 {
1666 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1667 }
1668}
1669
Jiajia Qinbc585152017-06-23 15:42:17 +08001670void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1671 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001672{
1673 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1674 {
1675 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001676 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1677 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1678 qual == EvqInOut || qual == EvqConstReadOnly))
1679 {
1680 if (argument->getMemoryQualifier().writeonly)
1681 {
1682 error(argument->getLine(),
1683 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1684 fnCall->getFunctionSymbolInfo()->getName().c_str());
1685 return;
1686 }
1687 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001688 if (qual == EvqOut || qual == EvqInOut)
1689 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001690 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001691 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001692 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001693 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001694 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001695 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001696 }
1697 }
1698 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001699}
1700
Martin Radev70866b82016-07-22 15:27:42 +03001701void TParseContext::checkInvariantVariableQualifier(bool invariant,
1702 const TQualifier qualifier,
1703 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001704{
Martin Radev70866b82016-07-22 15:27:42 +03001705 if (!invariant)
1706 return;
1707
1708 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001709 {
Martin Radev70866b82016-07-22 15:27:42 +03001710 // input variables in the fragment shader can be also qualified as invariant
1711 if (!sh::CanBeInvariantESSL1(qualifier))
1712 {
1713 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1714 }
1715 }
1716 else
1717 {
1718 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1719 {
1720 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1721 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001722 }
1723}
1724
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001725bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001726{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001727 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001728}
1729
Jamie Madillb98c3a82015-07-23 14:26:04 -04001730void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1731 const char *extName,
1732 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001733{
1734 pp::SourceLocation srcLoc;
1735 srcLoc.file = loc.first_file;
1736 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001737 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001738}
1739
Jamie Madillb98c3a82015-07-23 14:26:04 -04001740void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1741 const char *name,
1742 const char *value,
1743 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001744{
1745 pp::SourceLocation srcLoc;
1746 srcLoc.file = loc.first_file;
1747 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001748 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001749}
1750
Martin Radev4c4c8e72016-08-04 12:25:34 +03001751sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001752{
Jamie Madill2f294c92017-11-20 14:47:26 -05001753 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001754 for (size_t i = 0u; i < result.size(); ++i)
1755 {
1756 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1757 {
1758 result[i] = 1;
1759 }
1760 else
1761 {
1762 result[i] = mComputeShaderLocalSize[i];
1763 }
1764 }
1765 return result;
1766}
1767
Olli Etuaho56229f12017-07-10 14:16:33 +03001768TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1769 const TSourceLoc &line)
1770{
1771 TIntermConstantUnion *node = new TIntermConstantUnion(
1772 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1773 node->setLine(line);
1774 return node;
1775}
1776
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777/////////////////////////////////////////////////////////////////////////////////
1778//
1779// Non-Errors.
1780//
1781/////////////////////////////////////////////////////////////////////////////////
1782
Jamie Madill5c097022014-08-20 16:38:32 -04001783const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1784 const TString *name,
1785 const TSymbol *symbol)
1786{
Jamie Madill5c097022014-08-20 16:38:32 -04001787 if (!symbol)
1788 {
1789 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001790 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001791 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001792
1793 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001794 {
1795 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001796 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001797 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001798
1799 const TVariable *variable = static_cast<const TVariable *>(symbol);
1800
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001801 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001802 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001803 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001804 }
1805
Olli Etuaho0f684632017-07-13 12:42:15 +03001806 // Reject shaders using both gl_FragData and gl_FragColor
1807 TQualifier qualifier = variable->getType().getQualifier();
1808 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001809 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001810 mUsesFragData = true;
1811 }
1812 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1813 {
1814 mUsesFragColor = true;
1815 }
1816 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1817 {
1818 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001819 }
1820
Olli Etuaho0f684632017-07-13 12:42:15 +03001821 // This validation is not quite correct - it's only an error to write to
1822 // both FragData and FragColor. For simplicity, and because users shouldn't
1823 // be rewarded for reading from undefined varaibles, return an error
1824 // if they are both referenced, rather than assigned.
1825 if (mUsesFragData && mUsesFragColor)
1826 {
1827 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1828 if (mUsesSecondaryOutputs)
1829 {
1830 errorMessage =
1831 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1832 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1833 }
1834 error(location, errorMessage, name->c_str());
1835 }
1836
1837 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1838 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1839 qualifier == EvqWorkGroupSize)
1840 {
1841 error(location,
1842 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1843 "gl_WorkGroupSize");
1844 }
Jamie Madill5c097022014-08-20 16:38:32 -04001845 return variable;
1846}
1847
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001848TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1849 const TString *name,
1850 const TSymbol *symbol)
1851{
1852 const TVariable *variable = getNamedVariable(location, name, symbol);
1853
Olli Etuaho0f684632017-07-13 12:42:15 +03001854 if (!variable)
1855 {
1856 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1857 node->setLine(location);
1858 return node;
1859 }
1860
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001861 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001862 TIntermTyped *node = nullptr;
1863
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001864 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001865 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001866 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001867 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001868 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001869 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001870 {
1871 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1872 // needs to be added to the AST as a constant and not as a symbol.
1873 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1874 TConstantUnion *constArray = new TConstantUnion[3];
1875 for (size_t i = 0; i < 3; ++i)
1876 {
1877 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1878 }
1879
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001880 ASSERT(variableType.getBasicType() == EbtUInt);
1881 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001882
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001883 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001884 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001885 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001886 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001887 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1888 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001889 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001890 ASSERT(mGeometryShaderInputArraySize > 0u);
1891
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001892 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho55bde912017-10-25 13:41:13 +03001893 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001894 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001895 else
1896 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001897 node = new TIntermSymbol(variable->uniqueId(), variable->name(), variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001898 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001899 ASSERT(node != nullptr);
1900 node->setLine(location);
1901 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902}
1903
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904// Initializers show up in several places in the grammar. Have one set of
1905// code to handle them here.
1906//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001907// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001908bool TParseContext::executeInitializer(const TSourceLoc &line,
1909 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001910 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001911 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001912 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913{
Olli Etuaho13389b62016-10-16 11:48:18 +01001914 ASSERT(initNode != nullptr);
1915 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916
Olli Etuaho2935c582015-04-08 14:32:06 +03001917 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001918 if (type.isUnsizedArray())
1919 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001920 // In case initializer is not an array or type has more dimensions than initializer, this
1921 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1922 // actually is an array or not. Having a non-array initializer for an unsized array will
1923 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001924 auto *arraySizes = initializer->getType().getArraySizes();
1925 type.sizeUnsizedArrays(arraySizes);
Olli Etuaho376f1b52015-04-13 13:23:41 +03001926 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001927 if (!declareVariable(line, identifier, type, &variable))
1928 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001929 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001930 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931
Olli Etuahob0c645e2015-05-12 14:25:36 +03001932 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001933 if (symbolTable.atGlobalLevel() &&
1934 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001935 {
1936 // Error message does not completely match behavior with ESSL 1.00, but
1937 // we want to steer developers towards only using constant expressions.
1938 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001939 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001940 }
1941 if (globalInitWarning)
1942 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001943 warning(
1944 line,
1945 "global variable initializers should be constant expressions "
1946 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1947 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001948 }
1949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 //
1951 // identifier must be of type constant, a global, or a temporary
1952 //
1953 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301954 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1955 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001956 error(line, " cannot initialize this type of qualifier ",
1957 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001958 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001959 }
1960 //
1961 // test for and propagate constant
1962 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963
Arun Patole7e7e68d2015-05-22 12:02:25 +05301964 if (qualifier == EvqConst)
1965 {
1966 if (qualifier != initializer->getType().getQualifier())
1967 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001968 std::stringstream reasonStream;
1969 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1970 << "'";
1971 std::string reason = reasonStream.str();
1972 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001973 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001974 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001975 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301976 if (type != initializer->getType())
1977 {
1978 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001979 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001980 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001981 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001982 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001983
1984 // Save the constant folded value to the variable if possible. For example array
1985 // initializers are not folded, since that way copying the array literal to multiple places
1986 // in the shader is avoided.
1987 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1988 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301989 if (initializer->getAsConstantUnion())
1990 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001991 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 ASSERT(*initNode == nullptr);
1993 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301994 }
1995 else if (initializer->getAsSymbolNode())
1996 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001997 const TSymbol *symbol =
1998 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1999 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002001 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002002 if (constArray)
2003 {
2004 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002005 ASSERT(*initNode == nullptr);
2006 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002007 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002008 }
2009 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002010
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002011 TIntermSymbol *intermSymbol =
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002012 new TIntermSymbol(variable->uniqueId(), variable->name(), variable->getType());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002013 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002014 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2015 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002016 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002017 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002018 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002019 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002020
Olli Etuaho914b79a2017-06-19 16:03:19 +03002021 return true;
2022}
2023
2024TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2025 const TString &identifier,
2026 TIntermTyped *initializer,
2027 const TSourceLoc &loc)
2028{
2029 checkIsScalarBool(loc, pType);
2030 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002031 TType type(pType);
2032 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002033 {
2034 // The initializer is valid. The init condition needs to have a node - either the
2035 // initializer node, or a constant node in case the initialized variable is const and won't
2036 // be recorded in the AST.
2037 if (initNode == nullptr)
2038 {
2039 return initializer;
2040 }
2041 else
2042 {
2043 TIntermDeclaration *declaration = new TIntermDeclaration();
2044 declaration->appendDeclarator(initNode);
2045 return declaration;
2046 }
2047 }
2048 return nullptr;
2049}
2050
2051TIntermNode *TParseContext::addLoop(TLoopType type,
2052 TIntermNode *init,
2053 TIntermNode *cond,
2054 TIntermTyped *expr,
2055 TIntermNode *body,
2056 const TSourceLoc &line)
2057{
2058 TIntermNode *node = nullptr;
2059 TIntermTyped *typedCond = nullptr;
2060 if (cond)
2061 {
2062 typedCond = cond->getAsTyped();
2063 }
2064 if (cond == nullptr || typedCond)
2065 {
Olli Etuahocce89652017-06-19 16:04:09 +03002066 if (type == ELoopDoWhile)
2067 {
2068 checkIsScalarBool(line, typedCond);
2069 }
2070 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2071 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2072 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2073 !typedCond->isVector()));
2074
Olli Etuaho3ec75682017-07-05 17:02:55 +03002075 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002076 node->setLine(line);
2077 return node;
2078 }
2079
Olli Etuahocce89652017-06-19 16:04:09 +03002080 ASSERT(type != ELoopDoWhile);
2081
Olli Etuaho914b79a2017-06-19 16:03:19 +03002082 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2083 ASSERT(declaration);
2084 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2085 ASSERT(declarator->getLeft()->getAsSymbolNode());
2086
2087 // The condition is a declaration. In the AST representation we don't support declarations as
2088 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2089 // the loop.
2090 TIntermBlock *block = new TIntermBlock();
2091
2092 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2093 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2094 block->appendStatement(declareCondition);
2095
2096 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2097 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002098 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002099 block->appendStatement(loop);
2100 loop->setLine(line);
2101 block->setLine(line);
2102 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103}
2104
Olli Etuahocce89652017-06-19 16:04:09 +03002105TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2106 TIntermNodePair code,
2107 const TSourceLoc &loc)
2108{
Olli Etuaho56229f12017-07-10 14:16:33 +03002109 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002110
2111 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002112 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002113 {
2114 if (cond->getAsConstantUnion()->getBConst(0) == true)
2115 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002116 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002117 }
2118 else
2119 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002120 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002121 }
2122 }
2123
Olli Etuaho3ec75682017-07-05 17:02:55 +03002124 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002125 node->setLine(loc);
2126
2127 return node;
2128}
2129
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002130void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2131{
2132 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2133 typeSpecifier->getBasicType());
2134
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002135 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002136 {
2137 error(typeSpecifier->getLine(), "not supported", "first-class array");
2138 typeSpecifier->clearArrayness();
2139 }
2140}
2141
Martin Radev70866b82016-07-22 15:27:42 +03002142TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302143 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002144{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002145 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002146
Martin Radev70866b82016-07-22 15:27:42 +03002147 TPublicType returnType = typeSpecifier;
2148 returnType.qualifier = typeQualifier.qualifier;
2149 returnType.invariant = typeQualifier.invariant;
2150 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002151 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002152 returnType.precision = typeSpecifier.precision;
2153
2154 if (typeQualifier.precision != EbpUndefined)
2155 {
2156 returnType.precision = typeQualifier.precision;
2157 }
2158
Martin Radev4a9cd802016-09-01 16:51:51 +03002159 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2160 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002161
Martin Radev4a9cd802016-09-01 16:51:51 +03002162 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2163 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002166
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002167 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002168 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002169 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002170 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002172 returnType.clearArrayness();
2173 }
2174
Martin Radev70866b82016-07-22 15:27:42 +03002175 if (returnType.qualifier == EvqAttribute &&
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 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002181
Martin Radev70866b82016-07-22 15:27:42 +03002182 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002183 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002184 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002185 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002186 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002187 }
2188 }
2189 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002190 {
Martin Radev70866b82016-07-22 15:27:42 +03002191 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002192 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002194 }
Martin Radev70866b82016-07-22 15:27:42 +03002195 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2196 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002197 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2199 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002200 }
Martin Radev70866b82016-07-22 15:27:42 +03002201 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002202 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002203 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002204 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002205 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002206 }
2207
2208 return returnType;
2209}
2210
Olli Etuaho856c4972016-08-08 11:38:39 +03002211void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2212 const TPublicType &type,
2213 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002214{
2215 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002216 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002217 {
2218 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002219 }
2220
2221 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2222 switch (qualifier)
2223 {
2224 case EvqVertexIn:
2225 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002226 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002227 {
2228 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002229 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002230 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002231 return;
2232 case EvqFragmentOut:
2233 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002234 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002235 {
2236 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002237 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002238 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002239 return;
2240 default:
2241 break;
2242 }
2243
2244 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2245 // restrictions.
2246 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002247 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2248 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002249 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2250 {
2251 error(qualifierLocation, "must use 'flat' interpolation here",
2252 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002253 }
2254
Martin Radev4a9cd802016-09-01 16:51:51 +03002255 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 {
2257 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2258 // These restrictions are only implied by the ESSL 3.00 spec, but
2259 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002260 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002261 {
2262 error(qualifierLocation, "cannot be an array of structures",
2263 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 }
2265 if (type.isStructureContainingArrays())
2266 {
2267 error(qualifierLocation, "cannot be a structure containing an array",
2268 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 }
2270 if (type.isStructureContainingType(EbtStruct))
2271 {
2272 error(qualifierLocation, "cannot be a structure containing a structure",
2273 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002274 }
2275 if (type.isStructureContainingType(EbtBool))
2276 {
2277 error(qualifierLocation, "cannot be a structure containing a bool",
2278 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002279 }
2280 }
2281}
2282
Martin Radev2cc85b32016-08-05 16:22:53 +03002283void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2284{
2285 if (qualifier.getType() == QtStorage)
2286 {
2287 const TStorageQualifierWrapper &storageQualifier =
2288 static_cast<const TStorageQualifierWrapper &>(qualifier);
2289 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2290 !symbolTable.atGlobalLevel())
2291 {
2292 error(storageQualifier.getLine(),
2293 "Local variables can only use the const storage qualifier.",
2294 storageQualifier.getQualifierString().c_str());
2295 }
2296 }
2297}
2298
Olli Etuaho43364892017-02-13 16:00:12 +00002299void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002300 const TSourceLoc &location)
2301{
Jiajia Qinbc585152017-06-23 15:42:17 +08002302 const std::string reason(
2303 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2304 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002305 if (memoryQualifier.readonly)
2306 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002307 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002308 }
2309 if (memoryQualifier.writeonly)
2310 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002311 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002312 }
Martin Radev049edfa2016-11-11 14:35:37 +02002313 if (memoryQualifier.coherent)
2314 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002315 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002316 }
2317 if (memoryQualifier.restrictQualifier)
2318 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002319 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002320 }
2321 if (memoryQualifier.volatileQualifier)
2322 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002323 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002324 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002325}
2326
jchen104cdac9e2017-05-08 11:01:20 +08002327// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2328// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002329void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2330 const TSourceLoc &loc,
2331 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002332{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002333 if (!IsAtomicCounter(type->getBasicType()))
2334 {
2335 return;
2336 }
2337
2338 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2339 : kAtomicCounterSize;
2340 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2341 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002342 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002343 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002344 {
2345 offset = bindingState.appendSpan(size);
2346 }
2347 else
2348 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002349 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002350 }
2351 if (offset == -1)
2352 {
2353 error(loc, "Offset overlapping", "atomic counter");
2354 return;
2355 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002356 layoutQualifier.offset = offset;
2357 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002358}
2359
Olli Etuaho454c34c2017-10-25 16:35:56 +03002360void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2361 const char *token,
2362 TType *type)
2363{
2364 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2365 {
2366 if (type->isArray() && type->getOutermostArraySize() == 0u)
2367 {
2368 // Set size for the unsized geometry shader inputs if they are declared after a valid
2369 // input primitive declaration.
2370 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2371 {
2372 ASSERT(mGeometryShaderInputArraySize > 0u);
2373 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2374 }
2375 else
2376 {
2377 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2378 // An input can be declared without an array size if there is a previous layout
2379 // which specifies the size.
2380 error(location,
2381 "Missing a valid input primitive declaration before declaring an unsized "
2382 "array input",
2383 token);
2384 }
2385 }
2386 else if (type->isArray())
2387 {
2388 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2389 }
2390 else
2391 {
2392 error(location, "Geometry shader input variable must be declared as an array", token);
2393 }
2394 }
2395}
2396
Olli Etuaho13389b62016-10-16 11:48:18 +01002397TIntermDeclaration *TParseContext::parseSingleDeclaration(
2398 TPublicType &publicType,
2399 const TSourceLoc &identifierOrTypeLocation,
2400 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002401{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002402 TType type(publicType);
2403 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2404 mDirectiveHandler.pragma().stdgl.invariantAll)
2405 {
2406 TQualifier qualifier = type.getQualifier();
2407
2408 // The directive handler has already taken care of rejecting invalid uses of this pragma
2409 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2410 // affected variable declarations:
2411 //
2412 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2413 // elsewhere, in TranslatorGLSL.)
2414 //
2415 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2416 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2417 // the way this is currently implemented we have to enable this compiler option before
2418 // parsing the shader and determining the shading language version it uses. If this were
2419 // implemented as a post-pass, the workaround could be more targeted.
2420 //
2421 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2422 // the specification, but there are desktop OpenGL drivers that expect that this is the
2423 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2424 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2425 {
2426 type.setInvariant(true);
2427 }
2428 }
2429
Olli Etuaho454c34c2017-10-25 16:35:56 +03002430 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002431
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002432 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2433 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002434
Olli Etuahobab4c082015-04-24 16:38:49 +03002435 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002436 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002437
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002439 if (emptyDeclaration)
2440 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002441 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002442 // In most cases we don't need to create a symbol node for an empty declaration.
2443 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2444 if (type.getBasicType() == EbtStruct)
2445 {
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002446 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 }
jchen104cdac9e2017-05-08 11:01:20 +08002448 else if (IsAtomicCounter(publicType.getBasicType()))
2449 {
2450 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2451 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002452 }
2453 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002454 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002455 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuaho55bde912017-10-25 13:41:13 +03002457 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002458
Olli Etuaho55bc9052017-10-25 17:33:06 +03002459 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002460
Olli Etuaho2935c582015-04-08 14:32:06 +03002461 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002462 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002463
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002464 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002465 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002466 symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002467 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002468 }
2469
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002470 TIntermDeclaration *declaration = new TIntermDeclaration();
2471 declaration->setLine(identifierOrTypeLocation);
2472 if (symbol)
2473 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002474 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002475 declaration->appendDeclarator(symbol);
2476 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002477 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002478}
2479
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002480TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2481 TPublicType &elementType,
2482 const TSourceLoc &identifierLocation,
2483 const TString &identifier,
2484 const TSourceLoc &indexLocation,
2485 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002486{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002487 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002488
Olli Etuaho55bde912017-10-25 13:41:13 +03002489 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 identifierLocation);
2491
Olli Etuaho55bde912017-10-25 13:41:13 +03002492 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002493
Olli Etuaho55bde912017-10-25 13:41:13 +03002494 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002495
Olli Etuaho55bde912017-10-25 13:41:13 +03002496 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002497 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002498
Olli Etuaho454c34c2017-10-25 16:35:56 +03002499 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002500
2501 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2502
Olli Etuaho55bc9052017-10-25 17:33:06 +03002503 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002504
Olli Etuaho2935c582015-04-08 14:32:06 +03002505 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002506 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002507
Olli Etuaho13389b62016-10-16 11:48:18 +01002508 TIntermDeclaration *declaration = new TIntermDeclaration();
2509 declaration->setLine(identifierLocation);
2510
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002511 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002512 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002513 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002514 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 declaration->appendDeclarator(symbol);
2516 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002517
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002519}
2520
Olli Etuaho13389b62016-10-16 11:48:18 +01002521TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2522 const TSourceLoc &identifierLocation,
2523 const TString &identifier,
2524 const TSourceLoc &initLocation,
2525 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002526{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002527 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002528
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002529 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2530 identifierLocation);
2531
2532 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002533
Olli Etuaho13389b62016-10-16 11:48:18 +01002534 TIntermDeclaration *declaration = new TIntermDeclaration();
2535 declaration->setLine(identifierLocation);
2536
2537 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002538 TType type(publicType);
2539 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002540 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002541 if (initNode)
2542 {
2543 declaration->appendDeclarator(initNode);
2544 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002545 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002546 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002547}
2548
Olli Etuaho13389b62016-10-16 11:48:18 +01002549TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002550 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002551 const TSourceLoc &identifierLocation,
2552 const TString &identifier,
2553 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002554 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 const TSourceLoc &initLocation,
2556 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002557{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002558 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002559
Olli Etuaho55bde912017-10-25 13:41:13 +03002560 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002561 identifierLocation);
2562
Olli Etuaho55bde912017-10-25 13:41:13 +03002563 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564
Olli Etuaho55bde912017-10-25 13:41:13 +03002565 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002566
Olli Etuaho55bde912017-10-25 13:41:13 +03002567 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002568 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002569
Olli Etuaho13389b62016-10-16 11:48:18 +01002570 TIntermDeclaration *declaration = new TIntermDeclaration();
2571 declaration->setLine(identifierLocation);
2572
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002573 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002574 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002575 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002576 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002577 if (initNode)
2578 {
2579 declaration->appendDeclarator(initNode);
2580 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002582
2583 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002584}
2585
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002586TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002587 const TTypeQualifierBuilder &typeQualifierBuilder,
2588 const TSourceLoc &identifierLoc,
2589 const TString *identifier,
2590 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002591{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002592 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002593
Martin Radev70866b82016-07-22 15:27:42 +03002594 if (!typeQualifier.invariant)
2595 {
2596 error(identifierLoc, "Expected invariant", identifier->c_str());
2597 return nullptr;
2598 }
2599 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2600 {
2601 return nullptr;
2602 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603 if (!symbol)
2604 {
2605 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002606 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002607 }
Martin Radev70866b82016-07-22 15:27:42 +03002608 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002609 {
Martin Radev70866b82016-07-22 15:27:42 +03002610 error(identifierLoc, "invariant declaration specifies qualifier",
2611 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002612 }
Martin Radev70866b82016-07-22 15:27:42 +03002613 if (typeQualifier.precision != EbpUndefined)
2614 {
2615 error(identifierLoc, "invariant declaration specifies precision",
2616 getPrecisionString(typeQualifier.precision));
2617 }
2618 if (!typeQualifier.layoutQualifier.isEmpty())
2619 {
2620 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2621 }
2622
2623 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002624 if (!variable)
2625 {
2626 return nullptr;
2627 }
Martin Radev70866b82016-07-22 15:27:42 +03002628 const TType &type = variable->getType();
2629
2630 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2631 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002632 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002633
2634 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2635
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002636 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->uniqueId(), *identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002637 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002638
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002639 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002640}
2641
Olli Etuaho13389b62016-10-16 11:48:18 +01002642void TParseContext::parseDeclarator(TPublicType &publicType,
2643 const TSourceLoc &identifierLocation,
2644 const TString &identifier,
2645 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002646{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002647 // If the declaration starting this declarator list was empty (example: int,), some checks were
2648 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002649 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002650 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002651 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2652 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002653 }
2654
Olli Etuaho856c4972016-08-08 11:38:39 +03002655 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002656
Olli Etuaho2935c582015-04-08 14:32:06 +03002657 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002658 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002659
2660 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2661
Olli Etuaho55bde912017-10-25 13:41:13 +03002662 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2663
Olli Etuaho55bc9052017-10-25 17:33:06 +03002664 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2665
Olli Etuaho43364892017-02-13 16:00:12 +00002666 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002667
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002668 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002670 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, type);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002671 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002672 declarationOut->appendDeclarator(symbol);
2673 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002674}
2675
Olli Etuaho55bde912017-10-25 13:41:13 +03002676void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002677 const TSourceLoc &identifierLocation,
2678 const TString &identifier,
2679 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002680 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002681 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002682{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002683 // If the declaration starting this declarator list was empty (example: int,), some checks were
2684 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002685 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002686 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002687 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002688 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002689 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002690
Olli Etuaho55bde912017-10-25 13:41:13 +03002691 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002692
Olli Etuaho55bde912017-10-25 13:41:13 +03002693 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002694 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002695 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002696 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002697
Olli Etuaho454c34c2017-10-25 16:35:56 +03002698 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2699
Olli Etuaho55bde912017-10-25 13:41:13 +03002700 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2701
Olli Etuaho55bc9052017-10-25 17:33:06 +03002702 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002703
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002704 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002705 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002706
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002707 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002708 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02002709 TIntermSymbol *symbol = new TIntermSymbol(variable->uniqueId(), identifier, arrayType);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002710 symbol->setLine(identifierLocation);
2711 declarationOut->appendDeclarator(symbol);
2712 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002713 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002714}
2715
Olli Etuaho13389b62016-10-16 11:48:18 +01002716void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2717 const TSourceLoc &identifierLocation,
2718 const TString &identifier,
2719 const TSourceLoc &initLocation,
2720 TIntermTyped *initializer,
2721 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002722{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002723 // If the declaration starting this declarator list was empty (example: int,), some checks were
2724 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002725 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002726 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002727 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2728 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002729 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002730
Olli Etuaho856c4972016-08-08 11:38:39 +03002731 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002732
Olli Etuaho13389b62016-10-16 11:48:18 +01002733 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002734 TType type(publicType);
2735 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 {
2737 //
2738 // build the intermediate representation
2739 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002740 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002741 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002742 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002743 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002744 }
2745}
2746
Olli Etuaho55bde912017-10-25 13:41:13 +03002747void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002748 const TSourceLoc &identifierLocation,
2749 const TString &identifier,
2750 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002751 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002752 const TSourceLoc &initLocation,
2753 TIntermTyped *initializer,
2754 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002755{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002756 // If the declaration starting this declarator list was empty (example: int,), some checks were
2757 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002758 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002759 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002760 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002761 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762 }
2763
Olli Etuaho55bde912017-10-25 13:41:13 +03002764 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765
Olli Etuaho55bde912017-10-25 13:41:13 +03002766 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002767
Olli Etuaho55bde912017-10-25 13:41:13 +03002768 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002769 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002770
2771 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002772 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002773 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774 {
2775 if (initNode)
2776 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002777 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002778 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002779 }
2780}
2781
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002782TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2783{
2784 // It's simpler to parse an empty statement as a constant expression rather than having a
2785 // different type of node just for empty statements, that will be pruned from the AST anyway.
2786 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2787 node->setLine(location);
2788 return node;
2789}
2790
jchen104cdac9e2017-05-08 11:01:20 +08002791void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2792 const TSourceLoc &location)
2793{
2794 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2795 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2796 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2797 {
2798 error(location, "Requires both binding and offset", "layout");
2799 return;
2800 }
2801 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2802}
2803
Olli Etuahocce89652017-06-19 16:04:09 +03002804void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2805 const TPublicType &type,
2806 const TSourceLoc &loc)
2807{
2808 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2809 !getFragmentPrecisionHigh())
2810 {
2811 error(loc, "precision is not supported in fragment shader", "highp");
2812 }
2813
2814 if (!CanSetDefaultPrecisionOnType(type))
2815 {
2816 error(loc, "illegal type argument for default precision qualifier",
2817 getBasicString(type.getBasicType()));
2818 return;
2819 }
2820 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2821}
2822
Shaob5cc1192017-07-06 10:47:20 +08002823bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2824{
2825 switch (typeQualifier.layoutQualifier.primitiveType)
2826 {
2827 case EptLines:
2828 case EptLinesAdjacency:
2829 case EptTriangles:
2830 case EptTrianglesAdjacency:
2831 return typeQualifier.qualifier == EvqGeometryIn;
2832
2833 case EptLineStrip:
2834 case EptTriangleStrip:
2835 return typeQualifier.qualifier == EvqGeometryOut;
2836
2837 case EptPoints:
2838 return true;
2839
2840 default:
2841 UNREACHABLE();
2842 return false;
2843 }
2844}
2845
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002846void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2847 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002848{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002849 if (mGeometryShaderInputArraySize == 0u)
2850 {
2851 mGeometryShaderInputArraySize = inputArraySize;
2852 }
2853 else if (mGeometryShaderInputArraySize != inputArraySize)
2854 {
2855 error(line,
2856 "Array size or input primitive declaration doesn't match the size of earlier sized "
2857 "array inputs.",
2858 "layout");
2859 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002860}
2861
Shaob5cc1192017-07-06 10:47:20 +08002862bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2863{
2864 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2865
2866 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2867
2868 if (layoutQualifier.maxVertices != -1)
2869 {
2870 error(typeQualifier.line,
2871 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2872 return false;
2873 }
2874
2875 // Set mGeometryInputPrimitiveType if exists
2876 if (layoutQualifier.primitiveType != EptUndefined)
2877 {
2878 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2879 {
2880 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2881 return false;
2882 }
2883
2884 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2885 {
2886 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002887 setGeometryShaderInputArraySize(
2888 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2889 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002890 }
2891 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2892 {
2893 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2894 "layout");
2895 return false;
2896 }
2897 }
2898
2899 // Set mGeometryInvocations if exists
2900 if (layoutQualifier.invocations > 0)
2901 {
2902 if (mGeometryShaderInvocations == 0)
2903 {
2904 mGeometryShaderInvocations = layoutQualifier.invocations;
2905 }
2906 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2907 {
2908 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2909 "layout");
2910 return false;
2911 }
2912 }
2913
2914 return true;
2915}
2916
2917bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2918{
2919 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2920
2921 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2922
2923 if (layoutQualifier.invocations > 0)
2924 {
2925 error(typeQualifier.line,
2926 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2927 return false;
2928 }
2929
2930 // Set mGeometryOutputPrimitiveType if exists
2931 if (layoutQualifier.primitiveType != EptUndefined)
2932 {
2933 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2934 {
2935 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2936 return false;
2937 }
2938
2939 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2940 {
2941 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2942 }
2943 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2944 {
2945 error(typeQualifier.line,
2946 "primitive doesn't match earlier output primitive declaration", "layout");
2947 return false;
2948 }
2949 }
2950
2951 // Set mGeometryMaxVertices if exists
2952 if (layoutQualifier.maxVertices > -1)
2953 {
2954 if (mGeometryShaderMaxVertices == -1)
2955 {
2956 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2957 }
2958 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2959 {
2960 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2961 "layout");
2962 return false;
2963 }
2964 }
2965
2966 return true;
2967}
2968
Martin Radev70866b82016-07-22 15:27:42 +03002969void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002970{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002971 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002972 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973
Martin Radev70866b82016-07-22 15:27:42 +03002974 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2975 typeQualifier.line);
2976
Jamie Madillc2128ff2016-07-04 10:26:17 -04002977 // It should never be the case, but some strange parser errors can send us here.
2978 if (layoutQualifier.isEmpty())
2979 {
2980 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002981 return;
2982 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002983
Martin Radev802abe02016-08-04 17:48:32 +03002984 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002985 {
Olli Etuaho43364892017-02-13 16:00:12 +00002986 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002987 return;
2988 }
2989
Olli Etuaho43364892017-02-13 16:00:12 +00002990 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2991
2992 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002993
2994 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2995
Andrei Volykhina5527072017-03-22 16:46:30 +03002996 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2997
jchen104cdac9e2017-05-08 11:01:20 +08002998 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2999
Qin Jiajiaca68d982017-09-18 16:41:56 +08003000 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
3001 typeQualifier.qualifier);
3002
Martin Radev802abe02016-08-04 17:48:32 +03003003 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003004 {
Martin Radev802abe02016-08-04 17:48:32 +03003005 if (mComputeShaderLocalSizeDeclared &&
3006 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3007 {
3008 error(typeQualifier.line, "Work group size does not match the previous declaration",
3009 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003010 return;
3011 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003012
Martin Radev802abe02016-08-04 17:48:32 +03003013 if (mShaderVersion < 310)
3014 {
3015 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003016 return;
3017 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003018
Martin Radev4c4c8e72016-08-04 12:25:34 +03003019 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003020 {
3021 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003022 return;
3023 }
3024
3025 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3026 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3027
3028 const TConstantUnion *maxComputeWorkGroupSizeData =
3029 maxComputeWorkGroupSize->getConstPointer();
3030
3031 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3032 {
3033 if (layoutQualifier.localSize[i] != -1)
3034 {
3035 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3036 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3037 if (mComputeShaderLocalSize[i] < 1 ||
3038 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 std::stringstream reasonStream;
3041 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3042 << maxComputeWorkGroupSizeValue;
3043 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003044
Olli Etuaho4de340a2016-12-16 09:32:03 +00003045 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003046 return;
3047 }
3048 }
3049 }
3050
3051 mComputeShaderLocalSizeDeclared = true;
3052 }
Shaob5cc1192017-07-06 10:47:20 +08003053 else if (typeQualifier.qualifier == EvqGeometryIn)
3054 {
3055 if (mShaderVersion < 310)
3056 {
3057 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3058 return;
3059 }
3060
3061 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3062 {
3063 return;
3064 }
3065 }
3066 else if (typeQualifier.qualifier == EvqGeometryOut)
3067 {
3068 if (mShaderVersion < 310)
3069 {
3070 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3071 "layout");
3072 return;
3073 }
3074
3075 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3076 {
3077 return;
3078 }
3079 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003080 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3081 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003082 {
3083 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3084 // specification.
3085 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3086 {
3087 error(typeQualifier.line, "Number of views does not match the previous declaration",
3088 "layout");
3089 return;
3090 }
3091
3092 if (layoutQualifier.numViews == -1)
3093 {
3094 error(typeQualifier.line, "No num_views specified", "layout");
3095 return;
3096 }
3097
3098 if (layoutQualifier.numViews > mMaxNumViews)
3099 {
3100 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3101 "layout");
3102 return;
3103 }
3104
3105 mNumViews = layoutQualifier.numViews;
3106 }
Martin Radev802abe02016-08-04 17:48:32 +03003107 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003108 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003109 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003110 {
Martin Radev802abe02016-08-04 17:48:32 +03003111 return;
3112 }
3113
Jiajia Qinbc585152017-06-23 15:42:17 +08003114 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003115 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003116 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003117 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003118 return;
3119 }
3120
3121 if (mShaderVersion < 300)
3122 {
3123 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3124 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003125 return;
3126 }
3127
Olli Etuaho09b04a22016-12-15 13:30:26 +00003128 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003129
3130 if (layoutQualifier.matrixPacking != EmpUnspecified)
3131 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003132 if (typeQualifier.qualifier == EvqUniform)
3133 {
3134 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3135 }
3136 else if (typeQualifier.qualifier == EvqBuffer)
3137 {
3138 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3139 }
Martin Radev802abe02016-08-04 17:48:32 +03003140 }
3141
3142 if (layoutQualifier.blockStorage != EbsUnspecified)
3143 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003144 if (typeQualifier.qualifier == EvqUniform)
3145 {
3146 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3147 }
3148 else if (typeQualifier.qualifier == EvqBuffer)
3149 {
3150 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3151 }
Martin Radev802abe02016-08-04 17:48:32 +03003152 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003153 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003154}
3155
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003156TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3157 const TFunction &function,
3158 const TSourceLoc &location,
3159 bool insertParametersToSymbolTable)
3160{
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003161 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003162
Olli Etuahofe486322017-03-21 09:30:54 +00003163 TIntermFunctionPrototype *prototype =
3164 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003165 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3166 // point to the data that already exists in the symbol table.
3167 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3168 prototype->setLine(location);
3169
3170 for (size_t i = 0; i < function.getParamCount(); i++)
3171 {
3172 const TConstParameter &param = function.getParam(i);
3173
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003174 TIntermSymbol *symbol = nullptr;
3175
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003176 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3177 // be used for unused args).
3178 if (param.name != nullptr)
3179 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003180 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003183 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3184 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003185 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003186 symbol = new TIntermSymbol(variable->uniqueId(), variable->name(),
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003187 variable->getType());
3188 }
3189 else
3190 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003191 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003192 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003193 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003194 // Unsized type of a named parameter should have already been checked and sanitized.
3195 ASSERT(!param.type->isUnsizedArray());
3196 }
3197 else
3198 {
3199 if (param.type->isUnsizedArray())
3200 {
3201 error(location, "function parameter array must be sized at compile time", "[]");
3202 // We don't need to size the arrays since the parameter is unnamed and hence
3203 // inaccessible.
3204 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003206 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003207 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003208 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3209 // symbol.
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003210 symbol = new TIntermSymbol(symbolTable.getEmptySymbolId(), "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003211 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003212 symbol->setLine(location);
3213 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003214 }
3215 return prototype;
3216}
3217
Olli Etuaho16c745a2017-01-16 17:02:27 +00003218TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3219 const TFunction &parsedFunction,
3220 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003221{
Olli Etuaho476197f2016-10-11 13:59:08 +01003222 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3223 // first declaration. Either way the instance in the symbol table is used to track whether the
3224 // function is declared multiple times.
3225 TFunction *function = static_cast<TFunction *>(
3226 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3227 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003228 {
3229 // ESSL 1.00.17 section 4.2.7.
3230 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3231 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003232 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003233 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003234
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003235 TIntermFunctionPrototype *prototype =
3236 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003237
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003238 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003239
3240 if (!symbolTable.atGlobalLevel())
3241 {
3242 // ESSL 3.00.4 section 4.2.4.
3243 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003244 }
3245
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 return prototype;
3247}
3248
Olli Etuaho336b1472016-10-05 16:37:55 +01003249TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003250 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003251 TIntermBlock *functionBody,
3252 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003253{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003254 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003255 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3256 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003257 error(location, "function does not return a value:",
3258 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003259 }
3260
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261 if (functionBody == nullptr)
3262 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003263 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003264 functionBody->setLine(location);
3265 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003266 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003267 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003268 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003269
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003270 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003271 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003272}
3273
Olli Etuaho476197f2016-10-11 13:59:08 +01003274void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3275 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003276 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003277{
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 ASSERT(function);
3279 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003280 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003281 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003282
3283 if (builtIn)
3284 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003285 error(location, "built-in functions cannot be redefined", (*function)->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003286 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003287 else
Jamie Madill185fb402015-06-12 15:48:48 -04003288 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003289 TFunction *prevDec = static_cast<TFunction *>(
3290 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3291
3292 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3293 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3294 // occurance.
3295 if (*function != prevDec)
3296 {
3297 // Swap the parameters of the previous declaration to the parameters of the function
3298 // definition (parameter names may differ).
3299 prevDec->swapParameters(**function);
3300
3301 // The function definition will share the same symbol as any previous declaration.
3302 *function = prevDec;
3303 }
3304
3305 if ((*function)->isDefined())
3306 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003307 error(location, "function already has a body", (*function)->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003308 }
3309
3310 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003311 }
Jamie Madill185fb402015-06-12 15:48:48 -04003312
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003313 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003314 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003315 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003316
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003317 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003318 setLoopNestingLevel(0);
3319}
3320
Jamie Madillb98c3a82015-07-23 14:26:04 -04003321TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003322{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003323 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003324 // We don't know at this point whether this is a function definition or a prototype.
3325 // The definition production code will check for redefinitions.
3326 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003327 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003328 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3329 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003330 //
3331 TFunction *prevDec =
3332 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303333
Olli Etuahod80f2942017-11-06 12:44:45 +02003334 for (size_t i = 0u; i < function->getParamCount(); ++i)
3335 {
3336 auto &param = function->getParam(i);
3337 if (param.type->isStructSpecifier())
3338 {
3339 // ESSL 3.00.6 section 12.10.
3340 error(location, "Function parameter type cannot be a structure definition",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003341 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003342 }
3343 }
3344
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003345 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
3346 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303347 {
Martin Radevda6254b2016-12-14 17:00:36 +02003348 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303349 // Therefore overloading or redefining builtin functions is an error.
3350 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003351 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303352 }
3353 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003354 {
3355 if (prevDec->getReturnType() != function->getReturnType())
3356 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003357 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003358 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003359 }
3360 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003362 if (prevDec->getParam(i).type->getQualifier() !=
3363 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003364 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003365 error(location,
3366 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003367 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003368 }
3369 }
3370 }
3371
3372 //
3373 // Check for previously declared variables using the same name.
3374 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003375 TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003376 if (prevSym)
3377 {
3378 if (!prevSym->isFunction())
3379 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003380 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
3382 }
3383 else
3384 {
3385 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003386 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003387 }
3388
3389 // We're at the inner scope level of the function's arguments and body statement.
3390 // Add the function prototype to the surrounding scope instead.
3391 symbolTable.getOuterLevel()->insert(function);
3392
Olli Etuaho78d13742017-01-18 13:06:10 +00003393 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003394 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003395 {
3396 if (function->getParamCount() > 0)
3397 {
3398 error(location, "function cannot take any parameter(s)", "main");
3399 }
3400 if (function->getReturnType().getBasicType() != EbtVoid)
3401 {
3402 error(location, "main function cannot return a value",
3403 function->getReturnType().getBasicString());
3404 }
3405 }
3406
Jamie Madill185fb402015-06-12 15:48:48 -04003407 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003408 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3409 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003410 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3411 //
3412 return function;
3413}
3414
Olli Etuaho9de84a52016-06-14 17:36:01 +03003415TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3416 const TString *name,
3417 const TSourceLoc &location)
3418{
3419 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3420 {
3421 error(location, "no qualifiers allowed for function return",
3422 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003423 }
3424 if (!type.layoutQualifier.isEmpty())
3425 {
3426 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003427 }
jchen10cc2a10e2017-05-03 14:05:12 +08003428 // make sure an opaque type is not involved as well...
3429 std::string reason(getBasicString(type.getBasicType()));
3430 reason += "s can't be function return values";
3431 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003432 if (mShaderVersion < 300)
3433 {
3434 // Array return values are forbidden, but there's also no valid syntax for declaring array
3435 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003436 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003437
3438 if (type.isStructureContainingArrays())
3439 {
3440 // ESSL 1.00.17 section 6.1 Function Definitions
3441 error(location, "structures containing arrays can't be function return values",
3442 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003443 }
3444 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003445
3446 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003447 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003448}
3449
Olli Etuahocce89652017-06-19 16:04:09 +03003450TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3451{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003452 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003453 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3454 // really only interested in the mangled name of the function to look up the actual function
3455 // from the symbol table. If we just had the name string and the types of the parameters that
3456 // would be enough, but TFunction carries a lot of extra information in addition to that.
3457 // Besides function calls we do have to store constructor calls in the same data structure, for
3458 // them we need to store a TType.
3459 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved);
Olli Etuahocce89652017-06-19 16:04:09 +03003460}
3461
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003462TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003463{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003464 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003465 {
3466 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3467 "[]");
3468 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003469 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003470 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003471 error(publicType.getLine(), "constructor can't be a structure definition",
3472 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003473 }
3474
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003475 TType *type = new TType(publicType);
3476 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003477 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003478 error(publicType.getLine(), "cannot construct this type",
3479 getBasicString(publicType.getBasicType()));
3480 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003481 }
3482
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003483 return new TFunction(&symbolTable, nullptr, type, SymbolType::BuiltIn, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003484}
3485
Olli Etuaho55bde912017-10-25 13:41:13 +03003486void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3487 const char *errorMessage,
3488 const char *token,
3489 TType *arrayType)
3490{
3491 if (arrayType->isUnsizedArray())
3492 {
3493 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003494 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003495 }
3496}
3497
3498TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003499 const TString *name,
3500 const TSourceLoc &nameLoc)
3501{
Olli Etuaho55bde912017-10-25 13:41:13 +03003502 ASSERT(type);
3503 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3504 type);
3505 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003506 {
3507 error(nameLoc, "illegal use of type 'void'", name->c_str());
3508 }
3509 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003510 TParameter param = {name, type};
3511 return param;
3512}
3513
Olli Etuaho55bde912017-10-25 13:41:13 +03003514TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3515 const TString *name,
3516 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003517{
Olli Etuaho55bde912017-10-25 13:41:13 +03003518 TType *type = new TType(publicType);
3519 return parseParameterDeclarator(type, name, nameLoc);
3520}
3521
3522TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3523 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003524 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003525 const TSourceLoc &arrayLoc,
3526 TPublicType *elementType)
3527{
3528 checkArrayElementIsNotArray(arrayLoc, *elementType);
3529 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003530 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003531 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003532}
3533
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003534bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3535 TType type,
3536 const TSourceLoc &line)
3537{
3538 if (arguments->empty())
3539 {
3540 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3541 return false;
3542 }
3543 for (TIntermNode *arg : *arguments)
3544 {
3545 TIntermTyped *element = arg->getAsTyped();
3546 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003547 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3548 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003549 {
3550 error(line, "constructing from a non-dereferenced array", "constructor");
3551 return false;
3552 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003553 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003554 {
3555 if (dimensionalityFromElement == 1u)
3556 {
3557 error(line, "implicitly sized array of arrays constructor argument is not an array",
3558 "constructor");
3559 }
3560 else
3561 {
3562 error(line,
3563 "implicitly sized array of arrays constructor argument dimensionality is too "
3564 "low",
3565 "constructor");
3566 }
3567 return false;
3568 }
3569 }
3570 return true;
3571}
3572
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573// This function is used to test for the correctness of the parameters passed to various constructor
3574// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003575//
Olli Etuaho856c4972016-08-08 11:38:39 +03003576// 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 +00003577//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003578TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003579 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303580 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003581{
Olli Etuaho856c4972016-08-08 11:38:39 +03003582 if (type.isUnsizedArray())
3583 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003584 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003585 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003586 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003587 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003588 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003589 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3590 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003591 if (type.getOutermostArraySize() == 0u)
3592 {
3593 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3594 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003595 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003596 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003597 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003598 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003599 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003600 }
3601 }
3602 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003603 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003604
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003605 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003606 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003607 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003608 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003609
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003610 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003611 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003612
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003613 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3614 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003615 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003616 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003617 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003618 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003619}
3620
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003621//
3622// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003623// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003624//
Olli Etuaho13389b62016-10-16 11:48:18 +01003625TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003626 const TTypeQualifierBuilder &typeQualifierBuilder,
3627 const TSourceLoc &nameLine,
3628 const TString &blockName,
3629 TFieldList *fieldList,
3630 const TString *instanceName,
3631 const TSourceLoc &instanceLine,
3632 TIntermTyped *arrayIndex,
3633 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634{
Olli Etuaho856c4972016-08-08 11:38:39 +03003635 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003636
Olli Etuaho77ba4082016-12-16 12:01:18 +00003637 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003638
Jiajia Qinbc585152017-06-23 15:42:17 +08003639 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003640 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 error(typeQualifier.line,
3642 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3643 "3.10",
3644 getQualifierString(typeQualifier.qualifier));
3645 }
3646 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3647 {
3648 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003649 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003650 }
3651
Martin Radev70866b82016-07-22 15:27:42 +03003652 if (typeQualifier.invariant)
3653 {
3654 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3655 }
3656
Jiajia Qinbc585152017-06-23 15:42:17 +08003657 if (typeQualifier.qualifier != EvqBuffer)
3658 {
3659 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3660 }
Olli Etuaho43364892017-02-13 16:00:12 +00003661
jchen10af713a22017-04-19 09:10:56 +08003662 // add array index
3663 unsigned int arraySize = 0;
3664 if (arrayIndex != nullptr)
3665 {
3666 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3667 }
3668
3669 if (mShaderVersion < 310)
3670 {
3671 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3672 }
3673 else
3674 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003675 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3676 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003677 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003678
Andrei Volykhina5527072017-03-22 16:46:30 +03003679 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3680
Jamie Madill099c0f32013-06-20 11:55:52 -04003681 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003682 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003683 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3684 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003685
Jamie Madill099c0f32013-06-20 11:55:52 -04003686 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3687 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003688 if (typeQualifier.qualifier == EvqUniform)
3689 {
3690 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3691 }
3692 else if (typeQualifier.qualifier == EvqBuffer)
3693 {
3694 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3695 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003696 }
3697
Jamie Madill1566ef72013-06-20 11:55:54 -04003698 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3699 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003700 if (typeQualifier.qualifier == EvqUniform)
3701 {
3702 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3703 }
3704 else if (typeQualifier.qualifier == EvqBuffer)
3705 {
3706 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3707 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003708 }
3709
Olli Etuaho856c4972016-08-08 11:38:39 +03003710 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003711
Martin Radev2cc85b32016-08-05 16:22:53 +03003712 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3713
Jamie Madill98493dd2013-07-08 14:39:03 -04003714 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3716 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303718 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003719 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303720 {
jchen10cc2a10e2017-05-03 14:05:12 +08003721 std::string reason("unsupported type - ");
3722 reason += fieldType->getBasicString();
3723 reason += " types are not allowed in interface blocks";
3724 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003725 }
3726
Jamie Madill98493dd2013-07-08 14:39:03 -04003727 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003728 switch (qualifier)
3729 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003731 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003732 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003733 if (typeQualifier.qualifier == EvqBuffer)
3734 {
3735 error(field->line(), "invalid qualifier on shader storage block member",
3736 getQualifierString(qualifier));
3737 }
3738 break;
3739 case EvqBuffer:
3740 if (typeQualifier.qualifier == EvqUniform)
3741 {
3742 error(field->line(), "invalid qualifier on uniform block member",
3743 getQualifierString(qualifier));
3744 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 break;
3746 default:
3747 error(field->line(), "invalid qualifier on interface block member",
3748 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003749 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003750 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003751
Martin Radev70866b82016-07-22 15:27:42 +03003752 if (fieldType->isInvariant())
3753 {
3754 error(field->line(), "invalid qualifier on interface block member", "invariant");
3755 }
3756
Jamie Madilla5efff92013-06-06 11:56:47 -04003757 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003758 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003759 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003760 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003761
Jamie Madill98493dd2013-07-08 14:39:03 -04003762 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003763 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003764 error(field->line(), "invalid layout qualifier: cannot be used here",
3765 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003766 }
3767
Jamie Madill98493dd2013-07-08 14:39:03 -04003768 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003769 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003770 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003771 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003772 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003773 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003774 warning(field->line(),
3775 "extraneous layout qualifier: only has an effect on matrix types",
3776 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003777 }
3778
Jamie Madill98493dd2013-07-08 14:39:03 -04003779 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003780
Olli Etuahoebee5b32017-11-23 12:56:32 +02003781 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3782 typeQualifier.qualifier != EvqBuffer)
3783 {
3784 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3785 checkIsNotUnsizedArray(field->line(),
3786 "array members of interface blocks must specify a size",
3787 field->name().c_str(), field->type());
3788 }
3789
Jiajia Qinbc585152017-06-23 15:42:17 +08003790 if (typeQualifier.qualifier == EvqBuffer)
3791 {
3792 // set memory qualifiers
3793 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3794 // qualified with a memory qualifier, it is as if all of its members were declared with
3795 // the same memory qualifier.
3796 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3797 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3798 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3799 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3800 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3801 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3802 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3803 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3804 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3805 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3806 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003807 }
3808
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003809 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3810 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003811 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3812 {
3813 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3814 }
3815
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003816 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3817 if (arrayIndex != nullptr)
3818 {
3819 interfaceBlockType.makeArray(arraySize);
3820 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003821
3822 TString symbolName = "";
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003823 const TSymbolUniqueId *symbolId = nullptr;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003824
Jamie Madill98493dd2013-07-08 14:39:03 -04003825 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003826 {
3827 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003828 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3829 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003830 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303831 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003832
3833 // set parent pointer of the field variable
3834 fieldType->setInterfaceBlock(interfaceBlock);
3835
Olli Etuaho0f684632017-07-13 12:42:15 +03003836 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003837
Olli Etuaho0f684632017-07-13 12:42:15 +03003838 if (fieldVariable)
3839 {
3840 fieldVariable->setQualifier(typeQualifier.qualifier);
3841 }
3842 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303843 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003844 error(field->line(), "redefinition of an interface block member name",
3845 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003846 }
3847 }
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003848 symbolId = &symbolTable.getEmptySymbolId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003849 }
3850 else
3851 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003852 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003853
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003854 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003855 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3856 if (instanceTypeDef)
3857 {
3858 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003859 symbolId = &instanceTypeDef->uniqueId();
Olli Etuaho0f684632017-07-13 12:42:15 +03003860 }
3861 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303862 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003863 error(instanceLine, "redefinition of an interface block instance name",
3864 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003865 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003866 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003867 }
3868
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03003869 TIntermDeclaration *declaration = nullptr;
3870
3871 if (symbolId)
3872 {
3873 TIntermSymbol *blockSymbol = new TIntermSymbol(*symbolId, symbolName, interfaceBlockType);
3874 blockSymbol->setLine(typeQualifier.line);
3875 declaration = new TIntermDeclaration();
3876 declaration->appendDeclarator(blockSymbol);
3877 declaration->setLine(nameLine);
3878 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003879
3880 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003881 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003882}
3883
Olli Etuaho383b7912016-08-05 11:22:59 +03003884void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003885{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003886 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887
3888 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003889 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303890 if (mStructNestingLevel > 1)
3891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003893 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003894}
3895
3896void TParseContext::exitStructDeclaration()
3897{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003898 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003899}
3900
Olli Etuaho8a176262016-08-16 14:23:01 +03003901void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003902{
Jamie Madillacb4b812016-11-07 13:50:29 -05003903 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303904 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003905 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003906 }
3907
Arun Patole7e7e68d2015-05-22 12:02:25 +05303908 if (field.type()->getBasicType() != EbtStruct)
3909 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003910 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003911 }
3912
3913 // We're already inside a structure definition at this point, so add
3914 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303915 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3916 {
Jamie Madill41a49272014-03-18 16:10:13 -04003917 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003918 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3919 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003920 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003921 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003922 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003923 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003924}
3925
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003926//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927// Parse an array index expression
3928//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003929TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3930 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303931 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3934 {
3935 if (baseExpression->getAsSymbolNode())
3936 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303937 error(location, " left of '[' is not of type array, matrix, or vector ",
3938 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939 }
3940 else
3941 {
3942 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3943 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003944
Olli Etuaho3ec75682017-07-05 17:02:55 +03003945 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003946 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003947
Jiawei Shaod8105a02017-08-08 09:54:36 +08003948 if (baseExpression->getQualifier() == EvqPerVertexIn)
3949 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003950 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003951 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3952 {
3953 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3954 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3955 }
3956 }
3957
Jamie Madill21c1e452014-12-29 11:33:41 -05003958 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3959
Olli Etuaho36b05142015-11-12 13:10:42 +02003960 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3961 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3962 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3963 // index is a constant expression.
3964 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3965 {
3966 if (baseExpression->isInterfaceBlock())
3967 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003968 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003969 switch (baseExpression->getQualifier())
3970 {
3971 case EvqPerVertexIn:
3972 break;
3973 case EvqUniform:
3974 case EvqBuffer:
3975 error(location,
3976 "array indexes for uniform block arrays and shader storage block arrays "
3977 "must be constant integral expressions",
3978 "[");
3979 break;
3980 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003981 // We can reach here only in error cases.
3982 ASSERT(mDiagnostics->numErrors() > 0);
3983 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003984 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003985 }
3986 else if (baseExpression->getQualifier() == EvqFragmentOut)
3987 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003988 error(location,
3989 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003990 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003991 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3992 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003993 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003994 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003995 }
3996
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003997 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003998 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003999 // If an out-of-range index is not qualified as constant, the behavior in the spec is
4000 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
4001 // constant fold expressions that are not constant expressions). The most compatible way to
4002 // handle this case is to report a warning instead of an error and force the index to be in
4003 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004004 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03004005 int index = 0;
4006 if (indexConstantUnion->getBasicType() == EbtInt)
4007 {
4008 index = indexConstantUnion->getIConst(0);
4009 }
4010 else if (indexConstantUnion->getBasicType() == EbtUInt)
4011 {
4012 index = static_cast<int>(indexConstantUnion->getUConst(0));
4013 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004014
4015 int safeIndex = -1;
4016
Olli Etuahoebee5b32017-11-23 12:56:32 +02004017 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004018 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4020 safeIndex = 0;
4021 }
4022
4023 if (!baseExpression->getType().isUnsizedArray())
4024 {
4025 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004026 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004027 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4030 {
4031 outOfRangeError(outOfRangeIndexIsError, location,
4032 "array index for gl_FragData must be zero when "
4033 "GL_EXT_draw_buffers is disabled",
4034 "[]");
4035 safeIndex = 0;
4036 }
4037 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004038 }
4039 // Only do generic out-of-range check if similar error hasn't already been reported.
4040 if (safeIndex < 0)
4041 {
4042 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004043 {
4044 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4045 baseExpression->getOutermostArraySize(),
4046 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004047 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004048 else if (baseExpression->isMatrix())
4049 {
4050 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4051 baseExpression->getType().getCols(),
4052 "matrix field selection out of range");
4053 }
4054 else
4055 {
4056 ASSERT(baseExpression->isVector());
4057 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4058 baseExpression->getType().getNominalSize(),
4059 "vector field selection out of range");
4060 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004062
Olli Etuahoebee5b32017-11-23 12:56:32 +02004063 ASSERT(safeIndex >= 0);
4064 // Data of constant unions can't be changed, because it may be shared with other
4065 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4066 // sanitized object.
4067 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4068 {
4069 TConstantUnion *safeConstantUnion = new TConstantUnion();
4070 safeConstantUnion->setIConst(safeIndex);
4071 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4072 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4073 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004074
Olli Etuahoebee5b32017-11-23 12:56:32 +02004075 TIntermBinary *node =
4076 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4077 node->setLine(location);
4078 return node->fold(mDiagnostics);
4079 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004080 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004081
4082 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4083 node->setLine(location);
4084 // Indirect indexing can never be constant folded.
4085 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004086}
4087
Olli Etuahoebee5b32017-11-23 12:56:32 +02004088int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4089 const TSourceLoc &location,
4090 int index,
4091 int arraySize,
4092 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004093{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094 // Should not reach here with an unsized / runtime-sized array.
4095 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004096 // A negative index should already have been checked.
4097 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004098 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004099 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004100 std::stringstream reasonStream;
4101 reasonStream << reason << " '" << index << "'";
4102 std::string token = reasonStream.str();
4103 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004104 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004105 }
4106 return index;
4107}
4108
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4110 const TSourceLoc &dotLocation,
4111 const TString &fieldString,
4112 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 if (baseExpression->isArray())
4115 {
4116 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004117 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 }
4119
4120 if (baseExpression->isVector())
4121 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 TVector<int> fieldOffsets;
4123 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4124 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004125 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004126 fieldOffsets.resize(1);
4127 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004129 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4130 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004132 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 else if (baseExpression->getBasicType() == EbtStruct)
4135 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304136 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004137 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004138 {
4139 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004140 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004141 }
4142 else
4143 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004144 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004146 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004147 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004148 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004149 {
4150 fieldFound = true;
4151 break;
4152 }
4153 }
4154 if (fieldFound)
4155 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004156 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004157 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004158 TIntermBinary *node =
4159 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4160 node->setLine(dotLocation);
4161 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004162 }
4163 else
4164 {
4165 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004166 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004167 }
4168 }
4169 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004170 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304172 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004173 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004174 {
4175 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004176 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 }
4178 else
4179 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004180 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004181 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004182 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004183 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004184 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 {
4186 fieldFound = true;
4187 break;
4188 }
4189 }
4190 if (fieldFound)
4191 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004192 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004193 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004194 TIntermBinary *node =
4195 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4196 node->setLine(dotLocation);
4197 // Indexing interface blocks can never be constant folded.
4198 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004199 }
4200 else
4201 {
4202 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004203 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004204 }
4205 }
4206 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004207 else
4208 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004209 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004210 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004211 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004213 }
4214 else
4215 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304216 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004217 " field selection requires structure, vector, or interface block on left hand "
4218 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304219 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004220 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004221 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004222 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004223}
4224
Jamie Madillb98c3a82015-07-23 14:26:04 -04004225TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4226 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004227{
Jamie Madill2f294c92017-11-20 14:47:26 -05004228 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229
4230 if (qualifierType == "shared")
4231 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004232 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004233 {
4234 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4235 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004236 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237 }
4238 else if (qualifierType == "packed")
4239 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004240 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004241 {
4242 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4243 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004244 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004245 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004246 else if (qualifierType == "std430")
4247 {
4248 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4249 qualifier.blockStorage = EbsStd430;
4250 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004251 else if (qualifierType == "std140")
4252 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004253 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004254 }
4255 else if (qualifierType == "row_major")
4256 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004257 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004258 }
4259 else if (qualifierType == "column_major")
4260 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004261 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004262 }
4263 else if (qualifierType == "location")
4264 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004265 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4266 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004267 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004268 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004269 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004270 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4271 {
4272 qualifier.yuv = true;
4273 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004274 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004275 else if (qualifierType == "rgba32f")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifRGBA32F;
4279 }
4280 else if (qualifierType == "rgba16f")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA16F;
4284 }
4285 else if (qualifierType == "r32f")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifR32F;
4289 }
4290 else if (qualifierType == "rgba8")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.imageInternalFormat = EiifRGBA8;
4294 }
4295 else if (qualifierType == "rgba8_snorm")
4296 {
4297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4298 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4299 }
4300 else if (qualifierType == "rgba32i")
4301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.imageInternalFormat = EiifRGBA32I;
4304 }
4305 else if (qualifierType == "rgba16i")
4306 {
4307 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4308 qualifier.imageInternalFormat = EiifRGBA16I;
4309 }
4310 else if (qualifierType == "rgba8i")
4311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.imageInternalFormat = EiifRGBA8I;
4314 }
4315 else if (qualifierType == "r32i")
4316 {
4317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4318 qualifier.imageInternalFormat = EiifR32I;
4319 }
4320 else if (qualifierType == "rgba32ui")
4321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.imageInternalFormat = EiifRGBA32UI;
4324 }
4325 else if (qualifierType == "rgba16ui")
4326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.imageInternalFormat = EiifRGBA16UI;
4329 }
4330 else if (qualifierType == "rgba8ui")
4331 {
4332 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4333 qualifier.imageInternalFormat = EiifRGBA8UI;
4334 }
4335 else if (qualifierType == "r32ui")
4336 {
4337 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4338 qualifier.imageInternalFormat = EiifR32UI;
4339 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004340 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4341 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004342 {
4343 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4344 qualifier.primitiveType = EptPoints;
4345 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004346 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4347 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004348 {
4349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4350 qualifier.primitiveType = EptLines;
4351 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004352 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4353 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004354 {
4355 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4356 qualifier.primitiveType = EptLinesAdjacency;
4357 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004358 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4359 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004360 {
4361 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4362 qualifier.primitiveType = EptTriangles;
4363 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004364 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4365 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004366 {
4367 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4368 qualifier.primitiveType = EptTrianglesAdjacency;
4369 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004370 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4371 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004372 {
4373 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4374 qualifier.primitiveType = EptLineStrip;
4375 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004376 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4377 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004378 {
4379 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4380 qualifier.primitiveType = EptTriangleStrip;
4381 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004382
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004383 else
4384 {
4385 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004386 }
4387
Jamie Madilla5efff92013-06-06 11:56:47 -04004388 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004389}
4390
Martin Radev802abe02016-08-04 17:48:32 +03004391void TParseContext::parseLocalSize(const TString &qualifierType,
4392 const TSourceLoc &qualifierTypeLine,
4393 int intValue,
4394 const TSourceLoc &intValueLine,
4395 const std::string &intValueString,
4396 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004397 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004398{
Olli Etuaho856c4972016-08-08 11:38:39 +03004399 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004400 if (intValue < 1)
4401 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004402 std::stringstream reasonStream;
4403 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4404 std::string reason = reasonStream.str();
4405 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004406 }
4407 (*localSize)[index] = intValue;
4408}
4409
Olli Etuaho09b04a22016-12-15 13:30:26 +00004410void TParseContext::parseNumViews(int intValue,
4411 const TSourceLoc &intValueLine,
4412 const std::string &intValueString,
4413 int *numViews)
4414{
4415 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4416 // specification.
4417 if (intValue < 1)
4418 {
4419 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4420 }
4421 *numViews = intValue;
4422}
4423
Shaob5cc1192017-07-06 10:47:20 +08004424void TParseContext::parseInvocations(int intValue,
4425 const TSourceLoc &intValueLine,
4426 const std::string &intValueString,
4427 int *numInvocations)
4428{
4429 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4430 // it doesn't make sense to accept invocations <= 0.
4431 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4432 {
4433 error(intValueLine,
4434 "out of range: invocations must be in the range of [1, "
4435 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4436 intValueString.c_str());
4437 }
4438 else
4439 {
4440 *numInvocations = intValue;
4441 }
4442}
4443
4444void TParseContext::parseMaxVertices(int intValue,
4445 const TSourceLoc &intValueLine,
4446 const std::string &intValueString,
4447 int *maxVertices)
4448{
4449 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4450 // it doesn't make sense to accept max_vertices < 0.
4451 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4452 {
4453 error(
4454 intValueLine,
4455 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4456 intValueString.c_str());
4457 }
4458 else
4459 {
4460 *maxVertices = intValue;
4461 }
4462}
4463
Jamie Madillb98c3a82015-07-23 14:26:04 -04004464TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4465 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004466 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304467 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004468{
Jamie Madill2f294c92017-11-20 14:47:26 -05004469 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470
Martin Radev802abe02016-08-04 17:48:32 +03004471 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472
Martin Radev802abe02016-08-04 17:48:32 +03004473 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004474 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004475 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 if (intValue < 0)
4477 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004478 error(intValueLine, "out of range: location must be non-negative",
4479 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004480 }
4481 else
4482 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004483 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004484 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004485 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004486 }
Olli Etuaho43364892017-02-13 16:00:12 +00004487 else if (qualifierType == "binding")
4488 {
4489 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4490 if (intValue < 0)
4491 {
4492 error(intValueLine, "out of range: binding must be non-negative",
4493 intValueString.c_str());
4494 }
4495 else
4496 {
4497 qualifier.binding = intValue;
4498 }
4499 }
jchen104cdac9e2017-05-08 11:01:20 +08004500 else if (qualifierType == "offset")
4501 {
4502 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4503 if (intValue < 0)
4504 {
4505 error(intValueLine, "out of range: offset must be non-negative",
4506 intValueString.c_str());
4507 }
4508 else
4509 {
4510 qualifier.offset = intValue;
4511 }
4512 }
Martin Radev802abe02016-08-04 17:48:32 +03004513 else if (qualifierType == "local_size_x")
4514 {
4515 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4516 &qualifier.localSize);
4517 }
4518 else if (qualifierType == "local_size_y")
4519 {
4520 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4521 &qualifier.localSize);
4522 }
4523 else if (qualifierType == "local_size_z")
4524 {
4525 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4526 &qualifier.localSize);
4527 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004528 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004529 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004530 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4531 {
4532 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4533 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004534 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004535 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4536 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004537 {
4538 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4539 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004540 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4541 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004542 {
4543 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4544 }
4545
Martin Radev802abe02016-08-04 17:48:32 +03004546 else
4547 {
4548 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004549 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004550
Jamie Madilla5efff92013-06-06 11:56:47 -04004551 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004552}
4553
Olli Etuaho613b9592016-09-05 12:05:53 +03004554TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4555{
4556 return new TTypeQualifierBuilder(
4557 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4558 mShaderVersion);
4559}
4560
Olli Etuahocce89652017-06-19 16:04:09 +03004561TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4562 const TSourceLoc &loc)
4563{
4564 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4565 return new TStorageQualifierWrapper(qualifier, loc);
4566}
4567
4568TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4569{
4570 if (getShaderType() == GL_VERTEX_SHADER)
4571 {
4572 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4573 }
4574 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4575}
4576
4577TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4578{
4579 if (declaringFunction())
4580 {
4581 return new TStorageQualifierWrapper(EvqIn, loc);
4582 }
Shaob5cc1192017-07-06 10:47:20 +08004583
4584 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004585 {
Shaob5cc1192017-07-06 10:47:20 +08004586 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004587 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004588 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004589 {
4590 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4591 }
4592 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004593 }
Shaob5cc1192017-07-06 10:47:20 +08004594 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004595 {
Shaob5cc1192017-07-06 10:47:20 +08004596 if (mShaderVersion < 300)
4597 {
4598 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4599 }
4600 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004601 }
Shaob5cc1192017-07-06 10:47:20 +08004602 case GL_COMPUTE_SHADER:
4603 {
4604 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4605 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004606 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004607 {
4608 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4609 }
4610 default:
4611 {
4612 UNREACHABLE();
4613 return new TStorageQualifierWrapper(EvqLast, loc);
4614 }
Olli Etuahocce89652017-06-19 16:04:09 +03004615 }
Olli Etuahocce89652017-06-19 16:04:09 +03004616}
4617
4618TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4619{
4620 if (declaringFunction())
4621 {
4622 return new TStorageQualifierWrapper(EvqOut, loc);
4623 }
Shaob5cc1192017-07-06 10:47:20 +08004624 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004625 {
Shaob5cc1192017-07-06 10:47:20 +08004626 case GL_VERTEX_SHADER:
4627 {
4628 if (mShaderVersion < 300)
4629 {
4630 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4631 }
4632 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4633 }
4634 case GL_FRAGMENT_SHADER:
4635 {
4636 if (mShaderVersion < 300)
4637 {
4638 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4639 }
4640 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4641 }
4642 case GL_COMPUTE_SHADER:
4643 {
4644 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4645 return new TStorageQualifierWrapper(EvqLast, loc);
4646 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004647 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004648 {
4649 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4650 }
4651 default:
4652 {
4653 UNREACHABLE();
4654 return new TStorageQualifierWrapper(EvqLast, loc);
4655 }
Olli Etuahocce89652017-06-19 16:04:09 +03004656 }
Olli Etuahocce89652017-06-19 16:04:09 +03004657}
4658
4659TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4660{
4661 if (!declaringFunction())
4662 {
4663 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4664 }
4665 return new TStorageQualifierWrapper(EvqInOut, loc);
4666}
4667
Jamie Madillb98c3a82015-07-23 14:26:04 -04004668TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004669 TLayoutQualifier rightQualifier,
4670 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004671{
Martin Radevc28888b2016-07-22 15:27:42 +03004672 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004673 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004674}
4675
Olli Etuahod5f44c92017-11-29 17:15:40 +02004676TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004677{
4678 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004679 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004680}
4681
Olli Etuahod5f44c92017-11-29 17:15:40 +02004682TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4683 const TSourceLoc &loc,
4684 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004685{
4686 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004687 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004688}
4689
Olli Etuaho722bfb52017-10-26 17:00:11 +03004690void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4691 const TFieldList::const_iterator end,
4692 const TString &name,
4693 const TSourceLoc &location)
4694{
4695 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4696 {
4697 if ((*fieldIter)->name() == name)
4698 {
4699 error(location, "duplicate field name in structure", name.c_str());
4700 }
4701 }
4702}
4703
4704TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4705{
4706 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4707 ++fieldIter)
4708 {
4709 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4710 location);
4711 }
4712 return fields;
4713}
4714
Olli Etuaho4de340a2016-12-16 09:32:03 +00004715TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4716 const TFieldList *newlyAddedFields,
4717 const TSourceLoc &location)
4718{
4719 for (TField *field : *newlyAddedFields)
4720 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004721 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4722 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004723 processedFields->push_back(field);
4724 }
4725 return processedFields;
4726}
4727
Martin Radev70866b82016-07-22 15:27:42 +03004728TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4729 const TTypeQualifierBuilder &typeQualifierBuilder,
4730 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004731 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004732{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004733 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734
Martin Radev70866b82016-07-22 15:27:42 +03004735 typeSpecifier->qualifier = typeQualifier.qualifier;
4736 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004737 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004738 typeSpecifier->invariant = typeQualifier.invariant;
4739 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304740 {
Martin Radev70866b82016-07-22 15:27:42 +03004741 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004742 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004744}
4745
Jamie Madillb98c3a82015-07-23 14:26:04 -04004746TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004747 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748{
Martin Radev4a9cd802016-09-01 16:51:51 +03004749 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4750 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004751
Olli Etuahod5f44c92017-11-29 17:15:40 +02004752 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004753 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004754
Martin Radev4a9cd802016-09-01 16:51:51 +03004755 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004756
Olli Etuahod5f44c92017-11-29 17:15:40 +02004757 TFieldList *fieldList = new TFieldList();
4758
4759 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304760 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004761 TType *type = new TType(typeSpecifier);
4762 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304763 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004764 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004765 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004766 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004768
Olli Etuahod5f44c92017-11-29 17:15:40 +02004769 TField *field = new TField(type, declarator->name(), declarator->line());
4770 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4771 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004772 }
4773
Olli Etuahod5f44c92017-11-29 17:15:40 +02004774 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004775}
4776
Martin Radev4a9cd802016-09-01 16:51:51 +03004777TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4778 const TSourceLoc &nameLine,
4779 const TString *structName,
4780 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004781{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004782 SymbolType structSymbolType = SymbolType::UserDefined;
4783 if (structName == nullptr)
4784 {
4785 structName = NewPoolTString("");
4786 structSymbolType = SymbolType::Empty;
4787 }
4788 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789
Jamie Madill9b820842015-02-12 10:40:10 -05004790 // Store a bool in the struct if we're at global scope, to allow us to
4791 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004792 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004793
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004794 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004795 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004796 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004797 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304798 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004799 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004800 }
4801 }
4802
4803 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004804 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004805 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004806 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004807 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004808 switch (qualifier)
4809 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004810 case EvqGlobal:
4811 case EvqTemporary:
4812 break;
4813 default:
4814 error(field.line(), "invalid qualifier on struct member",
4815 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004816 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004817 }
Martin Radev70866b82016-07-22 15:27:42 +03004818 if (field.type()->isInvariant())
4819 {
4820 error(field.line(), "invalid qualifier on struct member", "invariant");
4821 }
jchen104cdac9e2017-05-08 11:01:20 +08004822 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4823 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004824 {
4825 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4826 }
4827
Olli Etuahoebee5b32017-11-23 12:56:32 +02004828 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4829 field.name().c_str(), field.type());
4830
Olli Etuaho43364892017-02-13 16:00:12 +00004831 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4832
4833 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004834
4835 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004836 }
4837
Martin Radev4a9cd802016-09-01 16:51:51 +03004838 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004839 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004840 exitStructDeclaration();
4841
Martin Radev4a9cd802016-09-01 16:51:51 +03004842 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004843}
4844
Jamie Madillb98c3a82015-07-23 14:26:04 -04004845TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004846 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004847 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004848{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004849 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004850 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004851 init->isVector())
4852 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004853 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4854 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004855 return nullptr;
4856 }
4857
Olli Etuaho923ecef2017-10-11 12:01:38 +03004858 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004859 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004860 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004861 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004862 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004863 }
4864
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004865 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4866 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004867 return node;
4868}
4869
4870TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4871{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004872 if (mSwitchNestingLevel == 0)
4873 {
4874 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 return nullptr;
4876 }
4877 if (condition == nullptr)
4878 {
4879 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004880 return nullptr;
4881 }
4882 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004883 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004884 {
4885 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004886 }
4887 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004888 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4889 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4890 // fold in case labels.
4891 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004892 {
4893 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004894 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004895 TIntermCase *node = new TIntermCase(condition);
4896 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004897 return node;
4898}
4899
4900TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4901{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004902 if (mSwitchNestingLevel == 0)
4903 {
4904 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004905 return nullptr;
4906 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004907 TIntermCase *node = new TIntermCase(nullptr);
4908 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004909 return node;
4910}
4911
Jamie Madillb98c3a82015-07-23 14:26:04 -04004912TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4913 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004914 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004915{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004916 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004917
4918 switch (op)
4919 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004920 case EOpLogicalNot:
4921 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4922 child->isVector())
4923 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004924 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004925 return nullptr;
4926 }
4927 break;
4928 case EOpBitwiseNot:
4929 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4930 child->isMatrix() || child->isArray())
4931 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004932 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004933 return nullptr;
4934 }
4935 break;
4936 case EOpPostIncrement:
4937 case EOpPreIncrement:
4938 case EOpPostDecrement:
4939 case EOpPreDecrement:
4940 case EOpNegative:
4941 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004942 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4943 child->getBasicType() == EbtBool || child->isArray() ||
4944 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004945 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004946 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004947 return nullptr;
4948 }
4949 // Operators for built-ins are already type checked against their prototype.
4950 default:
4951 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004952 }
4953
Jiajia Qinbc585152017-06-23 15:42:17 +08004954 if (child->getMemoryQualifier().writeonly)
4955 {
4956 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4957 return nullptr;
4958 }
4959
Olli Etuahof119a262016-08-19 15:54:22 +03004960 TIntermUnary *node = new TIntermUnary(op, child);
4961 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004962
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004963 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004964}
4965
Olli Etuaho09b22472015-02-11 11:47:26 +02004966TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4967{
Olli Etuahocce89652017-06-19 16:04:09 +03004968 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004969 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004970 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004971 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004972 return child;
4973 }
4974 return node;
4975}
4976
Jamie Madillb98c3a82015-07-23 14:26:04 -04004977TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4978 TIntermTyped *child,
4979 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004980{
Olli Etuaho856c4972016-08-08 11:38:39 +03004981 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004982 return addUnaryMath(op, child, loc);
4983}
4984
Jamie Madillb98c3a82015-07-23 14:26:04 -04004985bool TParseContext::binaryOpCommonCheck(TOperator op,
4986 TIntermTyped *left,
4987 TIntermTyped *right,
4988 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004989{
jchen10b4cf5652017-05-05 18:51:17 +08004990 // Check opaque types are not allowed to be operands in expressions other than array indexing
4991 // and structure member selection.
4992 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4993 {
4994 switch (op)
4995 {
4996 case EOpIndexDirect:
4997 case EOpIndexIndirect:
4998 break;
4999 case EOpIndexDirectStruct:
5000 UNREACHABLE();
5001
5002 default:
5003 error(loc, "Invalid operation for variables with an opaque type",
5004 GetOperatorString(op));
5005 return false;
5006 }
5007 }
jchen10cc2a10e2017-05-03 14:05:12 +08005008
Jiajia Qinbc585152017-06-23 15:42:17 +08005009 if (right->getMemoryQualifier().writeonly)
5010 {
5011 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5012 return false;
5013 }
5014
5015 if (left->getMemoryQualifier().writeonly)
5016 {
5017 switch (op)
5018 {
5019 case EOpAssign:
5020 case EOpInitialize:
5021 case EOpIndexDirect:
5022 case EOpIndexIndirect:
5023 case EOpIndexDirectStruct:
5024 case EOpIndexDirectInterfaceBlock:
5025 break;
5026 default:
5027 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5028 return false;
5029 }
5030 }
5031
Olli Etuaho244be012016-08-18 15:26:02 +03005032 if (left->getType().getStruct() || right->getType().getStruct())
5033 {
5034 switch (op)
5035 {
5036 case EOpIndexDirectStruct:
5037 ASSERT(left->getType().getStruct());
5038 break;
5039 case EOpEqual:
5040 case EOpNotEqual:
5041 case EOpAssign:
5042 case EOpInitialize:
5043 if (left->getType() != right->getType())
5044 {
5045 return false;
5046 }
5047 break;
5048 default:
5049 error(loc, "Invalid operation for structs", GetOperatorString(op));
5050 return false;
5051 }
5052 }
5053
Olli Etuaho94050052017-05-08 14:17:44 +03005054 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5055 {
5056 switch (op)
5057 {
5058 case EOpIndexDirectInterfaceBlock:
5059 ASSERT(left->getType().getInterfaceBlock());
5060 break;
5061 default:
5062 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5063 return false;
5064 }
5065 }
5066
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005067 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005068 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005069 error(loc, "array / non-array mismatch", GetOperatorString(op));
5070 return false;
5071 }
5072
5073 if (left->isArray())
5074 {
5075 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005076 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005077 {
5078 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5079 return false;
5080 }
5081
Olli Etuahoe79904c2015-03-18 16:56:42 +02005082 switch (op)
5083 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005084 case EOpEqual:
5085 case EOpNotEqual:
5086 case EOpAssign:
5087 case EOpInitialize:
5088 break;
5089 default:
5090 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5091 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005092 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005093 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005094 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005095 {
5096 error(loc, "array size mismatch", GetOperatorString(op));
5097 return false;
5098 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005099 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005100
5101 // Check ops which require integer / ivec parameters
5102 bool isBitShift = false;
5103 switch (op)
5104 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005105 case EOpBitShiftLeft:
5106 case EOpBitShiftRight:
5107 case EOpBitShiftLeftAssign:
5108 case EOpBitShiftRightAssign:
5109 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5110 // check that the basic type is an integer type.
5111 isBitShift = true;
5112 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5113 {
5114 return false;
5115 }
5116 break;
5117 case EOpBitwiseAnd:
5118 case EOpBitwiseXor:
5119 case EOpBitwiseOr:
5120 case EOpBitwiseAndAssign:
5121 case EOpBitwiseXorAssign:
5122 case EOpBitwiseOrAssign:
5123 // It is enough to check the type of only one operand, since later it
5124 // is checked that the operand types match.
5125 if (!IsInteger(left->getBasicType()))
5126 {
5127 return false;
5128 }
5129 break;
5130 default:
5131 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005132 }
5133
5134 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5135 // So the basic type should usually match.
5136 if (!isBitShift && left->getBasicType() != right->getBasicType())
5137 {
5138 return false;
5139 }
5140
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005141 // Check that:
5142 // 1. Type sizes match exactly on ops that require that.
5143 // 2. Restrictions for structs that contain arrays or samplers are respected.
5144 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005145 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005147 case EOpAssign:
5148 case EOpInitialize:
5149 case EOpEqual:
5150 case EOpNotEqual:
5151 // ESSL 1.00 sections 5.7, 5.8, 5.9
5152 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5153 {
5154 error(loc, "undefined operation for structs containing arrays",
5155 GetOperatorString(op));
5156 return false;
5157 }
5158 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5159 // we interpret the spec so that this extends to structs containing samplers,
5160 // similarly to ESSL 1.00 spec.
5161 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5162 left->getType().isStructureContainingSamplers())
5163 {
5164 error(loc, "undefined operation for structs containing samplers",
5165 GetOperatorString(op));
5166 return false;
5167 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005168
Olli Etuahoe1805592017-01-02 16:41:20 +00005169 if ((left->getNominalSize() != right->getNominalSize()) ||
5170 (left->getSecondarySize() != right->getSecondarySize()))
5171 {
5172 error(loc, "dimension mismatch", GetOperatorString(op));
5173 return false;
5174 }
5175 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005176 case EOpLessThan:
5177 case EOpGreaterThan:
5178 case EOpLessThanEqual:
5179 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005180 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005181 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005182 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183 return false;
5184 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005185 break;
5186 case EOpAdd:
5187 case EOpSub:
5188 case EOpDiv:
5189 case EOpIMod:
5190 case EOpBitShiftLeft:
5191 case EOpBitShiftRight:
5192 case EOpBitwiseAnd:
5193 case EOpBitwiseXor:
5194 case EOpBitwiseOr:
5195 case EOpAddAssign:
5196 case EOpSubAssign:
5197 case EOpDivAssign:
5198 case EOpIModAssign:
5199 case EOpBitShiftLeftAssign:
5200 case EOpBitShiftRightAssign:
5201 case EOpBitwiseAndAssign:
5202 case EOpBitwiseXorAssign:
5203 case EOpBitwiseOrAssign:
5204 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5205 {
5206 return false;
5207 }
5208
5209 // Are the sizes compatible?
5210 if (left->getNominalSize() != right->getNominalSize() ||
5211 left->getSecondarySize() != right->getSecondarySize())
5212 {
5213 // If the nominal sizes of operands do not match:
5214 // One of them must be a scalar.
5215 if (!left->isScalar() && !right->isScalar())
5216 return false;
5217
5218 // In the case of compound assignment other than multiply-assign,
5219 // the right side needs to be a scalar. Otherwise a vector/matrix
5220 // would be assigned to a scalar. A scalar can't be shifted by a
5221 // vector either.
5222 if (!right->isScalar() &&
5223 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5224 return false;
5225 }
5226 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 default:
5228 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005229 }
5230
Olli Etuahod6b14282015-03-17 14:31:35 +02005231 return true;
5232}
5233
Olli Etuaho1dded802016-08-18 18:13:13 +03005234bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5235 const TType &left,
5236 const TType &right)
5237{
5238 switch (op)
5239 {
5240 case EOpMul:
5241 case EOpMulAssign:
5242 return left.getNominalSize() == right.getNominalSize() &&
5243 left.getSecondarySize() == right.getSecondarySize();
5244 case EOpVectorTimesScalar:
5245 return true;
5246 case EOpVectorTimesScalarAssign:
5247 ASSERT(!left.isMatrix() && !right.isMatrix());
5248 return left.isVector() && !right.isVector();
5249 case EOpVectorTimesMatrix:
5250 return left.getNominalSize() == right.getRows();
5251 case EOpVectorTimesMatrixAssign:
5252 ASSERT(!left.isMatrix() && right.isMatrix());
5253 return left.isVector() && left.getNominalSize() == right.getRows() &&
5254 left.getNominalSize() == right.getCols();
5255 case EOpMatrixTimesVector:
5256 return left.getCols() == right.getNominalSize();
5257 case EOpMatrixTimesScalar:
5258 return true;
5259 case EOpMatrixTimesScalarAssign:
5260 ASSERT(left.isMatrix() && !right.isMatrix());
5261 return !right.isVector();
5262 case EOpMatrixTimesMatrix:
5263 return left.getCols() == right.getRows();
5264 case EOpMatrixTimesMatrixAssign:
5265 ASSERT(left.isMatrix() && right.isMatrix());
5266 // We need to check two things:
5267 // 1. The matrix multiplication step is valid.
5268 // 2. The result will have the same number of columns as the lvalue.
5269 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5270
5271 default:
5272 UNREACHABLE();
5273 return false;
5274 }
5275}
5276
Jamie Madillb98c3a82015-07-23 14:26:04 -04005277TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5278 TIntermTyped *left,
5279 TIntermTyped *right,
5280 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005281{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005282 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005283 return nullptr;
5284
Olli Etuahofc1806e2015-03-17 13:03:11 +02005285 switch (op)
5286 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005287 case EOpEqual:
5288 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 case EOpLessThan:
5290 case EOpGreaterThan:
5291 case EOpLessThanEqual:
5292 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005293 break;
5294 case EOpLogicalOr:
5295 case EOpLogicalXor:
5296 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005297 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5298 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005299 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005300 {
5301 return nullptr;
5302 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005303 // Basic types matching should have been already checked.
5304 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 break;
5306 case EOpAdd:
5307 case EOpSub:
5308 case EOpDiv:
5309 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005310 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5311 !right->getType().getStruct());
5312 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005313 {
5314 return nullptr;
5315 }
5316 break;
5317 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005318 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5319 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005320 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005321 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005322 {
5323 return nullptr;
5324 }
5325 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005326 default:
5327 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005328 }
5329
Olli Etuaho1dded802016-08-18 18:13:13 +03005330 if (op == EOpMul)
5331 {
5332 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5333 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5334 {
5335 return nullptr;
5336 }
5337 }
5338
Olli Etuaho3fdec912016-08-18 15:08:06 +03005339 TIntermBinary *node = new TIntermBinary(op, left, right);
5340 node->setLine(loc);
5341
Olli Etuaho3fdec912016-08-18 15:08:06 +03005342 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005343 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005344}
5345
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5347 TIntermTyped *left,
5348 TIntermTyped *right,
5349 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005350{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005351 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005352 if (node == 0)
5353 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5355 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005356 return left;
5357 }
5358 return node;
5359}
5360
Jamie Madillb98c3a82015-07-23 14:26:04 -04005361TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5362 TIntermTyped *left,
5363 TIntermTyped *right,
5364 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005365{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005366 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005367 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005368 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005369 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5370 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005371 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005372 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005373 }
5374 return node;
5375}
5376
Olli Etuaho13389b62016-10-16 11:48:18 +01005377TIntermBinary *TParseContext::createAssign(TOperator op,
5378 TIntermTyped *left,
5379 TIntermTyped *right,
5380 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005381{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005382 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005383 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005384 if (op == EOpMulAssign)
5385 {
5386 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5387 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5388 {
5389 return nullptr;
5390 }
5391 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005392 TIntermBinary *node = new TIntermBinary(op, left, right);
5393 node->setLine(loc);
5394
Olli Etuaho3fdec912016-08-18 15:08:06 +03005395 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005396 }
5397 return nullptr;
5398}
5399
Jamie Madillb98c3a82015-07-23 14:26:04 -04005400TIntermTyped *TParseContext::addAssign(TOperator op,
5401 TIntermTyped *left,
5402 TIntermTyped *right,
5403 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005404{
Olli Etuahocce89652017-06-19 16:04:09 +03005405 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005406 TIntermTyped *node = createAssign(op, left, right, loc);
5407 if (node == nullptr)
5408 {
5409 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005410 return left;
5411 }
5412 return node;
5413}
5414
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005415TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5416 TIntermTyped *right,
5417 const TSourceLoc &loc)
5418{
Corentin Wallez0d959252016-07-12 17:26:32 -04005419 // WebGL2 section 5.26, the following results in an error:
5420 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005421 if (mShaderSpec == SH_WEBGL2_SPEC &&
5422 (left->isArray() || left->getBasicType() == EbtVoid ||
5423 left->getType().isStructureContainingArrays() || right->isArray() ||
5424 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005425 {
5426 error(loc,
5427 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5428 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005429 }
5430
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005431 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5432 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5433 commaNode->getTypePointer()->setQualifier(resultQualifier);
5434 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005435}
5436
Olli Etuaho49300862015-02-20 14:54:49 +02005437TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5438{
5439 switch (op)
5440 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005441 case EOpContinue:
5442 if (mLoopNestingLevel <= 0)
5443 {
5444 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005445 }
5446 break;
5447 case EOpBreak:
5448 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5449 {
5450 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005451 }
5452 break;
5453 case EOpReturn:
5454 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5455 {
5456 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005457 }
5458 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005459 case EOpKill:
5460 if (mShaderType != GL_FRAGMENT_SHADER)
5461 {
5462 error(loc, "discard supported in fragment shaders only", "discard");
5463 }
5464 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005465 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005466 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005467 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005468 }
Olli Etuahocce89652017-06-19 16:04:09 +03005469 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005470}
5471
Jamie Madillb98c3a82015-07-23 14:26:04 -04005472TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005473 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005474 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005475{
Olli Etuahocce89652017-06-19 16:04:09 +03005476 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005477 {
Olli Etuahocce89652017-06-19 16:04:09 +03005478 ASSERT(op == EOpReturn);
5479 mFunctionReturnsValue = true;
5480 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5481 {
5482 error(loc, "void function cannot return a value", "return");
5483 }
5484 else if (*mCurrentFunctionType != expression->getType())
5485 {
5486 error(loc, "function return is not matching type:", "return");
5487 }
Olli Etuaho49300862015-02-20 14:54:49 +02005488 }
Olli Etuahocce89652017-06-19 16:04:09 +03005489 TIntermBranch *node = new TIntermBranch(op, expression);
5490 node->setLine(loc);
5491 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005492}
5493
Martin Radev84aa2dc2017-09-11 15:51:02 +03005494void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5495{
5496 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5497 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5498 bool isTextureGather = (name == "textureGather");
5499 bool isTextureGatherOffset = (name == "textureGatherOffset");
5500 if (isTextureGather || isTextureGatherOffset)
5501 {
5502 TIntermNode *componentNode = nullptr;
5503 TIntermSequence *arguments = functionCall->getSequence();
5504 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5505 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5506 ASSERT(sampler != nullptr);
5507 switch (sampler->getBasicType())
5508 {
5509 case EbtSampler2D:
5510 case EbtISampler2D:
5511 case EbtUSampler2D:
5512 case EbtSampler2DArray:
5513 case EbtISampler2DArray:
5514 case EbtUSampler2DArray:
5515 if ((isTextureGather && arguments->size() == 3u) ||
5516 (isTextureGatherOffset && arguments->size() == 4u))
5517 {
5518 componentNode = arguments->back();
5519 }
5520 break;
5521 case EbtSamplerCube:
5522 case EbtISamplerCube:
5523 case EbtUSamplerCube:
5524 ASSERT(!isTextureGatherOffset);
5525 if (arguments->size() == 3u)
5526 {
5527 componentNode = arguments->back();
5528 }
5529 break;
5530 case EbtSampler2DShadow:
5531 case EbtSampler2DArrayShadow:
5532 case EbtSamplerCubeShadow:
5533 break;
5534 default:
5535 UNREACHABLE();
5536 break;
5537 }
5538 if (componentNode)
5539 {
5540 const TIntermConstantUnion *componentConstantUnion =
5541 componentNode->getAsConstantUnion();
5542 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5543 {
5544 error(functionCall->getLine(), "Texture component must be a constant expression",
5545 name.c_str());
5546 }
5547 else
5548 {
5549 int component = componentConstantUnion->getIConst(0);
5550 if (component < 0 || component > 3)
5551 {
5552 error(functionCall->getLine(), "Component must be in the range [0;3]",
5553 name.c_str());
5554 }
5555 }
5556 }
5557 }
5558}
5559
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005560void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5561{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005562 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005563 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005564 TIntermNode *offset = nullptr;
5565 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005566 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005567 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5568 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5569 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005570 {
5571 offset = arguments->back();
5572 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005573 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005574 {
5575 // A bias parameter might follow the offset parameter.
5576 ASSERT(arguments->size() >= 3);
5577 offset = (*arguments)[2];
5578 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005579 else if (name == "textureGatherOffset")
5580 {
5581 ASSERT(arguments->size() >= 3u);
5582 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5583 ASSERT(sampler != nullptr);
5584 switch (sampler->getBasicType())
5585 {
5586 case EbtSampler2D:
5587 case EbtISampler2D:
5588 case EbtUSampler2D:
5589 case EbtSampler2DArray:
5590 case EbtISampler2DArray:
5591 case EbtUSampler2DArray:
5592 offset = (*arguments)[2];
5593 break;
5594 case EbtSampler2DShadow:
5595 case EbtSampler2DArrayShadow:
5596 offset = (*arguments)[3];
5597 break;
5598 default:
5599 UNREACHABLE();
5600 break;
5601 }
5602 useTextureGatherOffsetConstraints = true;
5603 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005604 if (offset != nullptr)
5605 {
5606 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5607 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5608 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005609 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005610 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005611 }
5612 else
5613 {
5614 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5615 size_t size = offsetConstantUnion->getType().getObjectSize();
5616 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005617 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5618 : mMinProgramTexelOffset;
5619 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5620 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005621 for (size_t i = 0u; i < size; ++i)
5622 {
5623 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005624 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005625 {
5626 std::stringstream tokenStream;
5627 tokenStream << offsetValue;
5628 std::string token = tokenStream.str();
5629 error(offset->getLine(), "Texture offset value out of valid range",
5630 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005631 }
5632 }
5633 }
5634 }
5635}
5636
Jiajia Qina3106c52017-11-03 09:39:39 +08005637void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5638{
5639 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5640 if (IsAtomicBuiltin(name))
5641 {
5642 TIntermSequence *arguments = functionCall->getSequence();
5643 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5644
5645 if (IsBufferOrSharedVariable(memNode))
5646 {
5647 return;
5648 }
5649
5650 while (memNode->getAsBinaryNode())
5651 {
5652 memNode = memNode->getAsBinaryNode()->getLeft();
5653 if (IsBufferOrSharedVariable(memNode))
5654 {
5655 return;
5656 }
5657 }
5658
5659 error(memNode->getLine(),
5660 "The value passed to the mem argument of an atomic memory function does not "
5661 "correspond to a buffer or shared variable.",
5662 functionCall->getFunctionSymbolInfo()->getName().c_str());
5663 }
5664}
5665
Martin Radev2cc85b32016-08-05 16:22:53 +03005666// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5667void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5668{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005669 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005670 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5671
5672 if (name.compare(0, 5, "image") == 0)
5673 {
5674 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005675 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005676
Olli Etuaho485eefd2017-02-14 17:40:06 +00005677 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005678
5679 if (name.compare(5, 5, "Store") == 0)
5680 {
5681 if (memoryQualifier.readonly)
5682 {
5683 error(imageNode->getLine(),
5684 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005685 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005686 }
5687 }
5688 else if (name.compare(5, 4, "Load") == 0)
5689 {
5690 if (memoryQualifier.writeonly)
5691 {
5692 error(imageNode->getLine(),
5693 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005694 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005695 }
5696 }
5697 }
5698}
5699
5700// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5701void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5702 const TFunction *functionDefinition,
5703 const TIntermAggregate *functionCall)
5704{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005705 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005706
5707 const TIntermSequence &arguments = *functionCall->getSequence();
5708
5709 ASSERT(functionDefinition->getParamCount() == arguments.size());
5710
5711 for (size_t i = 0; i < arguments.size(); ++i)
5712 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005713 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5714 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005715 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5716 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5717
5718 if (IsImage(functionArgumentType.getBasicType()))
5719 {
5720 const TMemoryQualifier &functionArgumentMemoryQualifier =
5721 functionArgumentType.getMemoryQualifier();
5722 const TMemoryQualifier &functionParameterMemoryQualifier =
5723 functionParameterType.getMemoryQualifier();
5724 if (functionArgumentMemoryQualifier.readonly &&
5725 !functionParameterMemoryQualifier.readonly)
5726 {
5727 error(functionCall->getLine(),
5728 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005729 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005730 }
5731
5732 if (functionArgumentMemoryQualifier.writeonly &&
5733 !functionParameterMemoryQualifier.writeonly)
5734 {
5735 error(functionCall->getLine(),
5736 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005737 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005738 }
Martin Radev049edfa2016-11-11 14:35:37 +02005739
5740 if (functionArgumentMemoryQualifier.coherent &&
5741 !functionParameterMemoryQualifier.coherent)
5742 {
5743 error(functionCall->getLine(),
5744 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005745 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005746 }
5747
5748 if (functionArgumentMemoryQualifier.volatileQualifier &&
5749 !functionParameterMemoryQualifier.volatileQualifier)
5750 {
5751 error(functionCall->getLine(),
5752 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005753 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005754 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005755 }
5756 }
5757}
5758
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005759TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005760{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005762}
5763
5764TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005765 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005766 TIntermNode *thisNode,
5767 const TSourceLoc &loc)
5768{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005769 if (thisNode != nullptr)
5770 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005771 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005772 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773
5774 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005775 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005776 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005777 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005778 }
5779 else
5780 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005781 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005782 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5783 }
5784}
5785
5786TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5787 TIntermSequence *arguments,
5788 TIntermNode *thisNode,
5789 const TSourceLoc &loc)
5790{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005791 TIntermTyped *typedThis = thisNode->getAsTyped();
5792 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5793 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5794 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5795 // So accessing fnCall->getName() below is safe.
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005796 if (fnCall->name() != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005797 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005798 error(loc, "invalid method", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005799 }
5800 else if (!arguments->empty())
5801 {
5802 error(loc, "method takes no parameters", "length");
5803 }
5804 else if (typedThis == nullptr || !typedThis->isArray())
5805 {
5806 error(loc, "length can only be called on arrays", "length");
5807 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005808 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5809 mGeometryShaderInputPrimitiveType == EptUndefined)
5810 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005811 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005812 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5813 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 else
5815 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005816 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5817 node->setLine(loc);
5818 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005820 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005821}
5822
5823TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5824 TIntermSequence *arguments,
5825 const TSourceLoc &loc)
5826{
5827 // First find by unmangled name to check whether the function name has been
5828 // hidden by a variable name or struct typename.
5829 // If a function is found, check for one with a matching argument list.
5830 bool builtIn;
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005831 const TSymbol *symbol = symbolTable.find(fnCall->name(), mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005832 if (symbol != nullptr && !symbol->isFunction())
5833 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005834 error(loc, "function name expected", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005835 }
5836 else
5837 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005838 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->name(), *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005839 mShaderVersion, &builtIn);
5840 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005842 error(loc, "no matching overloaded function found", fnCall->name().c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005843 }
5844 else
5845 {
5846 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 //
5848 // A declared function.
5849 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005850 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005851 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005852 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005853 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 if (builtIn && op != EOpNull)
5856 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005857 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858 if (fnCandidate->getParamCount() == 1)
5859 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005860 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005861 TIntermNode *unaryParamNode = arguments->front();
5862 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005863 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005864 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 }
5866 else
5867 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005868 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005869 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005870 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005871
5872 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005873 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305874
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005875 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305876 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005877 // See if we can constant fold a built-in. Note that this may be possible
5878 // even if it is not const-qualified.
5879 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305880 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005881 else
5882 {
5883 return callNode;
5884 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005885 }
5886 }
5887 else
5888 {
5889 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005890 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005891
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005892 // If builtIn == false, the function is user defined - could be an overloaded
5893 // built-in as well.
5894 // if builtIn == true, it's a builtIn function with no op associated with it.
5895 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005896 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005897 {
Olli Etuahofe486322017-03-21 09:30:54 +00005898 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005899 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005900 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005901 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005902 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005903 }
5904 else
5905 {
Olli Etuahofe486322017-03-21 09:30:54 +00005906 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005907 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005908 }
5909
Jiajia Qinbc585152017-06-23 15:42:17 +08005910 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005911
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005912 callNode->setLine(loc);
5913
5914 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005915 }
5916 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005917 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005918
5919 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005920 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005921}
5922
Jamie Madillb98c3a82015-07-23 14:26:04 -04005923TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005924 TIntermTyped *trueExpression,
5925 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005926 const TSourceLoc &loc)
5927{
Olli Etuaho56229f12017-07-10 14:16:33 +03005928 if (!checkIsScalarBool(loc, cond))
5929 {
5930 return falseExpression;
5931 }
Olli Etuaho52901742015-04-15 13:42:45 +03005932
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005933 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005934 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005935 std::stringstream reasonStream;
5936 reasonStream << "mismatching ternary operator operand types '"
5937 << trueExpression->getCompleteString() << " and '"
5938 << falseExpression->getCompleteString() << "'";
5939 std::string reason = reasonStream.str();
5940 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005941 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005942 }
Olli Etuahode318b22016-10-25 16:18:25 +01005943 if (IsOpaqueType(trueExpression->getBasicType()))
5944 {
5945 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005946 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005947 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5948 // Note that structs containing opaque types don't need to be checked as structs are
5949 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005950 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005951 return falseExpression;
5952 }
5953
Jiajia Qinbc585152017-06-23 15:42:17 +08005954 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5955 falseExpression->getMemoryQualifier().writeonly)
5956 {
5957 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5958 return falseExpression;
5959 }
5960
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005961 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005962 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005963 // ESSL 3.00.6 section 5.7:
5964 // Ternary operator support is optional for arrays. No certainty that it works across all
5965 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5966 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005967 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005968 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005969 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005970 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005971 }
Olli Etuaho94050052017-05-08 14:17:44 +03005972 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5973 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005974 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005975 return falseExpression;
5976 }
5977
Corentin Wallez0d959252016-07-12 17:26:32 -04005978 // WebGL2 section 5.26, the following results in an error:
5979 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005980 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005981 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005982 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005983 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005984 }
5985
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005986 // Note that the node resulting from here can be a constant union without being qualified as
5987 // constant.
5988 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5989 node->setLine(loc);
5990
5991 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005992}
Olli Etuaho49300862015-02-20 14:54:49 +02005993
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005994//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005995// Parse an array of strings using yyparse.
5996//
5997// Returns 0 for success.
5998//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005999int PaParseStrings(size_t count,
6000 const char *const string[],
6001 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05306002 TParseContext *context)
6003{
Yunchao He4f285442017-04-21 12:15:49 +08006004 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006005 return 1;
6006
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006007 if (glslang_initialize(context))
6008 return 1;
6009
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006010 int error = glslang_scan(count, string, length, context);
6011 if (!error)
6012 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006013
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006014 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006015
alokp@chromium.org6b495712012-06-29 00:06:58 +00006016 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006017}
Jamie Madill45bcc782016-11-07 13:58:48 -05006018
6019} // namespace sh