blob: b502fe2c62e8e28a78bd27a5543989c23ddb42aa [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 Etuaho195be942017-12-04 23:40:14 +02001109 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1110
Olli Etuaho43364892017-02-13 16:00:12 +00001111 checkBindingIsValid(line, type);
1112
Olli Etuaho856c4972016-08-08 11:38:39 +03001113 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 // gl_LastFragData may be redeclared with a new precision qualifier
1116 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1117 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1119 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001120 if (type.isArrayOfArrays())
1121 {
1122 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1123 identifier.c_str());
1124 return false;
1125 }
1126 else if (static_cast<int>(type.getOutermostArraySize()) ==
1127 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001128 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001129 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001130 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001131 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001132 }
1133 }
1134 else
1135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001136 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1137 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 return false;
1139 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001140 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141
Olli Etuaho8a176262016-08-16 14:23:01 +03001142 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001143 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144
Olli Etuaho195be942017-12-04 23:40:14 +02001145 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 {
1147 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001148 return false;
1149 }
1150
Olli Etuaho8a176262016-08-16 14:23:01 +03001151 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001152 return false;
1153
1154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155}
1156
Martin Radev70866b82016-07-22 15:27:42 +03001157void TParseContext::checkIsParameterQualifierValid(
1158 const TSourceLoc &line,
1159 const TTypeQualifierBuilder &typeQualifierBuilder,
1160 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301161{
Olli Etuahocce89652017-06-19 16:04:09 +03001162 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001163 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001164
1165 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301166 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001167 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1168 }
1169
1170 if (!IsImage(type->getBasicType()))
1171 {
Olli Etuaho43364892017-02-13 16:00:12 +00001172 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001173 }
1174 else
1175 {
1176 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178
Martin Radev70866b82016-07-22 15:27:42 +03001179 type->setQualifier(typeQualifier.qualifier);
1180
1181 if (typeQualifier.precision != EbpUndefined)
1182 {
1183 type->setPrecision(typeQualifier.precision);
1184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185}
1186
Olli Etuaho703671e2017-11-08 17:47:18 +02001187template <size_t size>
1188bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1189 const std::array<TExtension, size> &extensions)
1190{
1191 ASSERT(!extensions.empty());
1192 const TExtensionBehavior &extBehavior = extensionBehavior();
1193
1194 bool canUseWithWarning = false;
1195 bool canUseWithoutWarning = false;
1196
1197 const char *errorMsgString = "";
1198 TExtension errorMsgExtension = TExtension::UNDEFINED;
1199
1200 for (TExtension extension : extensions)
1201 {
1202 auto extIter = extBehavior.find(extension);
1203 if (canUseWithWarning)
1204 {
1205 // We already have an extension that we can use, but with a warning.
1206 // See if we can use the alternative extension without a warning.
1207 if (extIter == extBehavior.end())
1208 {
1209 continue;
1210 }
1211 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1212 {
1213 canUseWithoutWarning = true;
1214 break;
1215 }
1216 continue;
1217 }
1218 if (extIter == extBehavior.end())
1219 {
1220 errorMsgString = "extension is not supported";
1221 errorMsgExtension = extension;
1222 }
1223 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1224 {
1225 errorMsgString = "extension is disabled";
1226 errorMsgExtension = extension;
1227 }
1228 else if (extIter->second == EBhWarn)
1229 {
1230 errorMsgExtension = extension;
1231 canUseWithWarning = true;
1232 }
1233 else
1234 {
1235 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1236 canUseWithoutWarning = true;
1237 break;
1238 }
1239 }
1240
1241 if (canUseWithoutWarning)
1242 {
1243 return true;
1244 }
1245 if (canUseWithWarning)
1246 {
1247 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1248 return true;
1249 }
1250 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1251 return false;
1252}
1253
1254template bool TParseContext::checkCanUseOneOfExtensions(
1255 const TSourceLoc &line,
1256 const std::array<TExtension, 1> &extensions);
1257template bool TParseContext::checkCanUseOneOfExtensions(
1258 const TSourceLoc &line,
1259 const std::array<TExtension, 2> &extensions);
1260template bool TParseContext::checkCanUseOneOfExtensions(
1261 const TSourceLoc &line,
1262 const std::array<TExtension, 3> &extensions);
1263
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001264bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001265{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001266 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001267 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268}
1269
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001270// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1271// compile-time or link-time errors are the same whether or not the declaration is empty".
1272// This function implements all the checks that are done on qualifiers regardless of if the
1273// declaration is empty.
1274void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1275 const sh::TLayoutQualifier &layoutQualifier,
1276 const TSourceLoc &location)
1277{
1278 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1279 {
1280 error(location, "Shared memory declarations cannot have layout specified", "layout");
1281 }
1282
1283 if (layoutQualifier.matrixPacking != EmpUnspecified)
1284 {
1285 error(location, "layout qualifier only valid for interface blocks",
1286 getMatrixPackingString(layoutQualifier.matrixPacking));
1287 return;
1288 }
1289
1290 if (layoutQualifier.blockStorage != EbsUnspecified)
1291 {
1292 error(location, "layout qualifier only valid for interface blocks",
1293 getBlockStorageString(layoutQualifier.blockStorage));
1294 return;
1295 }
1296
1297 if (qualifier == EvqFragmentOut)
1298 {
1299 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1300 {
1301 error(location, "invalid layout qualifier combination", "yuv");
1302 return;
1303 }
1304 }
1305 else
1306 {
1307 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1308 }
1309
Olli Etuaho95468d12017-05-04 11:14:34 +03001310 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1311 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001312 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1313 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001314 {
1315 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1316 }
1317
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001318 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001319 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001320 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001321 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001322 // We're not checking whether the uniform location is in range here since that depends on
1323 // the type of the variable.
1324 // The type can only be fully determined for non-empty declarations.
1325 }
1326 if (!canHaveLocation)
1327 {
1328 checkLocationIsNotSpecified(location, layoutQualifier);
1329 }
1330}
1331
jchen104cdac9e2017-05-08 11:01:20 +08001332void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1333 const TSourceLoc &location)
1334{
1335 if (publicType.precision != EbpHigh)
1336 {
1337 error(location, "Can only be highp", "atomic counter");
1338 }
1339 // dEQP enforces compile error if location is specified. See uniform_location.test.
1340 if (publicType.layoutQualifier.location != -1)
1341 {
1342 error(location, "location must not be set for atomic_uint", "layout");
1343 }
1344 if (publicType.layoutQualifier.binding == -1)
1345 {
1346 error(location, "no binding specified", "atomic counter");
1347 }
1348}
1349
Olli Etuaho55bde912017-10-25 13:41:13 +03001350void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001351{
Olli Etuaho55bde912017-10-25 13:41:13 +03001352 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001353 {
1354 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1355 // error. It is assumed that this applies to empty declarations as well.
1356 error(location, "empty array declaration needs to specify a size", "");
1357 }
Martin Radevb8b01222016-11-20 23:25:53 +02001358}
1359
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001360// These checks are done for all declarations that are non-empty. They're done for non-empty
1361// declarations starting a declarator list, and declarators that follow an empty declaration.
1362void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1363 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001364{
Olli Etuahofa33d582015-04-09 14:33:12 +03001365 switch (publicType.qualifier)
1366 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001367 case EvqVaryingIn:
1368 case EvqVaryingOut:
1369 case EvqAttribute:
1370 case EvqVertexIn:
1371 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001372 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001373 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001374 {
1375 error(identifierLocation, "cannot be used with a structure",
1376 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001377 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001379 break;
1380 case EvqBuffer:
1381 if (publicType.getBasicType() != EbtInterfaceBlock)
1382 {
1383 error(identifierLocation,
1384 "cannot declare buffer variables at global scope(outside a block)",
1385 getQualifierString(publicType.qualifier));
1386 return;
1387 }
1388 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001389 default:
1390 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001391 }
jchen10cc2a10e2017-05-03 14:05:12 +08001392 std::string reason(getBasicString(publicType.getBasicType()));
1393 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001394 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001395 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001396 {
1397 return;
1398 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001399
Andrei Volykhina5527072017-03-22 16:46:30 +03001400 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1401 publicType.qualifier != EvqConst) &&
1402 publicType.getBasicType() == EbtYuvCscStandardEXT)
1403 {
1404 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1405 getQualifierString(publicType.qualifier));
1406 return;
1407 }
1408
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001409 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1410 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001411 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1412 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001413 TType type(publicType);
1414 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001415 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1417 publicType.layoutQualifier);
1418 }
1419 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001420
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001421 // check for layout qualifier issues
1422 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001423
Martin Radev2cc85b32016-08-05 16:22:53 +03001424 if (IsImage(publicType.getBasicType()))
1425 {
1426
1427 switch (layoutQualifier.imageInternalFormat)
1428 {
1429 case EiifRGBA32F:
1430 case EiifRGBA16F:
1431 case EiifR32F:
1432 case EiifRGBA8:
1433 case EiifRGBA8_SNORM:
1434 if (!IsFloatImage(publicType.getBasicType()))
1435 {
1436 error(identifierLocation,
1437 "internal image format requires a floating image type",
1438 getBasicString(publicType.getBasicType()));
1439 return;
1440 }
1441 break;
1442 case EiifRGBA32I:
1443 case EiifRGBA16I:
1444 case EiifRGBA8I:
1445 case EiifR32I:
1446 if (!IsIntegerImage(publicType.getBasicType()))
1447 {
1448 error(identifierLocation,
1449 "internal image format requires an integer image type",
1450 getBasicString(publicType.getBasicType()));
1451 return;
1452 }
1453 break;
1454 case EiifRGBA32UI:
1455 case EiifRGBA16UI:
1456 case EiifRGBA8UI:
1457 case EiifR32UI:
1458 if (!IsUnsignedImage(publicType.getBasicType()))
1459 {
1460 error(identifierLocation,
1461 "internal image format requires an unsigned image type",
1462 getBasicString(publicType.getBasicType()));
1463 return;
1464 }
1465 break;
1466 case EiifUnspecified:
1467 error(identifierLocation, "layout qualifier", "No image internal format specified");
1468 return;
1469 default:
1470 error(identifierLocation, "layout qualifier", "unrecognized token");
1471 return;
1472 }
1473
1474 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1475 switch (layoutQualifier.imageInternalFormat)
1476 {
1477 case EiifR32F:
1478 case EiifR32I:
1479 case EiifR32UI:
1480 break;
1481 default:
1482 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1483 {
1484 error(identifierLocation, "layout qualifier",
1485 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1486 "image variables must be qualified readonly and/or writeonly");
1487 return;
1488 }
1489 break;
1490 }
1491 }
1492 else
1493 {
Olli Etuaho43364892017-02-13 16:00:12 +00001494 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001495 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1496 }
jchen104cdac9e2017-05-08 11:01:20 +08001497
1498 if (IsAtomicCounter(publicType.getBasicType()))
1499 {
1500 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1501 }
1502 else
1503 {
1504 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1505 }
Olli Etuaho43364892017-02-13 16:00:12 +00001506}
Martin Radev2cc85b32016-08-05 16:22:53 +03001507
Olli Etuaho43364892017-02-13 16:00:12 +00001508void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1509{
1510 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001511 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1512 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1513 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1514 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1515 // when it comes to which shaders are accepted by the compiler.
1516 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001517 if (IsImage(type.getBasicType()))
1518 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1520 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001521 }
1522 else if (IsSampler(type.getBasicType()))
1523 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001524 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1525 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001526 }
jchen104cdac9e2017-05-08 11:01:20 +08001527 else if (IsAtomicCounter(type.getBasicType()))
1528 {
1529 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1530 }
Olli Etuaho43364892017-02-13 16:00:12 +00001531 else
1532 {
1533 ASSERT(!IsOpaqueType(type.getBasicType()));
1534 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001535 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001536}
1537
Olli Etuaho856c4972016-08-08 11:38:39 +03001538void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1539 const TString &layoutQualifierName,
1540 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001541{
1542
1543 if (mShaderVersion < versionRequired)
1544 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001545 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001546 }
1547}
1548
Olli Etuaho856c4972016-08-08 11:38:39 +03001549bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1550 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001551{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001552 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001553 for (size_t i = 0u; i < localSize.size(); ++i)
1554 {
1555 if (localSize[i] != -1)
1556 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001557 error(location,
1558 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1559 "global layout declaration",
1560 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001561 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001562 }
1563 }
1564
Olli Etuaho8a176262016-08-16 14:23:01 +03001565 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001566}
1567
Olli Etuaho43364892017-02-13 16:00:12 +00001568void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001569 TLayoutImageInternalFormat internalFormat)
1570{
1571 if (internalFormat != EiifUnspecified)
1572 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001573 error(location, "invalid layout qualifier: only valid when used with images",
1574 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001575 }
Olli Etuaho43364892017-02-13 16:00:12 +00001576}
1577
1578void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1579{
1580 if (binding != -1)
1581 {
1582 error(location,
1583 "invalid layout qualifier: only valid when used with opaque types or blocks",
1584 "binding");
1585 }
1586}
1587
jchen104cdac9e2017-05-08 11:01:20 +08001588void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1589{
1590 if (offset != -1)
1591 {
1592 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1593 "offset");
1594 }
1595}
1596
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001597void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1598 int binding,
1599 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001600{
1601 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001602 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001603 {
1604 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1605 }
1606}
1607
1608void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1609 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001611{
1612 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001613 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001614 {
1615 error(location, "sampler binding greater than maximum texture units", "binding");
1616 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001617}
1618
Jiajia Qinbc585152017-06-23 15:42:17 +08001619void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1620 const TQualifier &qualifier,
1621 int binding,
1622 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001623{
1624 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001625 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001626 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001627 if (binding + size > mMaxUniformBufferBindings)
1628 {
1629 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1630 "binding");
1631 }
1632 }
1633 else if (qualifier == EvqBuffer)
1634 {
1635 if (binding + size > mMaxShaderStorageBufferBindings)
1636 {
1637 error(location,
1638 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1639 "binding");
1640 }
jchen10af713a22017-04-19 09:10:56 +08001641 }
1642}
jchen104cdac9e2017-05-08 11:01:20 +08001643void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1644{
1645 if (binding >= mMaxAtomicCounterBindings)
1646 {
1647 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1648 "binding");
1649 }
1650}
jchen10af713a22017-04-19 09:10:56 +08001651
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001652void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1653 int objectLocationCount,
1654 const TLayoutQualifier &layoutQualifier)
1655{
1656 int loc = layoutQualifier.location;
1657 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1658 {
1659 error(location, "Uniform location out of range", "location");
1660 }
1661}
1662
Andrei Volykhina5527072017-03-22 16:46:30 +03001663void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1664{
1665 if (yuv != false)
1666 {
1667 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1668 }
1669}
1670
Jiajia Qinbc585152017-06-23 15:42:17 +08001671void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1672 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001673{
1674 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1675 {
1676 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001677 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1678 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1679 qual == EvqInOut || qual == EvqConstReadOnly))
1680 {
1681 if (argument->getMemoryQualifier().writeonly)
1682 {
1683 error(argument->getLine(),
1684 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001685 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001686 return;
1687 }
1688 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001689 if (qual == EvqOut || qual == EvqInOut)
1690 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001691 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001692 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001693 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001694 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001695 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001696 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 }
1698 }
1699 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700}
1701
Martin Radev70866b82016-07-22 15:27:42 +03001702void TParseContext::checkInvariantVariableQualifier(bool invariant,
1703 const TQualifier qualifier,
1704 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001705{
Martin Radev70866b82016-07-22 15:27:42 +03001706 if (!invariant)
1707 return;
1708
1709 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001710 {
Martin Radev70866b82016-07-22 15:27:42 +03001711 // input variables in the fragment shader can be also qualified as invariant
1712 if (!sh::CanBeInvariantESSL1(qualifier))
1713 {
1714 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1715 }
1716 }
1717 else
1718 {
1719 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1720 {
1721 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1722 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001723 }
1724}
1725
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001726bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001727{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001728 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001729}
1730
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1732 const char *extName,
1733 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001734{
1735 pp::SourceLocation srcLoc;
1736 srcLoc.file = loc.first_file;
1737 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001738 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001739}
1740
Jamie Madillb98c3a82015-07-23 14:26:04 -04001741void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1742 const char *name,
1743 const char *value,
1744 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001745{
1746 pp::SourceLocation srcLoc;
1747 srcLoc.file = loc.first_file;
1748 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001749 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001750}
1751
Martin Radev4c4c8e72016-08-04 12:25:34 +03001752sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001753{
Jamie Madill2f294c92017-11-20 14:47:26 -05001754 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001755 for (size_t i = 0u; i < result.size(); ++i)
1756 {
1757 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1758 {
1759 result[i] = 1;
1760 }
1761 else
1762 {
1763 result[i] = mComputeShaderLocalSize[i];
1764 }
1765 }
1766 return result;
1767}
1768
Olli Etuaho56229f12017-07-10 14:16:33 +03001769TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1770 const TSourceLoc &line)
1771{
1772 TIntermConstantUnion *node = new TIntermConstantUnion(
1773 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1774 node->setLine(line);
1775 return node;
1776}
1777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778/////////////////////////////////////////////////////////////////////////////////
1779//
1780// Non-Errors.
1781//
1782/////////////////////////////////////////////////////////////////////////////////
1783
Jamie Madill5c097022014-08-20 16:38:32 -04001784const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1785 const TString *name,
1786 const TSymbol *symbol)
1787{
Jamie Madill5c097022014-08-20 16:38:32 -04001788 if (!symbol)
1789 {
1790 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001791 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001792 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001793
1794 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001795 {
1796 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001797 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001798 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001799
1800 const TVariable *variable = static_cast<const TVariable *>(symbol);
1801
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001802 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001804 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001805 }
1806
Olli Etuaho0f684632017-07-13 12:42:15 +03001807 // Reject shaders using both gl_FragData and gl_FragColor
1808 TQualifier qualifier = variable->getType().getQualifier();
1809 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001810 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001811 mUsesFragData = true;
1812 }
1813 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1814 {
1815 mUsesFragColor = true;
1816 }
1817 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1818 {
1819 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001820 }
1821
Olli Etuaho0f684632017-07-13 12:42:15 +03001822 // This validation is not quite correct - it's only an error to write to
1823 // both FragData and FragColor. For simplicity, and because users shouldn't
1824 // be rewarded for reading from undefined varaibles, return an error
1825 // if they are both referenced, rather than assigned.
1826 if (mUsesFragData && mUsesFragColor)
1827 {
1828 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1829 if (mUsesSecondaryOutputs)
1830 {
1831 errorMessage =
1832 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1833 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1834 }
1835 error(location, errorMessage, name->c_str());
1836 }
1837
1838 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1839 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1840 qualifier == EvqWorkGroupSize)
1841 {
1842 error(location,
1843 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1844 "gl_WorkGroupSize");
1845 }
Jamie Madill5c097022014-08-20 16:38:32 -04001846 return variable;
1847}
1848
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001849TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1850 const TString *name,
1851 const TSymbol *symbol)
1852{
1853 const TVariable *variable = getNamedVariable(location, name, symbol);
1854
Olli Etuaho0f684632017-07-13 12:42:15 +03001855 if (!variable)
1856 {
1857 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1858 node->setLine(location);
1859 return node;
1860 }
1861
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001862 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001863 TIntermTyped *node = nullptr;
1864
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001865 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001866 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001867 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001868 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001869 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001871 {
1872 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1873 // needs to be added to the AST as a constant and not as a symbol.
1874 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1875 TConstantUnion *constArray = new TConstantUnion[3];
1876 for (size_t i = 0; i < 3; ++i)
1877 {
1878 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1879 }
1880
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001881 ASSERT(variableType.getBasicType() == EbtUInt);
1882 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001883
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001884 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001885 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001886 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001887 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001888 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1889 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001890 {
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001891 ASSERT(mGeometryShaderInputArraySize > 0u);
1892
Olli Etuaho195be942017-12-04 23:40:14 +02001893 node = new TIntermSymbol(variable);
Olli Etuaho55bde912017-10-25 13:41:13 +03001894 node->getTypePointer()->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001895 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001896 else
1897 {
Olli Etuaho195be942017-12-04 23:40:14 +02001898 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001899 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001900 ASSERT(node != nullptr);
1901 node->setLine(location);
1902 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001903}
1904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905// Initializers show up in several places in the grammar. Have one set of
1906// code to handle them here.
1907//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001908// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001909bool TParseContext::executeInitializer(const TSourceLoc &line,
1910 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001911 TType type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Olli Etuaho13389b62016-10-16 11:48:18 +01001915 ASSERT(initNode != nullptr);
1916 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917
Olli 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 Etuaho195be942017-12-04 23:40:14 +02001927
1928 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001929 if (!declareVariable(line, identifier, type, &variable))
1930 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001931 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933
Olli Etuahob0c645e2015-05-12 14:25:36 +03001934 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001935 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001936 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001937 {
1938 // Error message does not completely match behavior with ESSL 1.00, but
1939 // we want to steer developers towards only using constant expressions.
1940 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001941 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001942 }
1943 if (globalInitWarning)
1944 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001945 warning(
1946 line,
1947 "global variable initializers should be constant expressions "
1948 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1949 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001950 }
1951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001952 //
1953 // identifier must be of type constant, a global, or a temporary
1954 //
1955 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301956 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1957 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001958 error(line, " cannot initialize this type of qualifier ",
1959 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001960 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001961 }
1962 //
1963 // test for and propagate constant
1964 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
Arun Patole7e7e68d2015-05-22 12:02:25 +05301966 if (qualifier == EvqConst)
1967 {
1968 if (qualifier != initializer->getType().getQualifier())
1969 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001970 std::stringstream reasonStream;
1971 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1972 << "'";
1973 std::string reason = reasonStream.str();
1974 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001975 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001976 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301978 if (type != initializer->getType())
1979 {
1980 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001981 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001982 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001983 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001985
1986 // Save the constant folded value to the variable if possible. For example array
1987 // initializers are not folded, since that way copying the array literal to multiple places
1988 // in the shader is avoided.
1989 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1990 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301991 if (initializer->getAsConstantUnion())
1992 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001993 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001994 ASSERT(*initNode == nullptr);
1995 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301996 }
1997 else if (initializer->getAsSymbolNode())
1998 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001999 const TSymbol *symbol =
2000 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
2001 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002003 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002004 if (constArray)
2005 {
2006 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002007 ASSERT(*initNode == nullptr);
2008 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002009 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002010 }
2011 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002012
Olli Etuaho195be942017-12-04 23:40:14 +02002013 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002014 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01002015 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
2016 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02002017 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002018 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03002019 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02002020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 return true;
2023}
2024
2025TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2026 const TString &identifier,
2027 TIntermTyped *initializer,
2028 const TSourceLoc &loc)
2029{
2030 checkIsScalarBool(loc, pType);
2031 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002032 TType type(pType);
2033 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002034 {
2035 // The initializer is valid. The init condition needs to have a node - either the
2036 // initializer node, or a constant node in case the initialized variable is const and won't
2037 // be recorded in the AST.
2038 if (initNode == nullptr)
2039 {
2040 return initializer;
2041 }
2042 else
2043 {
2044 TIntermDeclaration *declaration = new TIntermDeclaration();
2045 declaration->appendDeclarator(initNode);
2046 return declaration;
2047 }
2048 }
2049 return nullptr;
2050}
2051
2052TIntermNode *TParseContext::addLoop(TLoopType type,
2053 TIntermNode *init,
2054 TIntermNode *cond,
2055 TIntermTyped *expr,
2056 TIntermNode *body,
2057 const TSourceLoc &line)
2058{
2059 TIntermNode *node = nullptr;
2060 TIntermTyped *typedCond = nullptr;
2061 if (cond)
2062 {
2063 typedCond = cond->getAsTyped();
2064 }
2065 if (cond == nullptr || typedCond)
2066 {
Olli Etuahocce89652017-06-19 16:04:09 +03002067 if (type == ELoopDoWhile)
2068 {
2069 checkIsScalarBool(line, typedCond);
2070 }
2071 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2072 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2073 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2074 !typedCond->isVector()));
2075
Olli Etuaho3ec75682017-07-05 17:02:55 +03002076 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 node->setLine(line);
2078 return node;
2079 }
2080
Olli Etuahocce89652017-06-19 16:04:09 +03002081 ASSERT(type != ELoopDoWhile);
2082
Olli Etuaho914b79a2017-06-19 16:03:19 +03002083 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2084 ASSERT(declaration);
2085 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2086 ASSERT(declarator->getLeft()->getAsSymbolNode());
2087
2088 // The condition is a declaration. In the AST representation we don't support declarations as
2089 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2090 // the loop.
2091 TIntermBlock *block = new TIntermBlock();
2092
2093 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2094 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2095 block->appendStatement(declareCondition);
2096
2097 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2098 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002099 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002100 block->appendStatement(loop);
2101 loop->setLine(line);
2102 block->setLine(line);
2103 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104}
2105
Olli Etuahocce89652017-06-19 16:04:09 +03002106TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2107 TIntermNodePair code,
2108 const TSourceLoc &loc)
2109{
Olli Etuaho56229f12017-07-10 14:16:33 +03002110 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002111
2112 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002113 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002114 {
2115 if (cond->getAsConstantUnion()->getBConst(0) == true)
2116 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002117 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002118 }
2119 else
2120 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002121 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002122 }
2123 }
2124
Olli Etuaho3ec75682017-07-05 17:02:55 +03002125 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002126 node->setLine(loc);
2127
2128 return node;
2129}
2130
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002131void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2132{
2133 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2134 typeSpecifier->getBasicType());
2135
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002136 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002137 {
2138 error(typeSpecifier->getLine(), "not supported", "first-class array");
2139 typeSpecifier->clearArrayness();
2140 }
2141}
2142
Martin Radev70866b82016-07-22 15:27:42 +03002143TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302144 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002145{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002146 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002147
Martin Radev70866b82016-07-22 15:27:42 +03002148 TPublicType returnType = typeSpecifier;
2149 returnType.qualifier = typeQualifier.qualifier;
2150 returnType.invariant = typeQualifier.invariant;
2151 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002152 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002153 returnType.precision = typeSpecifier.precision;
2154
2155 if (typeQualifier.precision != EbpUndefined)
2156 {
2157 returnType.precision = typeQualifier.precision;
2158 }
2159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2161 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002162
Martin Radev4a9cd802016-09-01 16:51:51 +03002163 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2164 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002165
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002167
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002168 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002169 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002170 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002171 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002172 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002173 returnType.clearArrayness();
2174 }
2175
Martin Radev70866b82016-07-22 15:27:42 +03002176 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002177 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002178 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002180 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002181 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002182
Martin Radev70866b82016-07-22 15:27:42 +03002183 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002184 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002185 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002186 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002187 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002188 }
2189 }
2190 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002191 {
Martin Radev70866b82016-07-22 15:27:42 +03002192 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002193 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002194 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002195 }
Martin Radev70866b82016-07-22 15:27:42 +03002196 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2197 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002198 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002199 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2200 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002201 }
Martin Radev70866b82016-07-22 15:27:42 +03002202 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002203 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002204 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002205 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002206 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002207 }
2208
2209 return returnType;
2210}
2211
Olli Etuaho856c4972016-08-08 11:38:39 +03002212void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2213 const TPublicType &type,
2214 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002215{
2216 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002217 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002218 {
2219 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002220 }
2221
2222 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2223 switch (qualifier)
2224 {
2225 case EvqVertexIn:
2226 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002227 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002228 {
2229 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002231 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 return;
2233 case EvqFragmentOut:
2234 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002235 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002236 {
2237 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002238 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002239 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002240 return;
2241 default:
2242 break;
2243 }
2244
2245 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2246 // restrictions.
2247 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002248 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2249 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002250 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2251 {
2252 error(qualifierLocation, "must use 'flat' interpolation here",
2253 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002254 }
2255
Martin Radev4a9cd802016-09-01 16:51:51 +03002256 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002257 {
2258 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2259 // These restrictions are only implied by the ESSL 3.00 spec, but
2260 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002261 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002262 {
2263 error(qualifierLocation, "cannot be an array of structures",
2264 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002265 }
2266 if (type.isStructureContainingArrays())
2267 {
2268 error(qualifierLocation, "cannot be a structure containing an array",
2269 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002270 }
2271 if (type.isStructureContainingType(EbtStruct))
2272 {
2273 error(qualifierLocation, "cannot be a structure containing a structure",
2274 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002275 }
2276 if (type.isStructureContainingType(EbtBool))
2277 {
2278 error(qualifierLocation, "cannot be a structure containing a bool",
2279 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002280 }
2281 }
2282}
2283
Martin Radev2cc85b32016-08-05 16:22:53 +03002284void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2285{
2286 if (qualifier.getType() == QtStorage)
2287 {
2288 const TStorageQualifierWrapper &storageQualifier =
2289 static_cast<const TStorageQualifierWrapper &>(qualifier);
2290 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2291 !symbolTable.atGlobalLevel())
2292 {
2293 error(storageQualifier.getLine(),
2294 "Local variables can only use the const storage qualifier.",
2295 storageQualifier.getQualifierString().c_str());
2296 }
2297 }
2298}
2299
Olli Etuaho43364892017-02-13 16:00:12 +00002300void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002301 const TSourceLoc &location)
2302{
Jiajia Qinbc585152017-06-23 15:42:17 +08002303 const std::string reason(
2304 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2305 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002306 if (memoryQualifier.readonly)
2307 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002308 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002309 }
2310 if (memoryQualifier.writeonly)
2311 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002312 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002313 }
Martin Radev049edfa2016-11-11 14:35:37 +02002314 if (memoryQualifier.coherent)
2315 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002316 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002317 }
2318 if (memoryQualifier.restrictQualifier)
2319 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002320 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002321 }
2322 if (memoryQualifier.volatileQualifier)
2323 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002324 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002325 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002326}
2327
jchen104cdac9e2017-05-08 11:01:20 +08002328// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2329// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002330void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2331 const TSourceLoc &loc,
2332 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002333{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002334 if (!IsAtomicCounter(type->getBasicType()))
2335 {
2336 return;
2337 }
2338
2339 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2340 : kAtomicCounterSize;
2341 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2342 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002343 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002344 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002345 {
2346 offset = bindingState.appendSpan(size);
2347 }
2348 else
2349 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002350 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002351 }
2352 if (offset == -1)
2353 {
2354 error(loc, "Offset overlapping", "atomic counter");
2355 return;
2356 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002357 layoutQualifier.offset = offset;
2358 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002359}
2360
Olli Etuaho454c34c2017-10-25 16:35:56 +03002361void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2362 const char *token,
2363 TType *type)
2364{
2365 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2366 {
2367 if (type->isArray() && type->getOutermostArraySize() == 0u)
2368 {
2369 // Set size for the unsized geometry shader inputs if they are declared after a valid
2370 // input primitive declaration.
2371 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2372 {
2373 ASSERT(mGeometryShaderInputArraySize > 0u);
2374 type->sizeOutermostUnsizedArray(mGeometryShaderInputArraySize);
2375 }
2376 else
2377 {
2378 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2379 // An input can be declared without an array size if there is a previous layout
2380 // which specifies the size.
2381 error(location,
2382 "Missing a valid input primitive declaration before declaring an unsized "
2383 "array input",
2384 token);
2385 }
2386 }
2387 else if (type->isArray())
2388 {
2389 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2390 }
2391 else
2392 {
2393 error(location, "Geometry shader input variable must be declared as an array", token);
2394 }
2395 }
2396}
2397
Olli Etuaho13389b62016-10-16 11:48:18 +01002398TIntermDeclaration *TParseContext::parseSingleDeclaration(
2399 TPublicType &publicType,
2400 const TSourceLoc &identifierOrTypeLocation,
2401 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002402{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002403 TType type(publicType);
2404 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2405 mDirectiveHandler.pragma().stdgl.invariantAll)
2406 {
2407 TQualifier qualifier = type.getQualifier();
2408
2409 // The directive handler has already taken care of rejecting invalid uses of this pragma
2410 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2411 // affected variable declarations:
2412 //
2413 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2414 // elsewhere, in TranslatorGLSL.)
2415 //
2416 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2417 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2418 // the way this is currently implemented we have to enable this compiler option before
2419 // parsing the shader and determining the shading language version it uses. If this were
2420 // implemented as a post-pass, the workaround could be more targeted.
2421 //
2422 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2423 // the specification, but there are desktop OpenGL drivers that expect that this is the
2424 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2425 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2426 {
2427 type.setInvariant(true);
2428 }
2429 }
2430
Olli Etuaho454c34c2017-10-25 16:35:56 +03002431 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), &type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002432
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002433 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2434 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002435
Olli Etuahobab4c082015-04-24 16:38:49 +03002436 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002437 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002438
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002439 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002440 if (emptyDeclaration)
2441 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002442 emptyDeclarationErrorCheck(type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002443 // In most cases we don't need to create a symbol node for an empty declaration.
2444 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2445 if (type.getBasicType() == EbtStruct)
2446 {
Olli Etuaho195be942017-12-04 23:40:14 +02002447 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002448 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002449 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002450 }
jchen104cdac9e2017-05-08 11:01:20 +08002451 else if (IsAtomicCounter(publicType.getBasicType()))
2452 {
2453 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2454 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002455 }
2456 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002457 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002458 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002459
Olli Etuaho55bde912017-10-25 13:41:13 +03002460 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002461
Olli Etuaho55bc9052017-10-25 17:33:06 +03002462 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, &type);
jchen104cdac9e2017-05-08 11:01:20 +08002463
Olli Etuaho2935c582015-04-08 14:32:06 +03002464 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002465 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002466 {
Olli Etuaho195be942017-12-04 23:40:14 +02002467 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002469 }
2470
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 TIntermDeclaration *declaration = new TIntermDeclaration();
2472 declaration->setLine(identifierOrTypeLocation);
2473 if (symbol)
2474 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002475 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002476 declaration->appendDeclarator(symbol);
2477 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002478 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002479}
2480
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002481TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2482 TPublicType &elementType,
2483 const TSourceLoc &identifierLocation,
2484 const TString &identifier,
2485 const TSourceLoc &indexLocation,
2486 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002487{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002488 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002491 identifierLocation);
2492
Olli Etuaho55bde912017-10-25 13:41:13 +03002493 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002494
Olli Etuaho55bde912017-10-25 13:41:13 +03002495 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002496
Olli Etuaho55bde912017-10-25 13:41:13 +03002497 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002498 arrayType.makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002499
Olli Etuaho454c34c2017-10-25 16:35:56 +03002500 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), &arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002501
2502 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2503
Olli Etuaho55bc9052017-10-25 17:33:06 +03002504 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002505
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 TIntermDeclaration *declaration = new TIntermDeclaration();
2507 declaration->setLine(identifierLocation);
2508
Olli Etuaho195be942017-12-04 23:40:14 +02002509 TVariable *variable = nullptr;
2510 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 {
Olli Etuaho195be942017-12-04 23:40:14 +02002512 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002513 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002514 declaration->appendDeclarator(symbol);
2515 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002516
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002518}
2519
Olli Etuaho13389b62016-10-16 11:48:18 +01002520TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2521 const TSourceLoc &identifierLocation,
2522 const TString &identifier,
2523 const TSourceLoc &initLocation,
2524 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002525{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002526 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002527
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002528 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2529 identifierLocation);
2530
2531 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002532
Olli Etuaho13389b62016-10-16 11:48:18 +01002533 TIntermDeclaration *declaration = new TIntermDeclaration();
2534 declaration->setLine(identifierLocation);
2535
2536 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002537 TType type(publicType);
2538 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002539 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 if (initNode)
2541 {
2542 declaration->appendDeclarator(initNode);
2543 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002544 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002545 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002546}
2547
Olli Etuaho13389b62016-10-16 11:48:18 +01002548TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002549 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002550 const TSourceLoc &identifierLocation,
2551 const TString &identifier,
2552 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002553 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002554 const TSourceLoc &initLocation,
2555 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002556{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002557 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002560 identifierLocation);
2561
Olli Etuaho55bde912017-10-25 13:41:13 +03002562 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563
Olli Etuaho55bde912017-10-25 13:41:13 +03002564 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565
Olli Etuaho55bde912017-10-25 13:41:13 +03002566 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002567 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002568
Olli Etuaho13389b62016-10-16 11:48:18 +01002569 TIntermDeclaration *declaration = new TIntermDeclaration();
2570 declaration->setLine(identifierLocation);
2571
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002572 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002574 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002576 if (initNode)
2577 {
2578 declaration->appendDeclarator(initNode);
2579 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002580 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002581
2582 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002583}
2584
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002585TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002586 const TTypeQualifierBuilder &typeQualifierBuilder,
2587 const TSourceLoc &identifierLoc,
2588 const TString *identifier,
2589 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002590{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002591 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002592
Martin Radev70866b82016-07-22 15:27:42 +03002593 if (!typeQualifier.invariant)
2594 {
2595 error(identifierLoc, "Expected invariant", identifier->c_str());
2596 return nullptr;
2597 }
2598 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2599 {
2600 return nullptr;
2601 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002602 if (!symbol)
2603 {
2604 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002605 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606 }
Martin Radev70866b82016-07-22 15:27:42 +03002607 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002608 {
Martin Radev70866b82016-07-22 15:27:42 +03002609 error(identifierLoc, "invariant declaration specifies qualifier",
2610 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002611 }
Martin Radev70866b82016-07-22 15:27:42 +03002612 if (typeQualifier.precision != EbpUndefined)
2613 {
2614 error(identifierLoc, "invariant declaration specifies precision",
2615 getPrecisionString(typeQualifier.precision));
2616 }
2617 if (!typeQualifier.layoutQualifier.isEmpty())
2618 {
2619 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2620 }
2621
2622 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002623 if (!variable)
2624 {
2625 return nullptr;
2626 }
Martin Radev70866b82016-07-22 15:27:42 +03002627 const TType &type = variable->getType();
2628
2629 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2630 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002631 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002632
2633 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2634
Olli Etuaho195be942017-12-04 23:40:14 +02002635 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002636 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002637
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002638 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002639}
2640
Olli Etuaho13389b62016-10-16 11:48:18 +01002641void TParseContext::parseDeclarator(TPublicType &publicType,
2642 const TSourceLoc &identifierLocation,
2643 const TString &identifier,
2644 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002645{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002646 // If the declaration starting this declarator list was empty (example: int,), some checks were
2647 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002649 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002650 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2651 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002652 }
2653
Olli Etuaho856c4972016-08-08 11:38:39 +03002654 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002655
Olli Etuaho43364892017-02-13 16:00:12 +00002656 TType type(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002657
2658 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &type);
2659
Olli Etuaho55bde912017-10-25 13:41:13 +03002660 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &type);
2661
Olli Etuaho55bc9052017-10-25 17:33:06 +03002662 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &type);
2663
Olli Etuaho195be942017-12-04 23:40:14 +02002664 TVariable *variable = nullptr;
2665 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002666 {
Olli Etuaho195be942017-12-04 23:40:14 +02002667 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002668 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 declarationOut->appendDeclarator(symbol);
2670 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002671}
2672
Olli Etuaho55bde912017-10-25 13:41:13 +03002673void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002674 const TSourceLoc &identifierLocation,
2675 const TString &identifier,
2676 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002677 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002678 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002679{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680 // If the declaration starting this declarator list was empty (example: int,), some checks were
2681 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002682 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002683 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002684 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002685 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002686 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002687
Olli Etuaho55bde912017-10-25 13:41:13 +03002688 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002689
Olli Etuaho55bde912017-10-25 13:41:13 +03002690 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002691 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002692 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002693 arrayType.makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002694
Olli Etuaho454c34c2017-10-25 16:35:56 +03002695 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), &arrayType);
2696
Olli Etuaho55bde912017-10-25 13:41:13 +03002697 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &arrayType);
2698
Olli Etuaho55bc9052017-10-25 17:33:06 +03002699 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, &arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002700
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002701 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002702 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002703 {
Olli Etuaho195be942017-12-04 23:40:14 +02002704 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002705 symbol->setLine(identifierLocation);
2706 declarationOut->appendDeclarator(symbol);
2707 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002708 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002709}
2710
Olli Etuaho13389b62016-10-16 11:48:18 +01002711void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2712 const TSourceLoc &identifierLocation,
2713 const TString &identifier,
2714 const TSourceLoc &initLocation,
2715 TIntermTyped *initializer,
2716 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002717{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002718 // If the declaration starting this declarator list was empty (example: int,), some checks were
2719 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002720 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002721 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002722 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2723 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002724 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002725
Olli Etuaho856c4972016-08-08 11:38:39 +03002726 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002727
Olli Etuaho13389b62016-10-16 11:48:18 +01002728 TIntermBinary *initNode = nullptr;
Olli Etuaho55bde912017-10-25 13:41:13 +03002729 TType type(publicType);
2730 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002731 {
2732 //
2733 // build the intermediate representation
2734 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002735 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002736 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002737 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002738 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002739 }
2740}
2741
Olli Etuaho55bde912017-10-25 13:41:13 +03002742void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002743 const TSourceLoc &identifierLocation,
2744 const TString &identifier,
2745 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002746 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002747 const TSourceLoc &initLocation,
2748 TIntermTyped *initializer,
2749 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002750{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002751 // If the declaration starting this declarator list was empty (example: int,), some checks were
2752 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002753 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002754 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002755 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002756 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757 }
2758
Olli Etuaho55bde912017-10-25 13:41:13 +03002759 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002760
Olli Etuaho55bde912017-10-25 13:41:13 +03002761 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002762
Olli Etuaho55bde912017-10-25 13:41:13 +03002763 TType arrayType(elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002764 arrayType.makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002765
2766 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002767 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002768 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 {
2770 if (initNode)
2771 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002772 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002773 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002774 }
2775}
2776
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002777TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2778{
2779 // It's simpler to parse an empty statement as a constant expression rather than having a
2780 // different type of node just for empty statements, that will be pruned from the AST anyway.
2781 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2782 node->setLine(location);
2783 return node;
2784}
2785
jchen104cdac9e2017-05-08 11:01:20 +08002786void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2787 const TSourceLoc &location)
2788{
2789 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2790 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2791 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2792 {
2793 error(location, "Requires both binding and offset", "layout");
2794 return;
2795 }
2796 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2797}
2798
Olli Etuahocce89652017-06-19 16:04:09 +03002799void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2800 const TPublicType &type,
2801 const TSourceLoc &loc)
2802{
2803 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2804 !getFragmentPrecisionHigh())
2805 {
2806 error(loc, "precision is not supported in fragment shader", "highp");
2807 }
2808
2809 if (!CanSetDefaultPrecisionOnType(type))
2810 {
2811 error(loc, "illegal type argument for default precision qualifier",
2812 getBasicString(type.getBasicType()));
2813 return;
2814 }
2815 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2816}
2817
Shaob5cc1192017-07-06 10:47:20 +08002818bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2819{
2820 switch (typeQualifier.layoutQualifier.primitiveType)
2821 {
2822 case EptLines:
2823 case EptLinesAdjacency:
2824 case EptTriangles:
2825 case EptTrianglesAdjacency:
2826 return typeQualifier.qualifier == EvqGeometryIn;
2827
2828 case EptLineStrip:
2829 case EptTriangleStrip:
2830 return typeQualifier.qualifier == EvqGeometryOut;
2831
2832 case EptPoints:
2833 return true;
2834
2835 default:
2836 UNREACHABLE();
2837 return false;
2838 }
2839}
2840
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002841void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2842 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002843{
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002844 if (mGeometryShaderInputArraySize == 0u)
2845 {
2846 mGeometryShaderInputArraySize = inputArraySize;
2847 }
2848 else if (mGeometryShaderInputArraySize != inputArraySize)
2849 {
2850 error(line,
2851 "Array size or input primitive declaration doesn't match the size of earlier sized "
2852 "array inputs.",
2853 "layout");
2854 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002855}
2856
Shaob5cc1192017-07-06 10:47:20 +08002857bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2858{
2859 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2860
2861 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2862
2863 if (layoutQualifier.maxVertices != -1)
2864 {
2865 error(typeQualifier.line,
2866 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2867 return false;
2868 }
2869
2870 // Set mGeometryInputPrimitiveType if exists
2871 if (layoutQualifier.primitiveType != EptUndefined)
2872 {
2873 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2874 {
2875 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2876 return false;
2877 }
2878
2879 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2880 {
2881 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002882 setGeometryShaderInputArraySize(
2883 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2884 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002885 }
2886 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2887 {
2888 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2889 "layout");
2890 return false;
2891 }
2892 }
2893
2894 // Set mGeometryInvocations if exists
2895 if (layoutQualifier.invocations > 0)
2896 {
2897 if (mGeometryShaderInvocations == 0)
2898 {
2899 mGeometryShaderInvocations = layoutQualifier.invocations;
2900 }
2901 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2902 {
2903 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2904 "layout");
2905 return false;
2906 }
2907 }
2908
2909 return true;
2910}
2911
2912bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2913{
2914 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2915
2916 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2917
2918 if (layoutQualifier.invocations > 0)
2919 {
2920 error(typeQualifier.line,
2921 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2922 return false;
2923 }
2924
2925 // Set mGeometryOutputPrimitiveType if exists
2926 if (layoutQualifier.primitiveType != EptUndefined)
2927 {
2928 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2929 {
2930 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2931 return false;
2932 }
2933
2934 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2935 {
2936 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2937 }
2938 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2939 {
2940 error(typeQualifier.line,
2941 "primitive doesn't match earlier output primitive declaration", "layout");
2942 return false;
2943 }
2944 }
2945
2946 // Set mGeometryMaxVertices if exists
2947 if (layoutQualifier.maxVertices > -1)
2948 {
2949 if (mGeometryShaderMaxVertices == -1)
2950 {
2951 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2952 }
2953 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2954 {
2955 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2956 "layout");
2957 return false;
2958 }
2959 }
2960
2961 return true;
2962}
2963
Martin Radev70866b82016-07-22 15:27:42 +03002964void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002965{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002966 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002967 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002968
Martin Radev70866b82016-07-22 15:27:42 +03002969 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2970 typeQualifier.line);
2971
Jamie Madillc2128ff2016-07-04 10:26:17 -04002972 // It should never be the case, but some strange parser errors can send us here.
2973 if (layoutQualifier.isEmpty())
2974 {
2975 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002976 return;
2977 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002978
Martin Radev802abe02016-08-04 17:48:32 +03002979 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002980 {
Olli Etuaho43364892017-02-13 16:00:12 +00002981 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002982 return;
2983 }
2984
Olli Etuaho43364892017-02-13 16:00:12 +00002985 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2986
2987 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002988
2989 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2990
Andrei Volykhina5527072017-03-22 16:46:30 +03002991 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2992
jchen104cdac9e2017-05-08 11:01:20 +08002993 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2994
Qin Jiajiaca68d982017-09-18 16:41:56 +08002995 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2996 typeQualifier.qualifier);
2997
Martin Radev802abe02016-08-04 17:48:32 +03002998 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002999 {
Martin Radev802abe02016-08-04 17:48:32 +03003000 if (mComputeShaderLocalSizeDeclared &&
3001 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3002 {
3003 error(typeQualifier.line, "Work group size does not match the previous declaration",
3004 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003005 return;
3006 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003007
Martin Radev802abe02016-08-04 17:48:32 +03003008 if (mShaderVersion < 310)
3009 {
3010 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003011 return;
3012 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003013
Martin Radev4c4c8e72016-08-04 12:25:34 +03003014 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003015 {
3016 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003017 return;
3018 }
3019
3020 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3021 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3022
3023 const TConstantUnion *maxComputeWorkGroupSizeData =
3024 maxComputeWorkGroupSize->getConstPointer();
3025
3026 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3027 {
3028 if (layoutQualifier.localSize[i] != -1)
3029 {
3030 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3031 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3032 if (mComputeShaderLocalSize[i] < 1 ||
3033 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3034 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003035 std::stringstream reasonStream;
3036 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3037 << maxComputeWorkGroupSizeValue;
3038 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003039
Olli Etuaho4de340a2016-12-16 09:32:03 +00003040 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003041 return;
3042 }
3043 }
3044 }
3045
3046 mComputeShaderLocalSizeDeclared = true;
3047 }
Shaob5cc1192017-07-06 10:47:20 +08003048 else if (typeQualifier.qualifier == EvqGeometryIn)
3049 {
3050 if (mShaderVersion < 310)
3051 {
3052 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3053 return;
3054 }
3055
3056 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3057 {
3058 return;
3059 }
3060 }
3061 else if (typeQualifier.qualifier == EvqGeometryOut)
3062 {
3063 if (mShaderVersion < 310)
3064 {
3065 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3066 "layout");
3067 return;
3068 }
3069
3070 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3071 {
3072 return;
3073 }
3074 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003075 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3076 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003077 {
3078 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3079 // specification.
3080 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3081 {
3082 error(typeQualifier.line, "Number of views does not match the previous declaration",
3083 "layout");
3084 return;
3085 }
3086
3087 if (layoutQualifier.numViews == -1)
3088 {
3089 error(typeQualifier.line, "No num_views specified", "layout");
3090 return;
3091 }
3092
3093 if (layoutQualifier.numViews > mMaxNumViews)
3094 {
3095 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3096 "layout");
3097 return;
3098 }
3099
3100 mNumViews = layoutQualifier.numViews;
3101 }
Martin Radev802abe02016-08-04 17:48:32 +03003102 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003103 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003104 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003105 {
Martin Radev802abe02016-08-04 17:48:32 +03003106 return;
3107 }
3108
Jiajia Qinbc585152017-06-23 15:42:17 +08003109 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003110 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003111 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003112 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003113 return;
3114 }
3115
3116 if (mShaderVersion < 300)
3117 {
3118 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3119 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003120 return;
3121 }
3122
Olli Etuaho09b04a22016-12-15 13:30:26 +00003123 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003124
3125 if (layoutQualifier.matrixPacking != EmpUnspecified)
3126 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003127 if (typeQualifier.qualifier == EvqUniform)
3128 {
3129 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3130 }
3131 else if (typeQualifier.qualifier == EvqBuffer)
3132 {
3133 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3134 }
Martin Radev802abe02016-08-04 17:48:32 +03003135 }
3136
3137 if (layoutQualifier.blockStorage != EbsUnspecified)
3138 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003139 if (typeQualifier.qualifier == EvqUniform)
3140 {
3141 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3142 }
3143 else if (typeQualifier.qualifier == EvqBuffer)
3144 {
3145 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3146 }
Martin Radev802abe02016-08-04 17:48:32 +03003147 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003148 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003149}
3150
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003151TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3152 const TFunction &function,
3153 const TSourceLoc &location,
3154 bool insertParametersToSymbolTable)
3155{
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003156 ASSERT(function.name());
3157 checkIsNotReserved(location, *function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003158
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003159 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 prototype->setLine(location);
3161
3162 for (size_t i = 0; i < function.getParamCount(); i++)
3163 {
3164 const TConstParameter &param = function.getParam(i);
3165
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003166 TIntermSymbol *symbol = nullptr;
3167
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003168 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3169 // be used for unused args).
3170 if (param.name != nullptr)
3171 {
Olli Etuaho195be942017-12-04 23:40:14 +02003172 TVariable *variable =
3173 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3174 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003175 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003176 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003177 {
Olli Etuaho195be942017-12-04 23:40:14 +02003178 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003179 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003180 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003183 // Unsized type of a named parameter should have already been checked and sanitized.
3184 ASSERT(!param.type->isUnsizedArray());
3185 }
3186 else
3187 {
3188 if (param.type->isUnsizedArray())
3189 {
3190 error(location, "function parameter array must be sized at compile time", "[]");
3191 // We don't need to size the arrays since the parameter is unnamed and hence
3192 // inaccessible.
3193 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003197 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3198 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003199 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003200 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003201 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003203 symbol->setLine(location);
3204 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 }
3206 return prototype;
3207}
3208
Olli Etuaho16c745a2017-01-16 17:02:27 +00003209TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3210 const TFunction &parsedFunction,
3211 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003212{
Olli Etuaho476197f2016-10-11 13:59:08 +01003213 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3214 // first declaration. Either way the instance in the symbol table is used to track whether the
3215 // function is declared multiple times.
3216 TFunction *function = static_cast<TFunction *>(
3217 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3218 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003219 {
3220 // ESSL 1.00.17 section 4.2.7.
3221 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3222 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003223 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003224 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003225
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003226 TIntermFunctionPrototype *prototype =
3227 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003228
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003230
3231 if (!symbolTable.atGlobalLevel())
3232 {
3233 // ESSL 3.00.4 section 4.2.4.
3234 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003235 }
3236
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003237 return prototype;
3238}
3239
Olli Etuaho336b1472016-10-05 16:37:55 +01003240TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003241 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003242 TIntermBlock *functionBody,
3243 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003244{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003245 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003246 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3247 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003248 error(location, "function does not return a value:",
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003249 functionPrototype->getFunction()->name()->c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003250 }
3251
Olli Etuahof51fdd22016-10-03 10:03:40 +01003252 if (functionBody == nullptr)
3253 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003254 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003255 functionBody->setLine(location);
3256 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003257 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003258 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003259 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003260
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003261 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003262 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003263}
3264
Olli Etuaho476197f2016-10-11 13:59:08 +01003265void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3266 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003267 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003268{
Olli Etuaho476197f2016-10-11 13:59:08 +01003269 ASSERT(function);
3270 ASSERT(*function);
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003271 ASSERT((*function)->name());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003272 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003273 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003274
3275 if (builtIn)
3276 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003277 error(location, "built-in functions cannot be redefined", (*function)->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003278 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003279 else
Jamie Madill185fb402015-06-12 15:48:48 -04003280 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003281 TFunction *prevDec = static_cast<TFunction *>(
3282 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3283
3284 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3285 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3286 // occurance.
3287 if (*function != prevDec)
3288 {
3289 // Swap the parameters of the previous declaration to the parameters of the function
3290 // definition (parameter names may differ).
3291 prevDec->swapParameters(**function);
3292
3293 // The function definition will share the same symbol as any previous declaration.
3294 *function = prevDec;
3295 }
3296
3297 if ((*function)->isDefined())
3298 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003299 error(location, "function already has a body", (*function)->name()->c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003300 }
3301
3302 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003303 }
Jamie Madill185fb402015-06-12 15:48:48 -04003304
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003305 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003306 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003307 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003308
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003309 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003310 setLoopNestingLevel(0);
3311}
3312
Jamie Madillb98c3a82015-07-23 14:26:04 -04003313TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003314{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003315 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003316 // We don't know at this point whether this is a function definition or a prototype.
3317 // The definition production code will check for redefinitions.
3318 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003319 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003320 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3321 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003322 //
3323 TFunction *prevDec =
3324 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303325
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003326 ASSERT(function->name() != nullptr);
3327
Olli Etuahod80f2942017-11-06 12:44:45 +02003328 for (size_t i = 0u; i < function->getParamCount(); ++i)
3329 {
3330 auto &param = function->getParam(i);
3331 if (param.type->isStructSpecifier())
3332 {
3333 // ESSL 3.00.6 section 12.10.
3334 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003335 function->name()->c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003336 }
3337 }
3338
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003339 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003340 function->name()->c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303341 {
Martin Radevda6254b2016-12-14 17:00:36 +02003342 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303343 // Therefore overloading or redefining builtin functions is an error.
3344 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003345 function->name()->c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303346 }
3347 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003348 {
3349 if (prevDec->getReturnType() != function->getReturnType())
3350 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003351 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003352 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003353 }
3354 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003356 if (prevDec->getParam(i).type->getQualifier() !=
3357 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003358 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003359 error(location,
3360 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003361 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003362 }
3363 }
3364 }
3365
3366 //
3367 // Check for previously declared variables using the same name.
3368 //
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003369 TSymbol *prevSym = symbolTable.find(*function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003370 if (prevSym)
3371 {
3372 if (!prevSym->isFunction())
3373 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003374 error(location, "redefinition of a function", function->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003375 }
3376 }
3377 else
3378 {
3379 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003380 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003381 }
3382
3383 // We're at the inner scope level of the function's arguments and body statement.
3384 // Add the function prototype to the surrounding scope instead.
3385 symbolTable.getOuterLevel()->insert(function);
3386
Olli Etuaho78d13742017-01-18 13:06:10 +00003387 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003388 if (*function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003389 {
3390 if (function->getParamCount() > 0)
3391 {
3392 error(location, "function cannot take any parameter(s)", "main");
3393 }
3394 if (function->getReturnType().getBasicType() != EbtVoid)
3395 {
3396 error(location, "main function cannot return a value",
3397 function->getReturnType().getBasicString());
3398 }
3399 }
3400
Jamie Madill185fb402015-06-12 15:48:48 -04003401 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003402 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3403 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003404 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3405 //
3406 return function;
3407}
3408
Olli Etuaho9de84a52016-06-14 17:36:01 +03003409TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3410 const TString *name,
3411 const TSourceLoc &location)
3412{
3413 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3414 {
3415 error(location, "no qualifiers allowed for function return",
3416 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003417 }
3418 if (!type.layoutQualifier.isEmpty())
3419 {
3420 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003421 }
jchen10cc2a10e2017-05-03 14:05:12 +08003422 // make sure an opaque type is not involved as well...
3423 std::string reason(getBasicString(type.getBasicType()));
3424 reason += "s can't be function return values";
3425 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003426 if (mShaderVersion < 300)
3427 {
3428 // Array return values are forbidden, but there's also no valid syntax for declaring array
3429 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003430 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003431
3432 if (type.isStructureContainingArrays())
3433 {
3434 // ESSL 1.00.17 section 6.1 Function Definitions
3435 error(location, "structures containing arrays can't be function return values",
3436 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003437 }
3438 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003439
3440 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003441 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003442}
3443
Olli Etuahocce89652017-06-19 16:04:09 +03003444TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3445{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003446 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003447 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3448 // really only interested in the mangled name of the function to look up the actual function
3449 // from the symbol table. If we just had the name string and the types of the parameters that
3450 // would be enough, but TFunction carries a lot of extra information in addition to that.
3451 // Besides function calls we do have to store constructor calls in the same data structure, for
3452 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003453 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003454}
3455
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003456TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003457{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003458 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003459 {
3460 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3461 "[]");
3462 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003463 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003464 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003465 error(publicType.getLine(), "constructor can't be a structure definition",
3466 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003467 }
3468
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003469 TType *type = new TType(publicType);
3470 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003471 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003472 error(publicType.getLine(), "cannot construct this type",
3473 getBasicString(publicType.getBasicType()));
3474 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003475 }
3476
Olli Etuaho0c371002017-12-13 17:00:25 +04003477 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003478}
3479
Olli Etuaho55bde912017-10-25 13:41:13 +03003480void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3481 const char *errorMessage,
3482 const char *token,
3483 TType *arrayType)
3484{
3485 if (arrayType->isUnsizedArray())
3486 {
3487 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003488 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003489 }
3490}
3491
3492TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003493 const TString *name,
3494 const TSourceLoc &nameLoc)
3495{
Olli Etuaho55bde912017-10-25 13:41:13 +03003496 ASSERT(type);
3497 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3498 type);
3499 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003500 {
3501 error(nameLoc, "illegal use of type 'void'", name->c_str());
3502 }
3503 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003504 TParameter param = {name, type};
3505 return param;
3506}
3507
Olli Etuaho55bde912017-10-25 13:41:13 +03003508TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3509 const TString *name,
3510 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003511{
Olli Etuaho55bde912017-10-25 13:41:13 +03003512 TType *type = new TType(publicType);
3513 return parseParameterDeclarator(type, name, nameLoc);
3514}
3515
3516TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3517 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003518 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003519 const TSourceLoc &arrayLoc,
3520 TPublicType *elementType)
3521{
3522 checkArrayElementIsNotArray(arrayLoc, *elementType);
3523 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003524 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003525 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003526}
3527
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003528bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3529 TType type,
3530 const TSourceLoc &line)
3531{
3532 if (arguments->empty())
3533 {
3534 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3535 return false;
3536 }
3537 for (TIntermNode *arg : *arguments)
3538 {
3539 TIntermTyped *element = arg->getAsTyped();
3540 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003541 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3542 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003543 {
3544 error(line, "constructing from a non-dereferenced array", "constructor");
3545 return false;
3546 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003547 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003548 {
3549 if (dimensionalityFromElement == 1u)
3550 {
3551 error(line, "implicitly sized array of arrays constructor argument is not an array",
3552 "constructor");
3553 }
3554 else
3555 {
3556 error(line,
3557 "implicitly sized array of arrays constructor argument dimensionality is too "
3558 "low",
3559 "constructor");
3560 }
3561 return false;
3562 }
3563 }
3564 return true;
3565}
3566
Jamie Madillb98c3a82015-07-23 14:26:04 -04003567// This function is used to test for the correctness of the parameters passed to various constructor
3568// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003569//
Olli Etuaho856c4972016-08-08 11:38:39 +03003570// 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 +00003571//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003572TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003573 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303574 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003575{
Olli Etuaho856c4972016-08-08 11:38:39 +03003576 if (type.isUnsizedArray())
3577 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003578 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003579 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003580 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003581 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003582 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003583 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3584 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003585 if (type.getOutermostArraySize() == 0u)
3586 {
3587 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3588 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003589 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003590 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003591 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003592 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003593 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003594 }
3595 }
3596 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003597 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003598
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003599 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003601 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003602 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003603
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003604 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003605 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003606
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003607 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3608 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003609 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003610 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003611 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003612 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003613}
3614
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003615//
3616// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003617// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003618//
Olli Etuaho13389b62016-10-16 11:48:18 +01003619TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003620 const TTypeQualifierBuilder &typeQualifierBuilder,
3621 const TSourceLoc &nameLine,
3622 const TString &blockName,
3623 TFieldList *fieldList,
3624 const TString *instanceName,
3625 const TSourceLoc &instanceLine,
3626 TIntermTyped *arrayIndex,
3627 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003628{
Olli Etuaho856c4972016-08-08 11:38:39 +03003629 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003630
Olli Etuaho77ba4082016-12-16 12:01:18 +00003631 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003632
Jiajia Qinbc585152017-06-23 15:42:17 +08003633 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003635 error(typeQualifier.line,
3636 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3637 "3.10",
3638 getQualifierString(typeQualifier.qualifier));
3639 }
3640 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3641 {
3642 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003643 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003644 }
3645
Martin Radev70866b82016-07-22 15:27:42 +03003646 if (typeQualifier.invariant)
3647 {
3648 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3649 }
3650
Jiajia Qinbc585152017-06-23 15:42:17 +08003651 if (typeQualifier.qualifier != EvqBuffer)
3652 {
3653 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3654 }
Olli Etuaho43364892017-02-13 16:00:12 +00003655
jchen10af713a22017-04-19 09:10:56 +08003656 // add array index
3657 unsigned int arraySize = 0;
3658 if (arrayIndex != nullptr)
3659 {
3660 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3661 }
3662
3663 if (mShaderVersion < 310)
3664 {
3665 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3666 }
3667 else
3668 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003669 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3670 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003671 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003672
Andrei Volykhina5527072017-03-22 16:46:30 +03003673 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3674
Jamie Madill099c0f32013-06-20 11:55:52 -04003675 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003676 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003677 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3678 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003679
Jamie Madill099c0f32013-06-20 11:55:52 -04003680 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3681 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003682 if (typeQualifier.qualifier == EvqUniform)
3683 {
3684 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3685 }
3686 else if (typeQualifier.qualifier == EvqBuffer)
3687 {
3688 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3689 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003690 }
3691
Jamie Madill1566ef72013-06-20 11:55:54 -04003692 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3693 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003694 if (typeQualifier.qualifier == EvqUniform)
3695 {
3696 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3697 }
3698 else if (typeQualifier.qualifier == EvqBuffer)
3699 {
3700 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3701 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003702 }
3703
Olli Etuaho856c4972016-08-08 11:38:39 +03003704 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003705
Martin Radev2cc85b32016-08-05 16:22:53 +03003706 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3707
Jamie Madill98493dd2013-07-08 14:39:03 -04003708 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303709 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3710 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303712 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003713 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303714 {
jchen10cc2a10e2017-05-03 14:05:12 +08003715 std::string reason("unsupported type - ");
3716 reason += fieldType->getBasicString();
3717 reason += " types are not allowed in interface blocks";
3718 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003719 }
3720
Jamie Madill98493dd2013-07-08 14:39:03 -04003721 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003722 switch (qualifier)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003725 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003726 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003727 if (typeQualifier.qualifier == EvqBuffer)
3728 {
3729 error(field->line(), "invalid qualifier on shader storage block member",
3730 getQualifierString(qualifier));
3731 }
3732 break;
3733 case EvqBuffer:
3734 if (typeQualifier.qualifier == EvqUniform)
3735 {
3736 error(field->line(), "invalid qualifier on uniform block member",
3737 getQualifierString(qualifier));
3738 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 break;
3740 default:
3741 error(field->line(), "invalid qualifier on interface block member",
3742 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003744 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003745
Martin Radev70866b82016-07-22 15:27:42 +03003746 if (fieldType->isInvariant())
3747 {
3748 error(field->line(), "invalid qualifier on interface block member", "invariant");
3749 }
3750
Jamie Madilla5efff92013-06-06 11:56:47 -04003751 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003752 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003753 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003754 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003755
Jamie Madill98493dd2013-07-08 14:39:03 -04003756 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003757 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003758 error(field->line(), "invalid layout qualifier: cannot be used here",
3759 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003760 }
3761
Jamie Madill98493dd2013-07-08 14:39:03 -04003762 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003763 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003764 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003765 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003766 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003767 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003768 warning(field->line(),
3769 "extraneous layout qualifier: only has an effect on matrix types",
3770 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003771 }
3772
Jamie Madill98493dd2013-07-08 14:39:03 -04003773 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003774
Olli Etuahoebee5b32017-11-23 12:56:32 +02003775 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3776 typeQualifier.qualifier != EvqBuffer)
3777 {
3778 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3779 checkIsNotUnsizedArray(field->line(),
3780 "array members of interface blocks must specify a size",
3781 field->name().c_str(), field->type());
3782 }
3783
Jiajia Qinbc585152017-06-23 15:42:17 +08003784 if (typeQualifier.qualifier == EvqBuffer)
3785 {
3786 // set memory qualifiers
3787 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3788 // qualified with a memory qualifier, it is as if all of its members were declared with
3789 // the same memory qualifier.
3790 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3791 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3792 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3793 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3794 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3795 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3796 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3797 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3798 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3799 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3800 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003801 }
3802
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003803 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3804 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003805 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3806 {
3807 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3808 }
3809
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003810 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3811 if (arrayIndex != nullptr)
3812 {
3813 interfaceBlockType.makeArray(arraySize);
3814 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815
Olli Etuaho195be942017-12-04 23:40:14 +02003816 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003817 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3818 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003819 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003820 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3821 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003822
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003823 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003824 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003825 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003826 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3827 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003828 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303829 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003830
3831 // set parent pointer of the field variable
3832 fieldType->setInterfaceBlock(interfaceBlock);
3833
Olli Etuaho195be942017-12-04 23:40:14 +02003834 TVariable *fieldVariable =
3835 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3836 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003837 {
3838 fieldVariable->setQualifier(typeQualifier.qualifier);
3839 }
3840 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303841 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003842 error(field->line(), "redefinition of an interface block member name",
3843 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003844 }
3845 }
3846 }
3847 else
3848 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003849 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003850
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003851 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003852 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303853 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003854 error(instanceLine, "redefinition of an interface block instance name",
3855 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003856 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003857 }
3858
Olli Etuaho195be942017-12-04 23:40:14 +02003859 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3860 blockSymbol->setLine(typeQualifier.line);
3861 TIntermDeclaration *declaration = new TIntermDeclaration();
3862 declaration->appendDeclarator(blockSymbol);
3863 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003864
3865 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003866 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003867}
3868
Olli Etuaho383b7912016-08-05 11:22:59 +03003869void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003870{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003871 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003872
3873 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003874 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303875 if (mStructNestingLevel > 1)
3876 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003877 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003878 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003879}
3880
3881void TParseContext::exitStructDeclaration()
3882{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003883 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003884}
3885
Olli Etuaho8a176262016-08-16 14:23:01 +03003886void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003887{
Jamie Madillacb4b812016-11-07 13:50:29 -05003888 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303889 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003890 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003891 }
3892
Arun Patole7e7e68d2015-05-22 12:02:25 +05303893 if (field.type()->getBasicType() != EbtStruct)
3894 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003895 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003896 }
3897
3898 // We're already inside a structure definition at this point, so add
3899 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303900 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3901 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003902 ASSERT(field.type()->getStruct()->name() != nullptr);
Jamie Madill41a49272014-03-18 16:10:13 -04003903 std::stringstream reasonStream;
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003904 reasonStream << "Reference of struct type " << field.type()->getStruct()->name()->c_str()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003905 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003906 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003907 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003908 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003909 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003910}
3911
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003912//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003913// Parse an array index expression
3914//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3916 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303917 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003918{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003919 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3920 {
3921 if (baseExpression->getAsSymbolNode())
3922 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303923 error(location, " left of '[' is not of type array, matrix, or vector ",
3924 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 }
3926 else
3927 {
3928 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3929 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930
Olli Etuaho3ec75682017-07-05 17:02:55 +03003931 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003933
Jiawei Shaod8105a02017-08-08 09:54:36 +08003934 if (baseExpression->getQualifier() == EvqPerVertexIn)
3935 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003936 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003937 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3938 {
3939 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3940 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3941 }
3942 }
3943
Jamie Madill21c1e452014-12-29 11:33:41 -05003944 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3945
Olli Etuaho36b05142015-11-12 13:10:42 +02003946 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3947 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3948 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3949 // index is a constant expression.
3950 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3951 {
3952 if (baseExpression->isInterfaceBlock())
3953 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003954 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003955 switch (baseExpression->getQualifier())
3956 {
3957 case EvqPerVertexIn:
3958 break;
3959 case EvqUniform:
3960 case EvqBuffer:
3961 error(location,
3962 "array indexes for uniform block arrays and shader storage block arrays "
3963 "must be constant integral expressions",
3964 "[");
3965 break;
3966 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003967 // We can reach here only in error cases.
3968 ASSERT(mDiagnostics->numErrors() > 0);
3969 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003970 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003971 }
3972 else if (baseExpression->getQualifier() == EvqFragmentOut)
3973 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003974 error(location,
3975 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003976 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003977 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3978 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003979 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003980 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003981 }
3982
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003983 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003984 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003985 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3986 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3987 // constant fold expressions that are not constant expressions). The most compatible way to
3988 // handle this case is to report a warning instead of an error and force the index to be in
3989 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003990 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003991 int index = 0;
3992 if (indexConstantUnion->getBasicType() == EbtInt)
3993 {
3994 index = indexConstantUnion->getIConst(0);
3995 }
3996 else if (indexConstantUnion->getBasicType() == EbtUInt)
3997 {
3998 index = static_cast<int>(indexConstantUnion->getUConst(0));
3999 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004000
4001 int safeIndex = -1;
4002
Olli Etuahoebee5b32017-11-23 12:56:32 +02004003 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004004 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004005 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4006 safeIndex = 0;
4007 }
4008
4009 if (!baseExpression->getType().isUnsizedArray())
4010 {
4011 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004012 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004013 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004014 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004015 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4016 {
4017 outOfRangeError(outOfRangeIndexIsError, location,
4018 "array index for gl_FragData must be zero when "
4019 "GL_EXT_draw_buffers is disabled",
4020 "[]");
4021 safeIndex = 0;
4022 }
4023 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004024 }
4025 // Only do generic out-of-range check if similar error hasn't already been reported.
4026 if (safeIndex < 0)
4027 {
4028 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 {
4030 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4031 baseExpression->getOutermostArraySize(),
4032 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004033 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004034 else if (baseExpression->isMatrix())
4035 {
4036 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4037 baseExpression->getType().getCols(),
4038 "matrix field selection out of range");
4039 }
4040 else
4041 {
4042 ASSERT(baseExpression->isVector());
4043 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4044 baseExpression->getType().getNominalSize(),
4045 "vector field selection out of range");
4046 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004047 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004048
Olli Etuahoebee5b32017-11-23 12:56:32 +02004049 ASSERT(safeIndex >= 0);
4050 // Data of constant unions can't be changed, because it may be shared with other
4051 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4052 // sanitized object.
4053 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4054 {
4055 TConstantUnion *safeConstantUnion = new TConstantUnion();
4056 safeConstantUnion->setIConst(safeIndex);
4057 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4058 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4059 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004060
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061 TIntermBinary *node =
4062 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4063 node->setLine(location);
4064 return node->fold(mDiagnostics);
4065 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004066 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004067
4068 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4069 node->setLine(location);
4070 // Indirect indexing can never be constant folded.
4071 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004072}
4073
Olli Etuahoebee5b32017-11-23 12:56:32 +02004074int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4075 const TSourceLoc &location,
4076 int index,
4077 int arraySize,
4078 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004079{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004080 // Should not reach here with an unsized / runtime-sized array.
4081 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004082 // A negative index should already have been checked.
4083 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004085 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004086 std::stringstream reasonStream;
4087 reasonStream << reason << " '" << index << "'";
4088 std::string token = reasonStream.str();
4089 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004090 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004091 }
4092 return index;
4093}
4094
Jamie Madillb98c3a82015-07-23 14:26:04 -04004095TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4096 const TSourceLoc &dotLocation,
4097 const TString &fieldString,
4098 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004099{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100 if (baseExpression->isArray())
4101 {
4102 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004103 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 }
4105
4106 if (baseExpression->isVector())
4107 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004108 TVector<int> fieldOffsets;
4109 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4110 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004111 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004112 fieldOffsets.resize(1);
4113 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004115 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4116 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004118 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004119 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004120 else if (baseExpression->getBasicType() == EbtStruct)
4121 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304122 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004123 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 {
4125 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004126 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 }
4128 else
4129 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004130 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004132 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004134 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 {
4136 fieldFound = true;
4137 break;
4138 }
4139 }
4140 if (fieldFound)
4141 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004142 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004143 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004144 TIntermBinary *node =
4145 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4146 node->setLine(dotLocation);
4147 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148 }
4149 else
4150 {
4151 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004152 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153 }
4154 }
4155 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004156 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004157 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304158 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004159 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004160 {
4161 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004162 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004163 }
4164 else
4165 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004166 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004168 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004169 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004170 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 {
4172 fieldFound = true;
4173 break;
4174 }
4175 }
4176 if (fieldFound)
4177 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004178 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004179 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004180 TIntermBinary *node =
4181 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4182 node->setLine(dotLocation);
4183 // Indexing interface blocks can never be constant folded.
4184 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004185 }
4186 else
4187 {
4188 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004189 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004190 }
4191 }
4192 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004193 else
4194 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004195 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004196 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004197 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304198 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004199 }
4200 else
4201 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304202 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004203 " field selection requires structure, vector, or interface block on left hand "
4204 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304205 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004206 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004207 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004208 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004209}
4210
Jamie Madillb98c3a82015-07-23 14:26:04 -04004211TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4212 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004213{
Jamie Madill2f294c92017-11-20 14:47:26 -05004214 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004215
4216 if (qualifierType == "shared")
4217 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004218 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004219 {
4220 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4221 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004222 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004223 }
4224 else if (qualifierType == "packed")
4225 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004226 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004227 {
4228 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4229 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004230 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004231 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004232 else if (qualifierType == "std430")
4233 {
4234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4235 qualifier.blockStorage = EbsStd430;
4236 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004237 else if (qualifierType == "std140")
4238 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004239 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004240 }
4241 else if (qualifierType == "row_major")
4242 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004243 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004244 }
4245 else if (qualifierType == "column_major")
4246 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004247 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004248 }
4249 else if (qualifierType == "location")
4250 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004251 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4252 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004253 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004254 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004255 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004256 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4257 {
4258 qualifier.yuv = true;
4259 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004260 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004261 else if (qualifierType == "rgba32f")
4262 {
4263 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4264 qualifier.imageInternalFormat = EiifRGBA32F;
4265 }
4266 else if (qualifierType == "rgba16f")
4267 {
4268 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4269 qualifier.imageInternalFormat = EiifRGBA16F;
4270 }
4271 else if (qualifierType == "r32f")
4272 {
4273 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4274 qualifier.imageInternalFormat = EiifR32F;
4275 }
4276 else if (qualifierType == "rgba8")
4277 {
4278 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4279 qualifier.imageInternalFormat = EiifRGBA8;
4280 }
4281 else if (qualifierType == "rgba8_snorm")
4282 {
4283 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4284 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4285 }
4286 else if (qualifierType == "rgba32i")
4287 {
4288 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4289 qualifier.imageInternalFormat = EiifRGBA32I;
4290 }
4291 else if (qualifierType == "rgba16i")
4292 {
4293 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4294 qualifier.imageInternalFormat = EiifRGBA16I;
4295 }
4296 else if (qualifierType == "rgba8i")
4297 {
4298 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4299 qualifier.imageInternalFormat = EiifRGBA8I;
4300 }
4301 else if (qualifierType == "r32i")
4302 {
4303 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4304 qualifier.imageInternalFormat = EiifR32I;
4305 }
4306 else if (qualifierType == "rgba32ui")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 qualifier.imageInternalFormat = EiifRGBA32UI;
4310 }
4311 else if (qualifierType == "rgba16ui")
4312 {
4313 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4314 qualifier.imageInternalFormat = EiifRGBA16UI;
4315 }
4316 else if (qualifierType == "rgba8ui")
4317 {
4318 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4319 qualifier.imageInternalFormat = EiifRGBA8UI;
4320 }
4321 else if (qualifierType == "r32ui")
4322 {
4323 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4324 qualifier.imageInternalFormat = EiifR32UI;
4325 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004326 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4327 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004328 {
4329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4330 qualifier.primitiveType = EptPoints;
4331 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004332 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4333 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004334 {
4335 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4336 qualifier.primitiveType = EptLines;
4337 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004338 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4339 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004340 {
4341 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4342 qualifier.primitiveType = EptLinesAdjacency;
4343 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004344 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4345 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004346 {
4347 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4348 qualifier.primitiveType = EptTriangles;
4349 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004350 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4351 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004352 {
4353 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4354 qualifier.primitiveType = EptTrianglesAdjacency;
4355 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004356 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4357 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004358 {
4359 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4360 qualifier.primitiveType = EptLineStrip;
4361 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004362 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4363 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004364 {
4365 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4366 qualifier.primitiveType = EptTriangleStrip;
4367 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004368
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004369 else
4370 {
4371 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004372 }
4373
Jamie Madilla5efff92013-06-06 11:56:47 -04004374 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004375}
4376
Martin Radev802abe02016-08-04 17:48:32 +03004377void TParseContext::parseLocalSize(const TString &qualifierType,
4378 const TSourceLoc &qualifierTypeLine,
4379 int intValue,
4380 const TSourceLoc &intValueLine,
4381 const std::string &intValueString,
4382 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004383 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004384{
Olli Etuaho856c4972016-08-08 11:38:39 +03004385 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004386 if (intValue < 1)
4387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004388 std::stringstream reasonStream;
4389 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4390 std::string reason = reasonStream.str();
4391 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004392 }
4393 (*localSize)[index] = intValue;
4394}
4395
Olli Etuaho09b04a22016-12-15 13:30:26 +00004396void TParseContext::parseNumViews(int intValue,
4397 const TSourceLoc &intValueLine,
4398 const std::string &intValueString,
4399 int *numViews)
4400{
4401 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4402 // specification.
4403 if (intValue < 1)
4404 {
4405 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4406 }
4407 *numViews = intValue;
4408}
4409
Shaob5cc1192017-07-06 10:47:20 +08004410void TParseContext::parseInvocations(int intValue,
4411 const TSourceLoc &intValueLine,
4412 const std::string &intValueString,
4413 int *numInvocations)
4414{
4415 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4416 // it doesn't make sense to accept invocations <= 0.
4417 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4418 {
4419 error(intValueLine,
4420 "out of range: invocations must be in the range of [1, "
4421 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4422 intValueString.c_str());
4423 }
4424 else
4425 {
4426 *numInvocations = intValue;
4427 }
4428}
4429
4430void TParseContext::parseMaxVertices(int intValue,
4431 const TSourceLoc &intValueLine,
4432 const std::string &intValueString,
4433 int *maxVertices)
4434{
4435 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4436 // it doesn't make sense to accept max_vertices < 0.
4437 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4438 {
4439 error(
4440 intValueLine,
4441 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4442 intValueString.c_str());
4443 }
4444 else
4445 {
4446 *maxVertices = intValue;
4447 }
4448}
4449
Jamie Madillb98c3a82015-07-23 14:26:04 -04004450TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4451 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004452 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304453 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004454{
Jamie Madill2f294c92017-11-20 14:47:26 -05004455 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004456
Martin Radev802abe02016-08-04 17:48:32 +03004457 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458
Martin Radev802abe02016-08-04 17:48:32 +03004459 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004461 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004462 if (intValue < 0)
4463 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004464 error(intValueLine, "out of range: location must be non-negative",
4465 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466 }
4467 else
4468 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004469 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004470 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004471 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472 }
Olli Etuaho43364892017-02-13 16:00:12 +00004473 else if (qualifierType == "binding")
4474 {
4475 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4476 if (intValue < 0)
4477 {
4478 error(intValueLine, "out of range: binding must be non-negative",
4479 intValueString.c_str());
4480 }
4481 else
4482 {
4483 qualifier.binding = intValue;
4484 }
4485 }
jchen104cdac9e2017-05-08 11:01:20 +08004486 else if (qualifierType == "offset")
4487 {
4488 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4489 if (intValue < 0)
4490 {
4491 error(intValueLine, "out of range: offset must be non-negative",
4492 intValueString.c_str());
4493 }
4494 else
4495 {
4496 qualifier.offset = intValue;
4497 }
4498 }
Martin Radev802abe02016-08-04 17:48:32 +03004499 else if (qualifierType == "local_size_x")
4500 {
4501 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4502 &qualifier.localSize);
4503 }
4504 else if (qualifierType == "local_size_y")
4505 {
4506 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4507 &qualifier.localSize);
4508 }
4509 else if (qualifierType == "local_size_z")
4510 {
4511 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4512 &qualifier.localSize);
4513 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004514 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004515 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004516 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4517 {
4518 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4519 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004520 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004521 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4522 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004523 {
4524 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4525 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004526 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4527 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004528 {
4529 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4530 }
4531
Martin Radev802abe02016-08-04 17:48:32 +03004532 else
4533 {
4534 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004535 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004536
Jamie Madilla5efff92013-06-06 11:56:47 -04004537 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004538}
4539
Olli Etuaho613b9592016-09-05 12:05:53 +03004540TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4541{
4542 return new TTypeQualifierBuilder(
4543 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4544 mShaderVersion);
4545}
4546
Olli Etuahocce89652017-06-19 16:04:09 +03004547TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4548 const TSourceLoc &loc)
4549{
4550 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4551 return new TStorageQualifierWrapper(qualifier, loc);
4552}
4553
4554TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4555{
4556 if (getShaderType() == GL_VERTEX_SHADER)
4557 {
4558 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4559 }
4560 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4561}
4562
4563TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4564{
4565 if (declaringFunction())
4566 {
4567 return new TStorageQualifierWrapper(EvqIn, loc);
4568 }
Shaob5cc1192017-07-06 10:47:20 +08004569
4570 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004571 {
Shaob5cc1192017-07-06 10:47:20 +08004572 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004573 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004574 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004575 {
4576 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4577 }
4578 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004579 }
Shaob5cc1192017-07-06 10:47:20 +08004580 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004581 {
Shaob5cc1192017-07-06 10:47:20 +08004582 if (mShaderVersion < 300)
4583 {
4584 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4585 }
4586 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004587 }
Shaob5cc1192017-07-06 10:47:20 +08004588 case GL_COMPUTE_SHADER:
4589 {
4590 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4591 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004592 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004593 {
4594 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4595 }
4596 default:
4597 {
4598 UNREACHABLE();
4599 return new TStorageQualifierWrapper(EvqLast, loc);
4600 }
Olli Etuahocce89652017-06-19 16:04:09 +03004601 }
Olli Etuahocce89652017-06-19 16:04:09 +03004602}
4603
4604TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4605{
4606 if (declaringFunction())
4607 {
4608 return new TStorageQualifierWrapper(EvqOut, loc);
4609 }
Shaob5cc1192017-07-06 10:47:20 +08004610 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004611 {
Shaob5cc1192017-07-06 10:47:20 +08004612 case GL_VERTEX_SHADER:
4613 {
4614 if (mShaderVersion < 300)
4615 {
4616 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4617 }
4618 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4619 }
4620 case GL_FRAGMENT_SHADER:
4621 {
4622 if (mShaderVersion < 300)
4623 {
4624 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4625 }
4626 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4627 }
4628 case GL_COMPUTE_SHADER:
4629 {
4630 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4631 return new TStorageQualifierWrapper(EvqLast, loc);
4632 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004633 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004634 {
4635 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4636 }
4637 default:
4638 {
4639 UNREACHABLE();
4640 return new TStorageQualifierWrapper(EvqLast, loc);
4641 }
Olli Etuahocce89652017-06-19 16:04:09 +03004642 }
Olli Etuahocce89652017-06-19 16:04:09 +03004643}
4644
4645TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4646{
4647 if (!declaringFunction())
4648 {
4649 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4650 }
4651 return new TStorageQualifierWrapper(EvqInOut, loc);
4652}
4653
Jamie Madillb98c3a82015-07-23 14:26:04 -04004654TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004655 TLayoutQualifier rightQualifier,
4656 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004657{
Martin Radevc28888b2016-07-22 15:27:42 +03004658 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004659 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004660}
4661
Olli Etuahod5f44c92017-11-29 17:15:40 +02004662TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004663{
4664 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004665 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004666}
4667
Olli Etuahod5f44c92017-11-29 17:15:40 +02004668TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4669 const TSourceLoc &loc,
4670 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004671{
4672 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004673 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004674}
4675
Olli Etuaho722bfb52017-10-26 17:00:11 +03004676void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4677 const TFieldList::const_iterator end,
4678 const TString &name,
4679 const TSourceLoc &location)
4680{
4681 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4682 {
4683 if ((*fieldIter)->name() == name)
4684 {
4685 error(location, "duplicate field name in structure", name.c_str());
4686 }
4687 }
4688}
4689
4690TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4691{
4692 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4693 ++fieldIter)
4694 {
4695 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4696 location);
4697 }
4698 return fields;
4699}
4700
Olli Etuaho4de340a2016-12-16 09:32:03 +00004701TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4702 const TFieldList *newlyAddedFields,
4703 const TSourceLoc &location)
4704{
4705 for (TField *field : *newlyAddedFields)
4706 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004707 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4708 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004709 processedFields->push_back(field);
4710 }
4711 return processedFields;
4712}
4713
Martin Radev70866b82016-07-22 15:27:42 +03004714TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4715 const TTypeQualifierBuilder &typeQualifierBuilder,
4716 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004717 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004718{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004719 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004720
Martin Radev70866b82016-07-22 15:27:42 +03004721 typeSpecifier->qualifier = typeQualifier.qualifier;
4722 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004723 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004724 typeSpecifier->invariant = typeQualifier.invariant;
4725 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304726 {
Martin Radev70866b82016-07-22 15:27:42 +03004727 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004728 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004729 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004730}
4731
Jamie Madillb98c3a82015-07-23 14:26:04 -04004732TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004733 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004734{
Martin Radev4a9cd802016-09-01 16:51:51 +03004735 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4736 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004737
Olli Etuahod5f44c92017-11-29 17:15:40 +02004738 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004739 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004740
Martin Radev4a9cd802016-09-01 16:51:51 +03004741 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004742
Olli Etuahod5f44c92017-11-29 17:15:40 +02004743 TFieldList *fieldList = new TFieldList();
4744
4745 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304746 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004747 TType *type = new TType(typeSpecifier);
4748 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304749 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004750 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004751 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004752 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004753 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004754
Olli Etuahod5f44c92017-11-29 17:15:40 +02004755 TField *field = new TField(type, declarator->name(), declarator->line());
4756 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4757 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004758 }
4759
Olli Etuahod5f44c92017-11-29 17:15:40 +02004760 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761}
4762
Martin Radev4a9cd802016-09-01 16:51:51 +03004763TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4764 const TSourceLoc &nameLine,
4765 const TString *structName,
4766 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004767{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004768 SymbolType structSymbolType = SymbolType::UserDefined;
4769 if (structName == nullptr)
4770 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004771 structSymbolType = SymbolType::Empty;
4772 }
4773 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774
Jamie Madill9b820842015-02-12 10:40:10 -05004775 // Store a bool in the struct if we're at global scope, to allow us to
4776 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004777 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004778
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004779 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004780 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004781 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004782 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304783 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004784 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004785 }
4786 }
4787
4788 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004789 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004790 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004791 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004792 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004793 switch (qualifier)
4794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004795 case EvqGlobal:
4796 case EvqTemporary:
4797 break;
4798 default:
4799 error(field.line(), "invalid qualifier on struct member",
4800 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004801 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802 }
Martin Radev70866b82016-07-22 15:27:42 +03004803 if (field.type()->isInvariant())
4804 {
4805 error(field.line(), "invalid qualifier on struct member", "invariant");
4806 }
jchen104cdac9e2017-05-08 11:01:20 +08004807 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4808 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004809 {
4810 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4811 }
4812
Olli Etuahoebee5b32017-11-23 12:56:32 +02004813 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4814 field.name().c_str(), field.type());
4815
Olli Etuaho43364892017-02-13 16:00:12 +00004816 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4817
4818 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004819
4820 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004821 }
4822
Martin Radev4a9cd802016-09-01 16:51:51 +03004823 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004824 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004825 exitStructDeclaration();
4826
Martin Radev4a9cd802016-09-01 16:51:51 +03004827 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004828}
4829
Jamie Madillb98c3a82015-07-23 14:26:04 -04004830TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004831 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004832 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004833{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004834 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004835 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004836 init->isVector())
4837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004838 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4839 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004840 return nullptr;
4841 }
4842
Olli Etuaho923ecef2017-10-11 12:01:38 +03004843 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004844 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004845 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004846 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004847 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004848 }
4849
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004850 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4851 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004852 return node;
4853}
4854
4855TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4856{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004857 if (mSwitchNestingLevel == 0)
4858 {
4859 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004860 return nullptr;
4861 }
4862 if (condition == nullptr)
4863 {
4864 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004865 return nullptr;
4866 }
4867 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004869 {
4870 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 }
4872 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004873 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4874 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4875 // fold in case labels.
4876 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004877 {
4878 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004879 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004880 TIntermCase *node = new TIntermCase(condition);
4881 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004882 return node;
4883}
4884
4885TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4886{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004887 if (mSwitchNestingLevel == 0)
4888 {
4889 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004890 return nullptr;
4891 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004892 TIntermCase *node = new TIntermCase(nullptr);
4893 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004894 return node;
4895}
4896
Jamie Madillb98c3a82015-07-23 14:26:04 -04004897TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4898 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004899 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004900{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004901 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004902
4903 switch (op)
4904 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004905 case EOpLogicalNot:
4906 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4907 child->isVector())
4908 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004909 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004910 return nullptr;
4911 }
4912 break;
4913 case EOpBitwiseNot:
4914 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4915 child->isMatrix() || child->isArray())
4916 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004917 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004918 return nullptr;
4919 }
4920 break;
4921 case EOpPostIncrement:
4922 case EOpPreIncrement:
4923 case EOpPostDecrement:
4924 case EOpPreDecrement:
4925 case EOpNegative:
4926 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004927 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4928 child->getBasicType() == EbtBool || child->isArray() ||
4929 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004930 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004931 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004932 return nullptr;
4933 }
4934 // Operators for built-ins are already type checked against their prototype.
4935 default:
4936 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004937 }
4938
Jiajia Qinbc585152017-06-23 15:42:17 +08004939 if (child->getMemoryQualifier().writeonly)
4940 {
4941 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4942 return nullptr;
4943 }
4944
Olli Etuahof119a262016-08-19 15:54:22 +03004945 TIntermUnary *node = new TIntermUnary(op, child);
4946 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004947
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004948 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004949}
4950
Olli Etuaho09b22472015-02-11 11:47:26 +02004951TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4952{
Olli Etuahocce89652017-06-19 16:04:09 +03004953 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004954 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004955 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004956 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004957 return child;
4958 }
4959 return node;
4960}
4961
Jamie Madillb98c3a82015-07-23 14:26:04 -04004962TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4963 TIntermTyped *child,
4964 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004965{
Olli Etuaho856c4972016-08-08 11:38:39 +03004966 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004967 return addUnaryMath(op, child, loc);
4968}
4969
Jamie Madillb98c3a82015-07-23 14:26:04 -04004970bool TParseContext::binaryOpCommonCheck(TOperator op,
4971 TIntermTyped *left,
4972 TIntermTyped *right,
4973 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004974{
jchen10b4cf5652017-05-05 18:51:17 +08004975 // Check opaque types are not allowed to be operands in expressions other than array indexing
4976 // and structure member selection.
4977 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4978 {
4979 switch (op)
4980 {
4981 case EOpIndexDirect:
4982 case EOpIndexIndirect:
4983 break;
4984 case EOpIndexDirectStruct:
4985 UNREACHABLE();
4986
4987 default:
4988 error(loc, "Invalid operation for variables with an opaque type",
4989 GetOperatorString(op));
4990 return false;
4991 }
4992 }
jchen10cc2a10e2017-05-03 14:05:12 +08004993
Jiajia Qinbc585152017-06-23 15:42:17 +08004994 if (right->getMemoryQualifier().writeonly)
4995 {
4996 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4997 return false;
4998 }
4999
5000 if (left->getMemoryQualifier().writeonly)
5001 {
5002 switch (op)
5003 {
5004 case EOpAssign:
5005 case EOpInitialize:
5006 case EOpIndexDirect:
5007 case EOpIndexIndirect:
5008 case EOpIndexDirectStruct:
5009 case EOpIndexDirectInterfaceBlock:
5010 break;
5011 default:
5012 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5013 return false;
5014 }
5015 }
5016
Olli Etuaho244be012016-08-18 15:26:02 +03005017 if (left->getType().getStruct() || right->getType().getStruct())
5018 {
5019 switch (op)
5020 {
5021 case EOpIndexDirectStruct:
5022 ASSERT(left->getType().getStruct());
5023 break;
5024 case EOpEqual:
5025 case EOpNotEqual:
5026 case EOpAssign:
5027 case EOpInitialize:
5028 if (left->getType() != right->getType())
5029 {
5030 return false;
5031 }
5032 break;
5033 default:
5034 error(loc, "Invalid operation for structs", GetOperatorString(op));
5035 return false;
5036 }
5037 }
5038
Olli Etuaho94050052017-05-08 14:17:44 +03005039 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5040 {
5041 switch (op)
5042 {
5043 case EOpIndexDirectInterfaceBlock:
5044 ASSERT(left->getType().getInterfaceBlock());
5045 break;
5046 default:
5047 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5048 return false;
5049 }
5050 }
5051
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005052 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005053 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005054 error(loc, "array / non-array mismatch", GetOperatorString(op));
5055 return false;
5056 }
5057
5058 if (left->isArray())
5059 {
5060 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005061 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005062 {
5063 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5064 return false;
5065 }
5066
Olli Etuahoe79904c2015-03-18 16:56:42 +02005067 switch (op)
5068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005069 case EOpEqual:
5070 case EOpNotEqual:
5071 case EOpAssign:
5072 case EOpInitialize:
5073 break;
5074 default:
5075 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5076 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005077 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005078 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005079 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005080 {
5081 error(loc, "array size mismatch", GetOperatorString(op));
5082 return false;
5083 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005084 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005085
5086 // Check ops which require integer / ivec parameters
5087 bool isBitShift = false;
5088 switch (op)
5089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005090 case EOpBitShiftLeft:
5091 case EOpBitShiftRight:
5092 case EOpBitShiftLeftAssign:
5093 case EOpBitShiftRightAssign:
5094 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5095 // check that the basic type is an integer type.
5096 isBitShift = true;
5097 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5098 {
5099 return false;
5100 }
5101 break;
5102 case EOpBitwiseAnd:
5103 case EOpBitwiseXor:
5104 case EOpBitwiseOr:
5105 case EOpBitwiseAndAssign:
5106 case EOpBitwiseXorAssign:
5107 case EOpBitwiseOrAssign:
5108 // It is enough to check the type of only one operand, since later it
5109 // is checked that the operand types match.
5110 if (!IsInteger(left->getBasicType()))
5111 {
5112 return false;
5113 }
5114 break;
5115 default:
5116 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005117 }
5118
5119 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5120 // So the basic type should usually match.
5121 if (!isBitShift && left->getBasicType() != right->getBasicType())
5122 {
5123 return false;
5124 }
5125
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005126 // Check that:
5127 // 1. Type sizes match exactly on ops that require that.
5128 // 2. Restrictions for structs that contain arrays or samplers are respected.
5129 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005131 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005132 case EOpAssign:
5133 case EOpInitialize:
5134 case EOpEqual:
5135 case EOpNotEqual:
5136 // ESSL 1.00 sections 5.7, 5.8, 5.9
5137 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5138 {
5139 error(loc, "undefined operation for structs containing arrays",
5140 GetOperatorString(op));
5141 return false;
5142 }
5143 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5144 // we interpret the spec so that this extends to structs containing samplers,
5145 // similarly to ESSL 1.00 spec.
5146 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5147 left->getType().isStructureContainingSamplers())
5148 {
5149 error(loc, "undefined operation for structs containing samplers",
5150 GetOperatorString(op));
5151 return false;
5152 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005153
Olli Etuahoe1805592017-01-02 16:41:20 +00005154 if ((left->getNominalSize() != right->getNominalSize()) ||
5155 (left->getSecondarySize() != right->getSecondarySize()))
5156 {
5157 error(loc, "dimension mismatch", GetOperatorString(op));
5158 return false;
5159 }
5160 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005161 case EOpLessThan:
5162 case EOpGreaterThan:
5163 case EOpLessThanEqual:
5164 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005165 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005167 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005168 return false;
5169 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005170 break;
5171 case EOpAdd:
5172 case EOpSub:
5173 case EOpDiv:
5174 case EOpIMod:
5175 case EOpBitShiftLeft:
5176 case EOpBitShiftRight:
5177 case EOpBitwiseAnd:
5178 case EOpBitwiseXor:
5179 case EOpBitwiseOr:
5180 case EOpAddAssign:
5181 case EOpSubAssign:
5182 case EOpDivAssign:
5183 case EOpIModAssign:
5184 case EOpBitShiftLeftAssign:
5185 case EOpBitShiftRightAssign:
5186 case EOpBitwiseAndAssign:
5187 case EOpBitwiseXorAssign:
5188 case EOpBitwiseOrAssign:
5189 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5190 {
5191 return false;
5192 }
5193
5194 // Are the sizes compatible?
5195 if (left->getNominalSize() != right->getNominalSize() ||
5196 left->getSecondarySize() != right->getSecondarySize())
5197 {
5198 // If the nominal sizes of operands do not match:
5199 // One of them must be a scalar.
5200 if (!left->isScalar() && !right->isScalar())
5201 return false;
5202
5203 // In the case of compound assignment other than multiply-assign,
5204 // the right side needs to be a scalar. Otherwise a vector/matrix
5205 // would be assigned to a scalar. A scalar can't be shifted by a
5206 // vector either.
5207 if (!right->isScalar() &&
5208 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5209 return false;
5210 }
5211 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005212 default:
5213 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005214 }
5215
Olli Etuahod6b14282015-03-17 14:31:35 +02005216 return true;
5217}
5218
Olli Etuaho1dded802016-08-18 18:13:13 +03005219bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5220 const TType &left,
5221 const TType &right)
5222{
5223 switch (op)
5224 {
5225 case EOpMul:
5226 case EOpMulAssign:
5227 return left.getNominalSize() == right.getNominalSize() &&
5228 left.getSecondarySize() == right.getSecondarySize();
5229 case EOpVectorTimesScalar:
5230 return true;
5231 case EOpVectorTimesScalarAssign:
5232 ASSERT(!left.isMatrix() && !right.isMatrix());
5233 return left.isVector() && !right.isVector();
5234 case EOpVectorTimesMatrix:
5235 return left.getNominalSize() == right.getRows();
5236 case EOpVectorTimesMatrixAssign:
5237 ASSERT(!left.isMatrix() && right.isMatrix());
5238 return left.isVector() && left.getNominalSize() == right.getRows() &&
5239 left.getNominalSize() == right.getCols();
5240 case EOpMatrixTimesVector:
5241 return left.getCols() == right.getNominalSize();
5242 case EOpMatrixTimesScalar:
5243 return true;
5244 case EOpMatrixTimesScalarAssign:
5245 ASSERT(left.isMatrix() && !right.isMatrix());
5246 return !right.isVector();
5247 case EOpMatrixTimesMatrix:
5248 return left.getCols() == right.getRows();
5249 case EOpMatrixTimesMatrixAssign:
5250 ASSERT(left.isMatrix() && right.isMatrix());
5251 // We need to check two things:
5252 // 1. The matrix multiplication step is valid.
5253 // 2. The result will have the same number of columns as the lvalue.
5254 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5255
5256 default:
5257 UNREACHABLE();
5258 return false;
5259 }
5260}
5261
Jamie Madillb98c3a82015-07-23 14:26:04 -04005262TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5263 TIntermTyped *left,
5264 TIntermTyped *right,
5265 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005266{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005267 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005268 return nullptr;
5269
Olli Etuahofc1806e2015-03-17 13:03:11 +02005270 switch (op)
5271 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005272 case EOpEqual:
5273 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005274 case EOpLessThan:
5275 case EOpGreaterThan:
5276 case EOpLessThanEqual:
5277 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 break;
5279 case EOpLogicalOr:
5280 case EOpLogicalXor:
5281 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005282 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5283 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005284 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005285 {
5286 return nullptr;
5287 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005288 // Basic types matching should have been already checked.
5289 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 break;
5291 case EOpAdd:
5292 case EOpSub:
5293 case EOpDiv:
5294 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005295 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5296 !right->getType().getStruct());
5297 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005298 {
5299 return nullptr;
5300 }
5301 break;
5302 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005303 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5304 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005305 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005306 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005307 {
5308 return nullptr;
5309 }
5310 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 default:
5312 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005313 }
5314
Olli Etuaho1dded802016-08-18 18:13:13 +03005315 if (op == EOpMul)
5316 {
5317 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5318 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5319 {
5320 return nullptr;
5321 }
5322 }
5323
Olli Etuaho3fdec912016-08-18 15:08:06 +03005324 TIntermBinary *node = new TIntermBinary(op, left, right);
5325 node->setLine(loc);
5326
Olli Etuaho3fdec912016-08-18 15:08:06 +03005327 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005328 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005329}
5330
Jamie Madillb98c3a82015-07-23 14:26:04 -04005331TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5332 TIntermTyped *left,
5333 TIntermTyped *right,
5334 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005335{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005336 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005337 if (node == 0)
5338 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005339 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5340 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005341 return left;
5342 }
5343 return node;
5344}
5345
Jamie Madillb98c3a82015-07-23 14:26:04 -04005346TIntermTyped *TParseContext::addBinaryMathBooleanResult(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 Etuaho56229f12017-07-10 14:16:33 +03005352 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005353 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005354 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5355 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005356 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005357 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005358 }
5359 return node;
5360}
5361
Olli Etuaho13389b62016-10-16 11:48:18 +01005362TIntermBinary *TParseContext::createAssign(TOperator op,
5363 TIntermTyped *left,
5364 TIntermTyped *right,
5365 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005366{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005367 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005368 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005369 if (op == EOpMulAssign)
5370 {
5371 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5372 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5373 {
5374 return nullptr;
5375 }
5376 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005377 TIntermBinary *node = new TIntermBinary(op, left, right);
5378 node->setLine(loc);
5379
Olli Etuaho3fdec912016-08-18 15:08:06 +03005380 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005381 }
5382 return nullptr;
5383}
5384
Jamie Madillb98c3a82015-07-23 14:26:04 -04005385TIntermTyped *TParseContext::addAssign(TOperator op,
5386 TIntermTyped *left,
5387 TIntermTyped *right,
5388 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005389{
Olli Etuahocce89652017-06-19 16:04:09 +03005390 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005391 TIntermTyped *node = createAssign(op, left, right, loc);
5392 if (node == nullptr)
5393 {
5394 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005395 return left;
5396 }
5397 return node;
5398}
5399
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005400TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5401 TIntermTyped *right,
5402 const TSourceLoc &loc)
5403{
Corentin Wallez0d959252016-07-12 17:26:32 -04005404 // WebGL2 section 5.26, the following results in an error:
5405 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005406 if (mShaderSpec == SH_WEBGL2_SPEC &&
5407 (left->isArray() || left->getBasicType() == EbtVoid ||
5408 left->getType().isStructureContainingArrays() || right->isArray() ||
5409 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005410 {
5411 error(loc,
5412 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5413 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005414 }
5415
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005416 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5417 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5418 commaNode->getTypePointer()->setQualifier(resultQualifier);
5419 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005420}
5421
Olli Etuaho49300862015-02-20 14:54:49 +02005422TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5423{
5424 switch (op)
5425 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005426 case EOpContinue:
5427 if (mLoopNestingLevel <= 0)
5428 {
5429 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005430 }
5431 break;
5432 case EOpBreak:
5433 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5434 {
5435 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005436 }
5437 break;
5438 case EOpReturn:
5439 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5440 {
5441 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005442 }
5443 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005444 case EOpKill:
5445 if (mShaderType != GL_FRAGMENT_SHADER)
5446 {
5447 error(loc, "discard supported in fragment shaders only", "discard");
5448 }
5449 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005450 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005451 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005453 }
Olli Etuahocce89652017-06-19 16:04:09 +03005454 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005455}
5456
Jamie Madillb98c3a82015-07-23 14:26:04 -04005457TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005458 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005460{
Olli Etuahocce89652017-06-19 16:04:09 +03005461 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005462 {
Olli Etuahocce89652017-06-19 16:04:09 +03005463 ASSERT(op == EOpReturn);
5464 mFunctionReturnsValue = true;
5465 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5466 {
5467 error(loc, "void function cannot return a value", "return");
5468 }
5469 else if (*mCurrentFunctionType != expression->getType())
5470 {
5471 error(loc, "function return is not matching type:", "return");
5472 }
Olli Etuaho49300862015-02-20 14:54:49 +02005473 }
Olli Etuahocce89652017-06-19 16:04:09 +03005474 TIntermBranch *node = new TIntermBranch(op, expression);
5475 node->setLine(loc);
5476 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005477}
5478
Martin Radev84aa2dc2017-09-11 15:51:02 +03005479void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5480{
5481 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005482 const TString &name = *functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005483 bool isTextureGather = (name == "textureGather");
5484 bool isTextureGatherOffset = (name == "textureGatherOffset");
5485 if (isTextureGather || isTextureGatherOffset)
5486 {
5487 TIntermNode *componentNode = nullptr;
5488 TIntermSequence *arguments = functionCall->getSequence();
5489 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5490 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5491 ASSERT(sampler != nullptr);
5492 switch (sampler->getBasicType())
5493 {
5494 case EbtSampler2D:
5495 case EbtISampler2D:
5496 case EbtUSampler2D:
5497 case EbtSampler2DArray:
5498 case EbtISampler2DArray:
5499 case EbtUSampler2DArray:
5500 if ((isTextureGather && arguments->size() == 3u) ||
5501 (isTextureGatherOffset && arguments->size() == 4u))
5502 {
5503 componentNode = arguments->back();
5504 }
5505 break;
5506 case EbtSamplerCube:
5507 case EbtISamplerCube:
5508 case EbtUSamplerCube:
5509 ASSERT(!isTextureGatherOffset);
5510 if (arguments->size() == 3u)
5511 {
5512 componentNode = arguments->back();
5513 }
5514 break;
5515 case EbtSampler2DShadow:
5516 case EbtSampler2DArrayShadow:
5517 case EbtSamplerCubeShadow:
5518 break;
5519 default:
5520 UNREACHABLE();
5521 break;
5522 }
5523 if (componentNode)
5524 {
5525 const TIntermConstantUnion *componentConstantUnion =
5526 componentNode->getAsConstantUnion();
5527 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5528 {
5529 error(functionCall->getLine(), "Texture component must be a constant expression",
5530 name.c_str());
5531 }
5532 else
5533 {
5534 int component = componentConstantUnion->getIConst(0);
5535 if (component < 0 || component > 3)
5536 {
5537 error(functionCall->getLine(), "Component must be in the range [0;3]",
5538 name.c_str());
5539 }
5540 }
5541 }
5542 }
5543}
5544
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005545void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5546{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005547 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005548 const TString &name = *functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005549 TIntermNode *offset = nullptr;
5550 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005551 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005552 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5553 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5554 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005555 {
5556 offset = arguments->back();
5557 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005558 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 {
5560 // A bias parameter might follow the offset parameter.
5561 ASSERT(arguments->size() >= 3);
5562 offset = (*arguments)[2];
5563 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005564 else if (name == "textureGatherOffset")
5565 {
5566 ASSERT(arguments->size() >= 3u);
5567 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5568 ASSERT(sampler != nullptr);
5569 switch (sampler->getBasicType())
5570 {
5571 case EbtSampler2D:
5572 case EbtISampler2D:
5573 case EbtUSampler2D:
5574 case EbtSampler2DArray:
5575 case EbtISampler2DArray:
5576 case EbtUSampler2DArray:
5577 offset = (*arguments)[2];
5578 break;
5579 case EbtSampler2DShadow:
5580 case EbtSampler2DArrayShadow:
5581 offset = (*arguments)[3];
5582 break;
5583 default:
5584 UNREACHABLE();
5585 break;
5586 }
5587 useTextureGatherOffsetConstraints = true;
5588 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005589 if (offset != nullptr)
5590 {
5591 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5592 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5593 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005594 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005595 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 }
5597 else
5598 {
5599 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5600 size_t size = offsetConstantUnion->getType().getObjectSize();
5601 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005602 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5603 : mMinProgramTexelOffset;
5604 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5605 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005606 for (size_t i = 0u; i < size; ++i)
5607 {
5608 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005609 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005610 {
5611 std::stringstream tokenStream;
5612 tokenStream << offsetValue;
5613 std::string token = tokenStream.str();
5614 error(offset->getLine(), "Texture offset value out of valid range",
5615 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005616 }
5617 }
5618 }
5619 }
5620}
5621
Jiajia Qina3106c52017-11-03 09:39:39 +08005622void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5623{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005624 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5625 const TString &name = *functionCall->getFunction()->name();
Jiajia Qina3106c52017-11-03 09:39:39 +08005626 if (IsAtomicBuiltin(name))
5627 {
5628 TIntermSequence *arguments = functionCall->getSequence();
5629 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5630
5631 if (IsBufferOrSharedVariable(memNode))
5632 {
5633 return;
5634 }
5635
5636 while (memNode->getAsBinaryNode())
5637 {
5638 memNode = memNode->getAsBinaryNode()->getLeft();
5639 if (IsBufferOrSharedVariable(memNode))
5640 {
5641 return;
5642 }
5643 }
5644
5645 error(memNode->getLine(),
5646 "The value passed to the mem argument of an atomic memory function does not "
5647 "correspond to a buffer or shared variable.",
Olli Etuaho1bb85282017-12-14 13:39:53 +02005648 functionCall->getFunction()->name()->c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005649 }
5650}
5651
Martin Radev2cc85b32016-08-05 16:22:53 +03005652// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5653void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5654{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005655 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuaho1bb85282017-12-14 13:39:53 +02005656 const TString &name = *functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005657
5658 if (name.compare(0, 5, "image") == 0)
5659 {
5660 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005661 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005662
Olli Etuaho485eefd2017-02-14 17:40:06 +00005663 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005664
5665 if (name.compare(5, 5, "Store") == 0)
5666 {
5667 if (memoryQualifier.readonly)
5668 {
5669 error(imageNode->getLine(),
5670 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005671 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005672 }
5673 }
5674 else if (name.compare(5, 4, "Load") == 0)
5675 {
5676 if (memoryQualifier.writeonly)
5677 {
5678 error(imageNode->getLine(),
5679 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005680 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005681 }
5682 }
5683 }
5684}
5685
5686// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5687void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5688 const TFunction *functionDefinition,
5689 const TIntermAggregate *functionCall)
5690{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005691 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005692
5693 const TIntermSequence &arguments = *functionCall->getSequence();
5694
5695 ASSERT(functionDefinition->getParamCount() == arguments.size());
5696
5697 for (size_t i = 0; i < arguments.size(); ++i)
5698 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005699 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5700 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005701 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5702 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5703
5704 if (IsImage(functionArgumentType.getBasicType()))
5705 {
5706 const TMemoryQualifier &functionArgumentMemoryQualifier =
5707 functionArgumentType.getMemoryQualifier();
5708 const TMemoryQualifier &functionParameterMemoryQualifier =
5709 functionParameterType.getMemoryQualifier();
5710 if (functionArgumentMemoryQualifier.readonly &&
5711 !functionParameterMemoryQualifier.readonly)
5712 {
5713 error(functionCall->getLine(),
5714 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005715 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005716 }
5717
5718 if (functionArgumentMemoryQualifier.writeonly &&
5719 !functionParameterMemoryQualifier.writeonly)
5720 {
5721 error(functionCall->getLine(),
5722 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005723 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005724 }
Martin Radev049edfa2016-11-11 14:35:37 +02005725
5726 if (functionArgumentMemoryQualifier.coherent &&
5727 !functionParameterMemoryQualifier.coherent)
5728 {
5729 error(functionCall->getLine(),
5730 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005731 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005732 }
5733
5734 if (functionArgumentMemoryQualifier.volatileQualifier &&
5735 !functionParameterMemoryQualifier.volatileQualifier)
5736 {
5737 error(functionCall->getLine(),
5738 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005739 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005740 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005741 }
5742 }
5743}
5744
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005745TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005746{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005747 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005748}
5749
5750TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005751 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005752 TIntermNode *thisNode,
5753 const TSourceLoc &loc)
5754{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005755 if (thisNode != nullptr)
5756 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005757 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005758 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005759
5760 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005761 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005762 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005763 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005764 }
5765 else
5766 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005767 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005768 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005769 }
5770}
5771
Olli Etuaho0c371002017-12-13 17:00:25 +04005772TIntermTyped *TParseContext::addMethod(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005773 TIntermSequence *arguments,
5774 TIntermNode *thisNode,
5775 const TSourceLoc &loc)
5776{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005777 TIntermTyped *typedThis = thisNode->getAsTyped();
5778 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5779 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5780 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005781 // So accessing fnCall->name() below is safe.
Olli Etuaho0c371002017-12-13 17:00:25 +04005782 if (*name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005783 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005784 error(loc, "invalid method", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005785 }
5786 else if (!arguments->empty())
5787 {
5788 error(loc, "method takes no parameters", "length");
5789 }
5790 else if (typedThis == nullptr || !typedThis->isArray())
5791 {
5792 error(loc, "length can only be called on arrays", "length");
5793 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005794 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5795 mGeometryShaderInputPrimitiveType == EptUndefined)
5796 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005797 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005798 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5799 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005800 else
5801 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005802 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5803 node->setLine(loc);
5804 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005805 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005806 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807}
5808
Olli Etuaho0c371002017-12-13 17:00:25 +04005809TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810 TIntermSequence *arguments,
5811 const TSourceLoc &loc)
5812{
Olli Etuaho0c371002017-12-13 17:00:25 +04005813 ASSERT(name);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005814 // First find by unmangled name to check whether the function name has been
5815 // hidden by a variable name or struct typename.
5816 // If a function is found, check for one with a matching argument list.
5817 bool builtIn;
Olli Etuaho0c371002017-12-13 17:00:25 +04005818 const TSymbol *symbol = symbolTable.find(*name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 if (symbol != nullptr && !symbol->isFunction())
5820 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005821 error(loc, "function name expected", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 }
5823 else
5824 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005825 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(*name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005826 mShaderVersion, &builtIn);
5827 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005828 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005829 error(loc, "no matching overloaded function found", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005830 }
5831 else
5832 {
5833 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 //
5835 // A declared function.
5836 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005837 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005839 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005841 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005842 if (builtIn && op != EOpNull)
5843 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 if (fnCandidate->getParamCount() == 1)
5846 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848 TIntermNode *unaryParamNode = arguments->front();
5849 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005850 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005852 }
5853 else
5854 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005856 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858
5859 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005860 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305861
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005862 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305863 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005864 // See if we can constant fold a built-in. Note that this may be possible
5865 // even if it is not const-qualified.
5866 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305867 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005868 else
5869 {
5870 return callNode;
5871 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005872 }
5873 }
5874 else
5875 {
5876 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005877 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005878
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005879 // If builtIn == false, the function is user defined - could be an overloaded
5880 // built-in as well.
5881 // if builtIn == true, it's a builtIn function with no op associated with it.
5882 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005883 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005884 {
Olli Etuahofe486322017-03-21 09:30:54 +00005885 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005887 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005888 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005889 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005890 }
5891 else
5892 {
Olli Etuahofe486322017-03-21 09:30:54 +00005893 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005894 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005895 }
5896
Jiajia Qinbc585152017-06-23 15:42:17 +08005897 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005898
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005899 callNode->setLine(loc);
5900
5901 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005902 }
5903 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005904 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005905
5906 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005907 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005908}
5909
Jamie Madillb98c3a82015-07-23 14:26:04 -04005910TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005911 TIntermTyped *trueExpression,
5912 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005913 const TSourceLoc &loc)
5914{
Olli Etuaho56229f12017-07-10 14:16:33 +03005915 if (!checkIsScalarBool(loc, cond))
5916 {
5917 return falseExpression;
5918 }
Olli Etuaho52901742015-04-15 13:42:45 +03005919
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005920 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005921 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005922 std::stringstream reasonStream;
5923 reasonStream << "mismatching ternary operator operand types '"
5924 << trueExpression->getCompleteString() << " and '"
5925 << falseExpression->getCompleteString() << "'";
5926 std::string reason = reasonStream.str();
5927 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005928 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005929 }
Olli Etuahode318b22016-10-25 16:18:25 +01005930 if (IsOpaqueType(trueExpression->getBasicType()))
5931 {
5932 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005933 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005934 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5935 // Note that structs containing opaque types don't need to be checked as structs are
5936 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005937 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005938 return falseExpression;
5939 }
5940
Jiajia Qinbc585152017-06-23 15:42:17 +08005941 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5942 falseExpression->getMemoryQualifier().writeonly)
5943 {
5944 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5945 return falseExpression;
5946 }
5947
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005948 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005949 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005950 // ESSL 3.00.6 section 5.7:
5951 // Ternary operator support is optional for arrays. No certainty that it works across all
5952 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5953 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005954 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005955 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005956 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005957 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005958 }
Olli Etuaho94050052017-05-08 14:17:44 +03005959 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5960 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005961 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005962 return falseExpression;
5963 }
5964
Corentin Wallez0d959252016-07-12 17:26:32 -04005965 // WebGL2 section 5.26, the following results in an error:
5966 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005967 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005968 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005969 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005970 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005971 }
5972
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005973 // Note that the node resulting from here can be a constant union without being qualified as
5974 // constant.
5975 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5976 node->setLine(loc);
5977
5978 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005979}
Olli Etuaho49300862015-02-20 14:54:49 +02005980
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005981//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005982// Parse an array of strings using yyparse.
5983//
5984// Returns 0 for success.
5985//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005986int PaParseStrings(size_t count,
5987 const char *const string[],
5988 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305989 TParseContext *context)
5990{
Yunchao He4f285442017-04-21 12:15:49 +08005991 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005992 return 1;
5993
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005994 if (glslang_initialize(context))
5995 return 1;
5996
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005997 int error = glslang_scan(count, string, length, context);
5998 if (!error)
5999 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006000
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006001 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006002
alokp@chromium.org6b495712012-06-29 00:06:58 +00006003 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006004}
Jamie Madill45bcc782016-11-07 13:58:48 -05006005
6006} // namespace sh