blob: 1ead5a5dcf6c09f574f6e88dab29b47c3d0ab980 [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 Etuahofe486322017-03-21 09:30:54 +00003159 TIntermFunctionPrototype *prototype =
3160 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003161 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3162 // point to the data that already exists in the symbol table.
3163 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3164 prototype->setLine(location);
3165
3166 for (size_t i = 0; i < function.getParamCount(); i++)
3167 {
3168 const TConstParameter &param = function.getParam(i);
3169
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 TIntermSymbol *symbol = nullptr;
3171
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003172 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3173 // be used for unused args).
3174 if (param.name != nullptr)
3175 {
Olli Etuaho195be942017-12-04 23:40:14 +02003176 TVariable *variable =
3177 new TVariable(&symbolTable, param.name, *param.type, SymbolType::UserDefined);
3178 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003179 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003180 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003181 {
Olli Etuaho195be942017-12-04 23:40:14 +02003182 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003183 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003184 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003185 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003186 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003187 // Unsized type of a named parameter should have already been checked and sanitized.
3188 ASSERT(!param.type->isUnsizedArray());
3189 }
3190 else
3191 {
3192 if (param.type->isUnsizedArray())
3193 {
3194 error(location, "function parameter array must be sized at compile time", "[]");
3195 // We don't need to size the arrays since the parameter is unnamed and hence
3196 // inaccessible.
3197 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003198 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003199 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003200 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003201 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3202 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003203 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003204 new TVariable(&symbolTable, nullptr, *param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003205 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003206 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003207 symbol->setLine(location);
3208 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003209 }
3210 return prototype;
3211}
3212
Olli Etuaho16c745a2017-01-16 17:02:27 +00003213TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3214 const TFunction &parsedFunction,
3215 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003216{
Olli Etuaho476197f2016-10-11 13:59:08 +01003217 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3218 // first declaration. Either way the instance in the symbol table is used to track whether the
3219 // function is declared multiple times.
3220 TFunction *function = static_cast<TFunction *>(
3221 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3222 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003223 {
3224 // ESSL 1.00.17 section 4.2.7.
3225 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3226 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003227 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003228 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003229
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003230 TIntermFunctionPrototype *prototype =
3231 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003232
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003233 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003234
3235 if (!symbolTable.atGlobalLevel())
3236 {
3237 // ESSL 3.00.4 section 4.2.4.
3238 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003239 }
3240
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003241 return prototype;
3242}
3243
Olli Etuaho336b1472016-10-05 16:37:55 +01003244TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003245 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003246 TIntermBlock *functionBody,
3247 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003248{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003249 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003250 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3251 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003252 error(location, "function does not return a value:",
3253 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003254 }
3255
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256 if (functionBody == nullptr)
3257 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003258 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003259 functionBody->setLine(location);
3260 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003261 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003262 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003263 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003264
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003265 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003266 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003267}
3268
Olli Etuaho476197f2016-10-11 13:59:08 +01003269void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3270 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003271 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003272{
Olli Etuaho476197f2016-10-11 13:59:08 +01003273 ASSERT(function);
3274 ASSERT(*function);
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003275 ASSERT((*function)->name());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003276 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003277 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003278
3279 if (builtIn)
3280 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003281 error(location, "built-in functions cannot be redefined", (*function)->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003282 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003283 else
Jamie Madill185fb402015-06-12 15:48:48 -04003284 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003285 TFunction *prevDec = static_cast<TFunction *>(
3286 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3287
3288 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3289 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3290 // occurance.
3291 if (*function != prevDec)
3292 {
3293 // Swap the parameters of the previous declaration to the parameters of the function
3294 // definition (parameter names may differ).
3295 prevDec->swapParameters(**function);
3296
3297 // The function definition will share the same symbol as any previous declaration.
3298 *function = prevDec;
3299 }
3300
3301 if ((*function)->isDefined())
3302 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003303 error(location, "function already has a body", (*function)->name()->c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003304 }
3305
3306 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003307 }
Jamie Madill185fb402015-06-12 15:48:48 -04003308
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003309 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003310 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003311 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003312
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003313 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003314 setLoopNestingLevel(0);
3315}
3316
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003318{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003319 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003320 // We don't know at this point whether this is a function definition or a prototype.
3321 // The definition production code will check for redefinitions.
3322 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003323 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003324 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3325 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003326 //
3327 TFunction *prevDec =
3328 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303329
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003330 ASSERT(function->name() != nullptr);
3331
Olli Etuahod80f2942017-11-06 12:44:45 +02003332 for (size_t i = 0u; i < function->getParamCount(); ++i)
3333 {
3334 auto &param = function->getParam(i);
3335 if (param.type->isStructSpecifier())
3336 {
3337 // ESSL 3.00.6 section 12.10.
3338 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003339 function->name()->c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003340 }
3341 }
3342
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003343 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003344 function->name()->c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303345 {
Martin Radevda6254b2016-12-14 17:00:36 +02003346 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303347 // Therefore overloading or redefining builtin functions is an error.
3348 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003349 function->name()->c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303350 }
3351 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003352 {
3353 if (prevDec->getReturnType() != function->getReturnType())
3354 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003355 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003356 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003357 }
3358 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003360 if (prevDec->getParam(i).type->getQualifier() !=
3361 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003362 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003363 error(location,
3364 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003365 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003366 }
3367 }
3368 }
3369
3370 //
3371 // Check for previously declared variables using the same name.
3372 //
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003373 TSymbol *prevSym = symbolTable.find(*function->name(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003374 if (prevSym)
3375 {
3376 if (!prevSym->isFunction())
3377 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003378 error(location, "redefinition of a function", function->name()->c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003379 }
3380 }
3381 else
3382 {
3383 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003384 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003385 }
3386
3387 // We're at the inner scope level of the function's arguments and body statement.
3388 // Add the function prototype to the surrounding scope instead.
3389 symbolTable.getOuterLevel()->insert(function);
3390
Olli Etuaho78d13742017-01-18 13:06:10 +00003391 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003392 if (*function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003393 {
3394 if (function->getParamCount() > 0)
3395 {
3396 error(location, "function cannot take any parameter(s)", "main");
3397 }
3398 if (function->getReturnType().getBasicType() != EbtVoid)
3399 {
3400 error(location, "main function cannot return a value",
3401 function->getReturnType().getBasicString());
3402 }
3403 }
3404
Jamie Madill185fb402015-06-12 15:48:48 -04003405 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003406 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3407 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003408 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3409 //
3410 return function;
3411}
3412
Olli Etuaho9de84a52016-06-14 17:36:01 +03003413TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3414 const TString *name,
3415 const TSourceLoc &location)
3416{
3417 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3418 {
3419 error(location, "no qualifiers allowed for function return",
3420 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003421 }
3422 if (!type.layoutQualifier.isEmpty())
3423 {
3424 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003425 }
jchen10cc2a10e2017-05-03 14:05:12 +08003426 // make sure an opaque type is not involved as well...
3427 std::string reason(getBasicString(type.getBasicType()));
3428 reason += "s can't be function return values";
3429 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003430 if (mShaderVersion < 300)
3431 {
3432 // Array return values are forbidden, but there's also no valid syntax for declaring array
3433 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003434 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003435
3436 if (type.isStructureContainingArrays())
3437 {
3438 // ESSL 1.00.17 section 6.1 Function Definitions
3439 error(location, "structures containing arrays can't be function return values",
3440 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003441 }
3442 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003443
3444 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003445 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003446}
3447
Olli Etuahocce89652017-06-19 16:04:09 +03003448TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3449{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003450 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003451 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3452 // really only interested in the mangled name of the function to look up the actual function
3453 // from the symbol table. If we just had the name string and the types of the parameters that
3454 // would be enough, but TFunction carries a lot of extra information in addition to that.
3455 // Besides function calls we do have to store constructor calls in the same data structure, for
3456 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003457 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003458}
3459
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003460TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003461{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003462 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003463 {
3464 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3465 "[]");
3466 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003467 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003468 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003469 error(publicType.getLine(), "constructor can't be a structure definition",
3470 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003471 }
3472
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003473 TType *type = new TType(publicType);
3474 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003475 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003476 error(publicType.getLine(), "cannot construct this type",
3477 getBasicString(publicType.getBasicType()));
3478 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003479 }
3480
Olli Etuaho0c371002017-12-13 17:00:25 +04003481 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003482}
3483
Olli Etuaho55bde912017-10-25 13:41:13 +03003484void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3485 const char *errorMessage,
3486 const char *token,
3487 TType *arrayType)
3488{
3489 if (arrayType->isUnsizedArray())
3490 {
3491 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003492 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003493 }
3494}
3495
3496TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003497 const TString *name,
3498 const TSourceLoc &nameLoc)
3499{
Olli Etuaho55bde912017-10-25 13:41:13 +03003500 ASSERT(type);
3501 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3502 type);
3503 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003504 {
3505 error(nameLoc, "illegal use of type 'void'", name->c_str());
3506 }
3507 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003508 TParameter param = {name, type};
3509 return param;
3510}
3511
Olli Etuaho55bde912017-10-25 13:41:13 +03003512TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3513 const TString *name,
3514 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003515{
Olli Etuaho55bde912017-10-25 13:41:13 +03003516 TType *type = new TType(publicType);
3517 return parseParameterDeclarator(type, name, nameLoc);
3518}
3519
3520TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3521 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003522 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003523 const TSourceLoc &arrayLoc,
3524 TPublicType *elementType)
3525{
3526 checkArrayElementIsNotArray(arrayLoc, *elementType);
3527 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003528 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003529 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003530}
3531
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003532bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3533 TType type,
3534 const TSourceLoc &line)
3535{
3536 if (arguments->empty())
3537 {
3538 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3539 return false;
3540 }
3541 for (TIntermNode *arg : *arguments)
3542 {
3543 TIntermTyped *element = arg->getAsTyped();
3544 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003545 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3546 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003547 {
3548 error(line, "constructing from a non-dereferenced array", "constructor");
3549 return false;
3550 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003551 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003552 {
3553 if (dimensionalityFromElement == 1u)
3554 {
3555 error(line, "implicitly sized array of arrays constructor argument is not an array",
3556 "constructor");
3557 }
3558 else
3559 {
3560 error(line,
3561 "implicitly sized array of arrays constructor argument dimensionality is too "
3562 "low",
3563 "constructor");
3564 }
3565 return false;
3566 }
3567 }
3568 return true;
3569}
3570
Jamie Madillb98c3a82015-07-23 14:26:04 -04003571// This function is used to test for the correctness of the parameters passed to various constructor
3572// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003573//
Olli Etuaho856c4972016-08-08 11:38:39 +03003574// 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 +00003575//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003576TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003577 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303578 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003579{
Olli Etuaho856c4972016-08-08 11:38:39 +03003580 if (type.isUnsizedArray())
3581 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003582 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003583 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003584 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003585 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003586 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003587 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3588 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003589 if (type.getOutermostArraySize() == 0u)
3590 {
3591 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3592 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003593 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003594 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003595 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003596 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003597 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003598 }
3599 }
3600 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003601 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003602
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003603 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003604 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003605 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003606 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003607
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003608 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003609 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003610
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003611 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3612 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003613 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003614 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003615 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003616 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003617}
3618
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003619//
3620// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003621// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003622//
Olli Etuaho13389b62016-10-16 11:48:18 +01003623TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003624 const TTypeQualifierBuilder &typeQualifierBuilder,
3625 const TSourceLoc &nameLine,
3626 const TString &blockName,
3627 TFieldList *fieldList,
3628 const TString *instanceName,
3629 const TSourceLoc &instanceLine,
3630 TIntermTyped *arrayIndex,
3631 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003632{
Olli Etuaho856c4972016-08-08 11:38:39 +03003633 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003634
Olli Etuaho77ba4082016-12-16 12:01:18 +00003635 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003636
Jiajia Qinbc585152017-06-23 15:42:17 +08003637 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003639 error(typeQualifier.line,
3640 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3641 "3.10",
3642 getQualifierString(typeQualifier.qualifier));
3643 }
3644 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3645 {
3646 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003647 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003648 }
3649
Martin Radev70866b82016-07-22 15:27:42 +03003650 if (typeQualifier.invariant)
3651 {
3652 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3653 }
3654
Jiajia Qinbc585152017-06-23 15:42:17 +08003655 if (typeQualifier.qualifier != EvqBuffer)
3656 {
3657 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3658 }
Olli Etuaho43364892017-02-13 16:00:12 +00003659
jchen10af713a22017-04-19 09:10:56 +08003660 // add array index
3661 unsigned int arraySize = 0;
3662 if (arrayIndex != nullptr)
3663 {
3664 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3665 }
3666
3667 if (mShaderVersion < 310)
3668 {
3669 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3670 }
3671 else
3672 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003673 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3674 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003675 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003676
Andrei Volykhina5527072017-03-22 16:46:30 +03003677 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3678
Jamie Madill099c0f32013-06-20 11:55:52 -04003679 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003680 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003681 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3682 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003683
Jamie Madill099c0f32013-06-20 11:55:52 -04003684 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3685 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003686 if (typeQualifier.qualifier == EvqUniform)
3687 {
3688 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3689 }
3690 else if (typeQualifier.qualifier == EvqBuffer)
3691 {
3692 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3693 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003694 }
3695
Jamie Madill1566ef72013-06-20 11:55:54 -04003696 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3697 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003698 if (typeQualifier.qualifier == EvqUniform)
3699 {
3700 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3701 }
3702 else if (typeQualifier.qualifier == EvqBuffer)
3703 {
3704 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3705 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003706 }
3707
Olli Etuaho856c4972016-08-08 11:38:39 +03003708 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003709
Martin Radev2cc85b32016-08-05 16:22:53 +03003710 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3711
Jamie Madill98493dd2013-07-08 14:39:03 -04003712 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303713 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3714 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303716 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003717 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303718 {
jchen10cc2a10e2017-05-03 14:05:12 +08003719 std::string reason("unsupported type - ");
3720 reason += fieldType->getBasicString();
3721 reason += " types are not allowed in interface blocks";
3722 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003723 }
3724
Jamie Madill98493dd2013-07-08 14:39:03 -04003725 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003726 switch (qualifier)
3727 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003729 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003731 if (typeQualifier.qualifier == EvqBuffer)
3732 {
3733 error(field->line(), "invalid qualifier on shader storage block member",
3734 getQualifierString(qualifier));
3735 }
3736 break;
3737 case EvqBuffer:
3738 if (typeQualifier.qualifier == EvqUniform)
3739 {
3740 error(field->line(), "invalid qualifier on uniform block member",
3741 getQualifierString(qualifier));
3742 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 break;
3744 default:
3745 error(field->line(), "invalid qualifier on interface block member",
3746 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003747 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003748 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003749
Martin Radev70866b82016-07-22 15:27:42 +03003750 if (fieldType->isInvariant())
3751 {
3752 error(field->line(), "invalid qualifier on interface block member", "invariant");
3753 }
3754
Jamie Madilla5efff92013-06-06 11:56:47 -04003755 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003756 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003757 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003758 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003759
Jamie Madill98493dd2013-07-08 14:39:03 -04003760 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003761 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003762 error(field->line(), "invalid layout qualifier: cannot be used here",
3763 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003764 }
3765
Jamie Madill98493dd2013-07-08 14:39:03 -04003766 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003767 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003768 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003769 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003770 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003771 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003772 warning(field->line(),
3773 "extraneous layout qualifier: only has an effect on matrix types",
3774 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003775 }
3776
Jamie Madill98493dd2013-07-08 14:39:03 -04003777 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003778
Olli Etuahoebee5b32017-11-23 12:56:32 +02003779 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3780 typeQualifier.qualifier != EvqBuffer)
3781 {
3782 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3783 checkIsNotUnsizedArray(field->line(),
3784 "array members of interface blocks must specify a size",
3785 field->name().c_str(), field->type());
3786 }
3787
Jiajia Qinbc585152017-06-23 15:42:17 +08003788 if (typeQualifier.qualifier == EvqBuffer)
3789 {
3790 // set memory qualifiers
3791 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3792 // qualified with a memory qualifier, it is as if all of its members were declared with
3793 // the same memory qualifier.
3794 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3795 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3796 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3797 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3798 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3799 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3800 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3801 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3802 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3803 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3804 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003805 }
3806
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003807 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3808 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003809 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3810 {
3811 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3812 }
3813
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003814 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3815 if (arrayIndex != nullptr)
3816 {
3817 interfaceBlockType.makeArray(arraySize);
3818 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003819
Olli Etuaho195be942017-12-04 23:40:14 +02003820 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003821 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3822 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003823 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003824 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3825 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003826
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003827 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003828 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003829 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003830 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303833 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003834
3835 // set parent pointer of the field variable
3836 fieldType->setInterfaceBlock(interfaceBlock);
3837
Olli Etuaho195be942017-12-04 23:40:14 +02003838 TVariable *fieldVariable =
3839 new TVariable(&symbolTable, &field->name(), *fieldType, SymbolType::UserDefined);
3840 if (symbolTable.declareVariable(fieldVariable))
Olli Etuaho0f684632017-07-13 12:42:15 +03003841 {
3842 fieldVariable->setQualifier(typeQualifier.qualifier);
3843 }
3844 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003846 error(field->line(), "redefinition of an interface block member name",
3847 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003848 }
3849 }
3850 }
3851 else
3852 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003853 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003854
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003855 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003856 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303857 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003858 error(instanceLine, "redefinition of an interface block instance name",
3859 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003860 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003861 }
3862
Olli Etuaho195be942017-12-04 23:40:14 +02003863 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3864 blockSymbol->setLine(typeQualifier.line);
3865 TIntermDeclaration *declaration = new TIntermDeclaration();
3866 declaration->appendDeclarator(blockSymbol);
3867 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003868
3869 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003870 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003871}
3872
Olli Etuaho383b7912016-08-05 11:22:59 +03003873void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003874{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003875 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003876
3877 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003878 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303879 if (mStructNestingLevel > 1)
3880 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003881 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003882 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003883}
3884
3885void TParseContext::exitStructDeclaration()
3886{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003887 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003888}
3889
Olli Etuaho8a176262016-08-16 14:23:01 +03003890void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003891{
Jamie Madillacb4b812016-11-07 13:50:29 -05003892 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303893 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003894 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003895 }
3896
Arun Patole7e7e68d2015-05-22 12:02:25 +05303897 if (field.type()->getBasicType() != EbtStruct)
3898 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003899 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003900 }
3901
3902 // We're already inside a structure definition at this point, so add
3903 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303904 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3905 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003906 ASSERT(field.type()->getStruct()->name() != nullptr);
Jamie Madill41a49272014-03-18 16:10:13 -04003907 std::stringstream reasonStream;
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003908 reasonStream << "Reference of struct type " << field.type()->getStruct()->name()->c_str()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003909 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003910 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003911 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003912 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003913 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003914}
3915
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003916//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917// Parse an array index expression
3918//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003919TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3920 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303921 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003922{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003923 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3924 {
3925 if (baseExpression->getAsSymbolNode())
3926 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303927 error(location, " left of '[' is not of type array, matrix, or vector ",
3928 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003929 }
3930 else
3931 {
3932 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3933 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003934
Olli Etuaho3ec75682017-07-05 17:02:55 +03003935 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003936 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003937
Jiawei Shaod8105a02017-08-08 09:54:36 +08003938 if (baseExpression->getQualifier() == EvqPerVertexIn)
3939 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003940 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003941 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3942 {
3943 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3944 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3945 }
3946 }
3947
Jamie Madill21c1e452014-12-29 11:33:41 -05003948 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3949
Olli Etuaho36b05142015-11-12 13:10:42 +02003950 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3951 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3952 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3953 // index is a constant expression.
3954 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3955 {
3956 if (baseExpression->isInterfaceBlock())
3957 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003958 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003959 switch (baseExpression->getQualifier())
3960 {
3961 case EvqPerVertexIn:
3962 break;
3963 case EvqUniform:
3964 case EvqBuffer:
3965 error(location,
3966 "array indexes for uniform block arrays and shader storage block arrays "
3967 "must be constant integral expressions",
3968 "[");
3969 break;
3970 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003971 // We can reach here only in error cases.
3972 ASSERT(mDiagnostics->numErrors() > 0);
3973 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003974 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003975 }
3976 else if (baseExpression->getQualifier() == EvqFragmentOut)
3977 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003978 error(location,
3979 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003980 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003981 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3982 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003983 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003984 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003985 }
3986
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003987 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003988 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003989 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3990 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3991 // constant fold expressions that are not constant expressions). The most compatible way to
3992 // handle this case is to report a warning instead of an error and force the index to be in
3993 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003994 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003995 int index = 0;
3996 if (indexConstantUnion->getBasicType() == EbtInt)
3997 {
3998 index = indexConstantUnion->getIConst(0);
3999 }
4000 else if (indexConstantUnion->getBasicType() == EbtUInt)
4001 {
4002 index = static_cast<int>(indexConstantUnion->getUConst(0));
4003 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004004
4005 int safeIndex = -1;
4006
Olli Etuahoebee5b32017-11-23 12:56:32 +02004007 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04004008 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004009 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
4010 safeIndex = 0;
4011 }
4012
4013 if (!baseExpression->getType().isUnsizedArray())
4014 {
4015 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03004016 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004017 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004018 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004019 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
4020 {
4021 outOfRangeError(outOfRangeIndexIsError, location,
4022 "array index for gl_FragData must be zero when "
4023 "GL_EXT_draw_buffers is disabled",
4024 "[]");
4025 safeIndex = 0;
4026 }
4027 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004028 }
4029 // Only do generic out-of-range check if similar error hasn't already been reported.
4030 if (safeIndex < 0)
4031 {
4032 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004033 {
4034 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4035 baseExpression->getOutermostArraySize(),
4036 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004037 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004038 else if (baseExpression->isMatrix())
4039 {
4040 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4041 baseExpression->getType().getCols(),
4042 "matrix field selection out of range");
4043 }
4044 else
4045 {
4046 ASSERT(baseExpression->isVector());
4047 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4048 baseExpression->getType().getNominalSize(),
4049 "vector field selection out of range");
4050 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004051 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004052
Olli Etuahoebee5b32017-11-23 12:56:32 +02004053 ASSERT(safeIndex >= 0);
4054 // Data of constant unions can't be changed, because it may be shared with other
4055 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4056 // sanitized object.
4057 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4058 {
4059 TConstantUnion *safeConstantUnion = new TConstantUnion();
4060 safeConstantUnion->setIConst(safeIndex);
4061 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
4062 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
4063 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004064
Olli Etuahoebee5b32017-11-23 12:56:32 +02004065 TIntermBinary *node =
4066 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4067 node->setLine(location);
4068 return node->fold(mDiagnostics);
4069 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004070 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071
4072 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4073 node->setLine(location);
4074 // Indirect indexing can never be constant folded.
4075 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004076}
4077
Olli Etuahoebee5b32017-11-23 12:56:32 +02004078int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4079 const TSourceLoc &location,
4080 int index,
4081 int arraySize,
4082 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004083{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004084 // Should not reach here with an unsized / runtime-sized array.
4085 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004086 // A negative index should already have been checked.
4087 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004088 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004089 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004090 std::stringstream reasonStream;
4091 reasonStream << reason << " '" << index << "'";
4092 std::string token = reasonStream.str();
4093 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004094 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004095 }
4096 return index;
4097}
4098
Jamie Madillb98c3a82015-07-23 14:26:04 -04004099TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4100 const TSourceLoc &dotLocation,
4101 const TString &fieldString,
4102 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004103{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104 if (baseExpression->isArray())
4105 {
4106 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 }
4109
4110 if (baseExpression->isVector())
4111 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004112 TVector<int> fieldOffsets;
4113 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4114 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004115 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004116 fieldOffsets.resize(1);
4117 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004119 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4120 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004121
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004122 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004123 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 else if (baseExpression->getBasicType() == EbtStruct)
4125 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304126 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004127 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004128 {
4129 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004130 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004131 }
4132 else
4133 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004134 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004135 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004136 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004137 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139 {
4140 fieldFound = true;
4141 break;
4142 }
4143 }
4144 if (fieldFound)
4145 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004146 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004147 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004148 TIntermBinary *node =
4149 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4150 node->setLine(dotLocation);
4151 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004152 }
4153 else
4154 {
4155 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004156 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004157 }
4158 }
4159 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004160 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004161 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304162 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004163 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004164 {
4165 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004166 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004167 }
4168 else
4169 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004170 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004172 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004173 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004174 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004175 {
4176 fieldFound = true;
4177 break;
4178 }
4179 }
4180 if (fieldFound)
4181 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004182 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004183 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004184 TIntermBinary *node =
4185 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4186 node->setLine(dotLocation);
4187 // Indexing interface blocks can never be constant folded.
4188 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004189 }
4190 else
4191 {
4192 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004193 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004194 }
4195 }
4196 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004197 else
4198 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004199 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004200 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004201 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304202 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004203 }
4204 else
4205 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304206 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004207 " field selection requires structure, vector, or interface block on left hand "
4208 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304209 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004210 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004211 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004212 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004213}
4214
Jamie Madillb98c3a82015-07-23 14:26:04 -04004215TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4216 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004217{
Jamie Madill2f294c92017-11-20 14:47:26 -05004218 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004219
4220 if (qualifierType == "shared")
4221 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004222 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004223 {
4224 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4225 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004226 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004227 }
4228 else if (qualifierType == "packed")
4229 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004230 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004231 {
4232 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4233 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004234 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004235 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004236 else if (qualifierType == "std430")
4237 {
4238 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4239 qualifier.blockStorage = EbsStd430;
4240 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004241 else if (qualifierType == "std140")
4242 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004243 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004244 }
4245 else if (qualifierType == "row_major")
4246 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004247 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004248 }
4249 else if (qualifierType == "column_major")
4250 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004251 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004252 }
4253 else if (qualifierType == "location")
4254 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004255 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4256 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004257 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004258 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004259 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004260 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4261 {
4262 qualifier.yuv = true;
4263 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004264 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004265 else if (qualifierType == "rgba32f")
4266 {
4267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4268 qualifier.imageInternalFormat = EiifRGBA32F;
4269 }
4270 else if (qualifierType == "rgba16f")
4271 {
4272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4273 qualifier.imageInternalFormat = EiifRGBA16F;
4274 }
4275 else if (qualifierType == "r32f")
4276 {
4277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4278 qualifier.imageInternalFormat = EiifR32F;
4279 }
4280 else if (qualifierType == "rgba8")
4281 {
4282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4283 qualifier.imageInternalFormat = EiifRGBA8;
4284 }
4285 else if (qualifierType == "rgba8_snorm")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4289 }
4290 else if (qualifierType == "rgba32i")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 qualifier.imageInternalFormat = EiifRGBA32I;
4294 }
4295 else if (qualifierType == "rgba16i")
4296 {
4297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4298 qualifier.imageInternalFormat = EiifRGBA16I;
4299 }
4300 else if (qualifierType == "rgba8i")
4301 {
4302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4303 qualifier.imageInternalFormat = EiifRGBA8I;
4304 }
4305 else if (qualifierType == "r32i")
4306 {
4307 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4308 qualifier.imageInternalFormat = EiifR32I;
4309 }
4310 else if (qualifierType == "rgba32ui")
4311 {
4312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4313 qualifier.imageInternalFormat = EiifRGBA32UI;
4314 }
4315 else if (qualifierType == "rgba16ui")
4316 {
4317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4318 qualifier.imageInternalFormat = EiifRGBA16UI;
4319 }
4320 else if (qualifierType == "rgba8ui")
4321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.imageInternalFormat = EiifRGBA8UI;
4324 }
4325 else if (qualifierType == "r32ui")
4326 {
4327 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4328 qualifier.imageInternalFormat = EiifR32UI;
4329 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004330 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4331 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004332 {
4333 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4334 qualifier.primitiveType = EptPoints;
4335 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004336 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4337 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004338 {
4339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4340 qualifier.primitiveType = EptLines;
4341 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004342 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4343 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004344 {
4345 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4346 qualifier.primitiveType = EptLinesAdjacency;
4347 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004348 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4349 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004350 {
4351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4352 qualifier.primitiveType = EptTriangles;
4353 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004354 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4355 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004356 {
4357 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4358 qualifier.primitiveType = EptTrianglesAdjacency;
4359 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004360 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4361 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004362 {
4363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4364 qualifier.primitiveType = EptLineStrip;
4365 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004366 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4367 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004368 {
4369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4370 qualifier.primitiveType = EptTriangleStrip;
4371 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004372
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004373 else
4374 {
4375 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004376 }
4377
Jamie Madilla5efff92013-06-06 11:56:47 -04004378 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004379}
4380
Martin Radev802abe02016-08-04 17:48:32 +03004381void TParseContext::parseLocalSize(const TString &qualifierType,
4382 const TSourceLoc &qualifierTypeLine,
4383 int intValue,
4384 const TSourceLoc &intValueLine,
4385 const std::string &intValueString,
4386 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004387 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004388{
Olli Etuaho856c4972016-08-08 11:38:39 +03004389 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004390 if (intValue < 1)
4391 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004392 std::stringstream reasonStream;
4393 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4394 std::string reason = reasonStream.str();
4395 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004396 }
4397 (*localSize)[index] = intValue;
4398}
4399
Olli Etuaho09b04a22016-12-15 13:30:26 +00004400void TParseContext::parseNumViews(int intValue,
4401 const TSourceLoc &intValueLine,
4402 const std::string &intValueString,
4403 int *numViews)
4404{
4405 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4406 // specification.
4407 if (intValue < 1)
4408 {
4409 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4410 }
4411 *numViews = intValue;
4412}
4413
Shaob5cc1192017-07-06 10:47:20 +08004414void TParseContext::parseInvocations(int intValue,
4415 const TSourceLoc &intValueLine,
4416 const std::string &intValueString,
4417 int *numInvocations)
4418{
4419 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4420 // it doesn't make sense to accept invocations <= 0.
4421 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4422 {
4423 error(intValueLine,
4424 "out of range: invocations must be in the range of [1, "
4425 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4426 intValueString.c_str());
4427 }
4428 else
4429 {
4430 *numInvocations = intValue;
4431 }
4432}
4433
4434void TParseContext::parseMaxVertices(int intValue,
4435 const TSourceLoc &intValueLine,
4436 const std::string &intValueString,
4437 int *maxVertices)
4438{
4439 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4440 // it doesn't make sense to accept max_vertices < 0.
4441 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4442 {
4443 error(
4444 intValueLine,
4445 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4446 intValueString.c_str());
4447 }
4448 else
4449 {
4450 *maxVertices = intValue;
4451 }
4452}
4453
Jamie Madillb98c3a82015-07-23 14:26:04 -04004454TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4455 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004456 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304457 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004458{
Jamie Madill2f294c92017-11-20 14:47:26 -05004459 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004460
Martin Radev802abe02016-08-04 17:48:32 +03004461 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004462
Martin Radev802abe02016-08-04 17:48:32 +03004463 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004465 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004466 if (intValue < 0)
4467 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004468 error(intValueLine, "out of range: location must be non-negative",
4469 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004470 }
4471 else
4472 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004473 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004474 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004476 }
Olli Etuaho43364892017-02-13 16:00:12 +00004477 else if (qualifierType == "binding")
4478 {
4479 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4480 if (intValue < 0)
4481 {
4482 error(intValueLine, "out of range: binding must be non-negative",
4483 intValueString.c_str());
4484 }
4485 else
4486 {
4487 qualifier.binding = intValue;
4488 }
4489 }
jchen104cdac9e2017-05-08 11:01:20 +08004490 else if (qualifierType == "offset")
4491 {
4492 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4493 if (intValue < 0)
4494 {
4495 error(intValueLine, "out of range: offset must be non-negative",
4496 intValueString.c_str());
4497 }
4498 else
4499 {
4500 qualifier.offset = intValue;
4501 }
4502 }
Martin Radev802abe02016-08-04 17:48:32 +03004503 else if (qualifierType == "local_size_x")
4504 {
4505 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4506 &qualifier.localSize);
4507 }
4508 else if (qualifierType == "local_size_y")
4509 {
4510 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4511 &qualifier.localSize);
4512 }
4513 else if (qualifierType == "local_size_z")
4514 {
4515 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4516 &qualifier.localSize);
4517 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004518 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004519 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004520 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4521 {
4522 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4523 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004524 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004525 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4526 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004527 {
4528 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4529 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004530 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4531 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004532 {
4533 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4534 }
4535
Martin Radev802abe02016-08-04 17:48:32 +03004536 else
4537 {
4538 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004539 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004540
Jamie Madilla5efff92013-06-06 11:56:47 -04004541 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004542}
4543
Olli Etuaho613b9592016-09-05 12:05:53 +03004544TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4545{
4546 return new TTypeQualifierBuilder(
4547 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4548 mShaderVersion);
4549}
4550
Olli Etuahocce89652017-06-19 16:04:09 +03004551TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4552 const TSourceLoc &loc)
4553{
4554 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4555 return new TStorageQualifierWrapper(qualifier, loc);
4556}
4557
4558TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4559{
4560 if (getShaderType() == GL_VERTEX_SHADER)
4561 {
4562 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4563 }
4564 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4565}
4566
4567TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4568{
4569 if (declaringFunction())
4570 {
4571 return new TStorageQualifierWrapper(EvqIn, loc);
4572 }
Shaob5cc1192017-07-06 10:47:20 +08004573
4574 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004575 {
Shaob5cc1192017-07-06 10:47:20 +08004576 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004577 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004578 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004579 {
4580 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4581 }
4582 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004583 }
Shaob5cc1192017-07-06 10:47:20 +08004584 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004585 {
Shaob5cc1192017-07-06 10:47:20 +08004586 if (mShaderVersion < 300)
4587 {
4588 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4589 }
4590 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004591 }
Shaob5cc1192017-07-06 10:47:20 +08004592 case GL_COMPUTE_SHADER:
4593 {
4594 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4595 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004596 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004597 {
4598 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4599 }
4600 default:
4601 {
4602 UNREACHABLE();
4603 return new TStorageQualifierWrapper(EvqLast, loc);
4604 }
Olli Etuahocce89652017-06-19 16:04:09 +03004605 }
Olli Etuahocce89652017-06-19 16:04:09 +03004606}
4607
4608TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4609{
4610 if (declaringFunction())
4611 {
4612 return new TStorageQualifierWrapper(EvqOut, loc);
4613 }
Shaob5cc1192017-07-06 10:47:20 +08004614 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004615 {
Shaob5cc1192017-07-06 10:47:20 +08004616 case GL_VERTEX_SHADER:
4617 {
4618 if (mShaderVersion < 300)
4619 {
4620 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4621 }
4622 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4623 }
4624 case GL_FRAGMENT_SHADER:
4625 {
4626 if (mShaderVersion < 300)
4627 {
4628 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4629 }
4630 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4631 }
4632 case GL_COMPUTE_SHADER:
4633 {
4634 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4635 return new TStorageQualifierWrapper(EvqLast, loc);
4636 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004637 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004638 {
4639 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4640 }
4641 default:
4642 {
4643 UNREACHABLE();
4644 return new TStorageQualifierWrapper(EvqLast, loc);
4645 }
Olli Etuahocce89652017-06-19 16:04:09 +03004646 }
Olli Etuahocce89652017-06-19 16:04:09 +03004647}
4648
4649TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4650{
4651 if (!declaringFunction())
4652 {
4653 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4654 }
4655 return new TStorageQualifierWrapper(EvqInOut, loc);
4656}
4657
Jamie Madillb98c3a82015-07-23 14:26:04 -04004658TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004659 TLayoutQualifier rightQualifier,
4660 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004661{
Martin Radevc28888b2016-07-22 15:27:42 +03004662 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004663 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004664}
4665
Olli Etuahod5f44c92017-11-29 17:15:40 +02004666TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004667{
4668 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004669 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004670}
4671
Olli Etuahod5f44c92017-11-29 17:15:40 +02004672TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4673 const TSourceLoc &loc,
4674 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004675{
4676 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004677 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004678}
4679
Olli Etuaho722bfb52017-10-26 17:00:11 +03004680void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4681 const TFieldList::const_iterator end,
4682 const TString &name,
4683 const TSourceLoc &location)
4684{
4685 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4686 {
4687 if ((*fieldIter)->name() == name)
4688 {
4689 error(location, "duplicate field name in structure", name.c_str());
4690 }
4691 }
4692}
4693
4694TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4695{
4696 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4697 ++fieldIter)
4698 {
4699 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4700 location);
4701 }
4702 return fields;
4703}
4704
Olli Etuaho4de340a2016-12-16 09:32:03 +00004705TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4706 const TFieldList *newlyAddedFields,
4707 const TSourceLoc &location)
4708{
4709 for (TField *field : *newlyAddedFields)
4710 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004711 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4712 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004713 processedFields->push_back(field);
4714 }
4715 return processedFields;
4716}
4717
Martin Radev70866b82016-07-22 15:27:42 +03004718TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4719 const TTypeQualifierBuilder &typeQualifierBuilder,
4720 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004721 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004722{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004723 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004724
Martin Radev70866b82016-07-22 15:27:42 +03004725 typeSpecifier->qualifier = typeQualifier.qualifier;
4726 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004727 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004728 typeSpecifier->invariant = typeQualifier.invariant;
4729 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304730 {
Martin Radev70866b82016-07-22 15:27:42 +03004731 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004732 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004733 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004734}
4735
Jamie Madillb98c3a82015-07-23 14:26:04 -04004736TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004737 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004738{
Martin Radev4a9cd802016-09-01 16:51:51 +03004739 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4740 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004741
Olli Etuahod5f44c92017-11-29 17:15:40 +02004742 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004743 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004744
Martin Radev4a9cd802016-09-01 16:51:51 +03004745 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004746
Olli Etuahod5f44c92017-11-29 17:15:40 +02004747 TFieldList *fieldList = new TFieldList();
4748
4749 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304750 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004751 TType *type = new TType(typeSpecifier);
4752 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304753 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004754 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004755 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004756 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004757 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004758
Olli Etuahod5f44c92017-11-29 17:15:40 +02004759 TField *field = new TField(type, declarator->name(), declarator->line());
4760 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4761 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004762 }
4763
Olli Etuahod5f44c92017-11-29 17:15:40 +02004764 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004765}
4766
Martin Radev4a9cd802016-09-01 16:51:51 +03004767TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4768 const TSourceLoc &nameLine,
4769 const TString *structName,
4770 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004771{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004772 SymbolType structSymbolType = SymbolType::UserDefined;
4773 if (structName == nullptr)
4774 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004775 structSymbolType = SymbolType::Empty;
4776 }
4777 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004778
Jamie Madill9b820842015-02-12 10:40:10 -05004779 // Store a bool in the struct if we're at global scope, to allow us to
4780 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004781 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004782
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004783 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004784 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004785 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004786 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304787 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004788 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004789 }
4790 }
4791
4792 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004793 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004794 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004795 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004796 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004797 switch (qualifier)
4798 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004799 case EvqGlobal:
4800 case EvqTemporary:
4801 break;
4802 default:
4803 error(field.line(), "invalid qualifier on struct member",
4804 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004805 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004806 }
Martin Radev70866b82016-07-22 15:27:42 +03004807 if (field.type()->isInvariant())
4808 {
4809 error(field.line(), "invalid qualifier on struct member", "invariant");
4810 }
jchen104cdac9e2017-05-08 11:01:20 +08004811 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4812 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004813 {
4814 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4815 }
4816
Olli Etuahoebee5b32017-11-23 12:56:32 +02004817 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4818 field.name().c_str(), field.type());
4819
Olli Etuaho43364892017-02-13 16:00:12 +00004820 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4821
4822 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004823
4824 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004825 }
4826
Martin Radev4a9cd802016-09-01 16:51:51 +03004827 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004828 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004829 exitStructDeclaration();
4830
Martin Radev4a9cd802016-09-01 16:51:51 +03004831 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004832}
4833
Jamie Madillb98c3a82015-07-23 14:26:04 -04004834TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004835 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004836 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004837{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004838 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004839 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004840 init->isVector())
4841 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004842 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4843 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004844 return nullptr;
4845 }
4846
Olli Etuaho923ecef2017-10-11 12:01:38 +03004847 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004848 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004849 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004850 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004851 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004852 }
4853
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004854 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4855 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004856 return node;
4857}
4858
4859TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4860{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004861 if (mSwitchNestingLevel == 0)
4862 {
4863 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004864 return nullptr;
4865 }
4866 if (condition == nullptr)
4867 {
4868 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004869 return nullptr;
4870 }
4871 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004872 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004873 {
4874 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004875 }
4876 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004877 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4878 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4879 // fold in case labels.
4880 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004881 {
4882 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004883 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004884 TIntermCase *node = new TIntermCase(condition);
4885 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004886 return node;
4887}
4888
4889TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4890{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004891 if (mSwitchNestingLevel == 0)
4892 {
4893 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004894 return nullptr;
4895 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004896 TIntermCase *node = new TIntermCase(nullptr);
4897 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004898 return node;
4899}
4900
Jamie Madillb98c3a82015-07-23 14:26:04 -04004901TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4902 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004903 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004904{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004905 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004906
4907 switch (op)
4908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004909 case EOpLogicalNot:
4910 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4911 child->isVector())
4912 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004913 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004914 return nullptr;
4915 }
4916 break;
4917 case EOpBitwiseNot:
4918 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4919 child->isMatrix() || child->isArray())
4920 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004921 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004922 return nullptr;
4923 }
4924 break;
4925 case EOpPostIncrement:
4926 case EOpPreIncrement:
4927 case EOpPostDecrement:
4928 case EOpPreDecrement:
4929 case EOpNegative:
4930 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004931 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4932 child->getBasicType() == EbtBool || child->isArray() ||
4933 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004934 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004935 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004936 return nullptr;
4937 }
4938 // Operators for built-ins are already type checked against their prototype.
4939 default:
4940 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004941 }
4942
Jiajia Qinbc585152017-06-23 15:42:17 +08004943 if (child->getMemoryQualifier().writeonly)
4944 {
4945 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4946 return nullptr;
4947 }
4948
Olli Etuahof119a262016-08-19 15:54:22 +03004949 TIntermUnary *node = new TIntermUnary(op, child);
4950 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004951
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004952 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004953}
4954
Olli Etuaho09b22472015-02-11 11:47:26 +02004955TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4956{
Olli Etuahocce89652017-06-19 16:04:09 +03004957 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004958 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004959 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004960 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004961 return child;
4962 }
4963 return node;
4964}
4965
Jamie Madillb98c3a82015-07-23 14:26:04 -04004966TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4967 TIntermTyped *child,
4968 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004969{
Olli Etuaho856c4972016-08-08 11:38:39 +03004970 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004971 return addUnaryMath(op, child, loc);
4972}
4973
Jamie Madillb98c3a82015-07-23 14:26:04 -04004974bool TParseContext::binaryOpCommonCheck(TOperator op,
4975 TIntermTyped *left,
4976 TIntermTyped *right,
4977 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004978{
jchen10b4cf5652017-05-05 18:51:17 +08004979 // Check opaque types are not allowed to be operands in expressions other than array indexing
4980 // and structure member selection.
4981 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4982 {
4983 switch (op)
4984 {
4985 case EOpIndexDirect:
4986 case EOpIndexIndirect:
4987 break;
4988 case EOpIndexDirectStruct:
4989 UNREACHABLE();
4990
4991 default:
4992 error(loc, "Invalid operation for variables with an opaque type",
4993 GetOperatorString(op));
4994 return false;
4995 }
4996 }
jchen10cc2a10e2017-05-03 14:05:12 +08004997
Jiajia Qinbc585152017-06-23 15:42:17 +08004998 if (right->getMemoryQualifier().writeonly)
4999 {
5000 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5001 return false;
5002 }
5003
5004 if (left->getMemoryQualifier().writeonly)
5005 {
5006 switch (op)
5007 {
5008 case EOpAssign:
5009 case EOpInitialize:
5010 case EOpIndexDirect:
5011 case EOpIndexIndirect:
5012 case EOpIndexDirectStruct:
5013 case EOpIndexDirectInterfaceBlock:
5014 break;
5015 default:
5016 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5017 return false;
5018 }
5019 }
5020
Olli Etuaho244be012016-08-18 15:26:02 +03005021 if (left->getType().getStruct() || right->getType().getStruct())
5022 {
5023 switch (op)
5024 {
5025 case EOpIndexDirectStruct:
5026 ASSERT(left->getType().getStruct());
5027 break;
5028 case EOpEqual:
5029 case EOpNotEqual:
5030 case EOpAssign:
5031 case EOpInitialize:
5032 if (left->getType() != right->getType())
5033 {
5034 return false;
5035 }
5036 break;
5037 default:
5038 error(loc, "Invalid operation for structs", GetOperatorString(op));
5039 return false;
5040 }
5041 }
5042
Olli Etuaho94050052017-05-08 14:17:44 +03005043 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5044 {
5045 switch (op)
5046 {
5047 case EOpIndexDirectInterfaceBlock:
5048 ASSERT(left->getType().getInterfaceBlock());
5049 break;
5050 default:
5051 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5052 return false;
5053 }
5054 }
5055
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005056 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005057 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005058 error(loc, "array / non-array mismatch", GetOperatorString(op));
5059 return false;
5060 }
5061
5062 if (left->isArray())
5063 {
5064 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005065 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005066 {
5067 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5068 return false;
5069 }
5070
Olli Etuahoe79904c2015-03-18 16:56:42 +02005071 switch (op)
5072 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005073 case EOpEqual:
5074 case EOpNotEqual:
5075 case EOpAssign:
5076 case EOpInitialize:
5077 break;
5078 default:
5079 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5080 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005081 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005082 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005083 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005084 {
5085 error(loc, "array size mismatch", GetOperatorString(op));
5086 return false;
5087 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005088 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005089
5090 // Check ops which require integer / ivec parameters
5091 bool isBitShift = false;
5092 switch (op)
5093 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005094 case EOpBitShiftLeft:
5095 case EOpBitShiftRight:
5096 case EOpBitShiftLeftAssign:
5097 case EOpBitShiftRightAssign:
5098 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5099 // check that the basic type is an integer type.
5100 isBitShift = true;
5101 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5102 {
5103 return false;
5104 }
5105 break;
5106 case EOpBitwiseAnd:
5107 case EOpBitwiseXor:
5108 case EOpBitwiseOr:
5109 case EOpBitwiseAndAssign:
5110 case EOpBitwiseXorAssign:
5111 case EOpBitwiseOrAssign:
5112 // It is enough to check the type of only one operand, since later it
5113 // is checked that the operand types match.
5114 if (!IsInteger(left->getBasicType()))
5115 {
5116 return false;
5117 }
5118 break;
5119 default:
5120 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005121 }
5122
5123 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5124 // So the basic type should usually match.
5125 if (!isBitShift && left->getBasicType() != right->getBasicType())
5126 {
5127 return false;
5128 }
5129
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005130 // Check that:
5131 // 1. Type sizes match exactly on ops that require that.
5132 // 2. Restrictions for structs that contain arrays or samplers are respected.
5133 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005134 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005136 case EOpAssign:
5137 case EOpInitialize:
5138 case EOpEqual:
5139 case EOpNotEqual:
5140 // ESSL 1.00 sections 5.7, 5.8, 5.9
5141 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5142 {
5143 error(loc, "undefined operation for structs containing arrays",
5144 GetOperatorString(op));
5145 return false;
5146 }
5147 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5148 // we interpret the spec so that this extends to structs containing samplers,
5149 // similarly to ESSL 1.00 spec.
5150 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5151 left->getType().isStructureContainingSamplers())
5152 {
5153 error(loc, "undefined operation for structs containing samplers",
5154 GetOperatorString(op));
5155 return false;
5156 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005157
Olli Etuahoe1805592017-01-02 16:41:20 +00005158 if ((left->getNominalSize() != right->getNominalSize()) ||
5159 (left->getSecondarySize() != right->getSecondarySize()))
5160 {
5161 error(loc, "dimension mismatch", GetOperatorString(op));
5162 return false;
5163 }
5164 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005165 case EOpLessThan:
5166 case EOpGreaterThan:
5167 case EOpLessThanEqual:
5168 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005169 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005170 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005171 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005172 return false;
5173 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005174 break;
5175 case EOpAdd:
5176 case EOpSub:
5177 case EOpDiv:
5178 case EOpIMod:
5179 case EOpBitShiftLeft:
5180 case EOpBitShiftRight:
5181 case EOpBitwiseAnd:
5182 case EOpBitwiseXor:
5183 case EOpBitwiseOr:
5184 case EOpAddAssign:
5185 case EOpSubAssign:
5186 case EOpDivAssign:
5187 case EOpIModAssign:
5188 case EOpBitShiftLeftAssign:
5189 case EOpBitShiftRightAssign:
5190 case EOpBitwiseAndAssign:
5191 case EOpBitwiseXorAssign:
5192 case EOpBitwiseOrAssign:
5193 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5194 {
5195 return false;
5196 }
5197
5198 // Are the sizes compatible?
5199 if (left->getNominalSize() != right->getNominalSize() ||
5200 left->getSecondarySize() != right->getSecondarySize())
5201 {
5202 // If the nominal sizes of operands do not match:
5203 // One of them must be a scalar.
5204 if (!left->isScalar() && !right->isScalar())
5205 return false;
5206
5207 // In the case of compound assignment other than multiply-assign,
5208 // the right side needs to be a scalar. Otherwise a vector/matrix
5209 // would be assigned to a scalar. A scalar can't be shifted by a
5210 // vector either.
5211 if (!right->isScalar() &&
5212 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5213 return false;
5214 }
5215 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005216 default:
5217 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005218 }
5219
Olli Etuahod6b14282015-03-17 14:31:35 +02005220 return true;
5221}
5222
Olli Etuaho1dded802016-08-18 18:13:13 +03005223bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5224 const TType &left,
5225 const TType &right)
5226{
5227 switch (op)
5228 {
5229 case EOpMul:
5230 case EOpMulAssign:
5231 return left.getNominalSize() == right.getNominalSize() &&
5232 left.getSecondarySize() == right.getSecondarySize();
5233 case EOpVectorTimesScalar:
5234 return true;
5235 case EOpVectorTimesScalarAssign:
5236 ASSERT(!left.isMatrix() && !right.isMatrix());
5237 return left.isVector() && !right.isVector();
5238 case EOpVectorTimesMatrix:
5239 return left.getNominalSize() == right.getRows();
5240 case EOpVectorTimesMatrixAssign:
5241 ASSERT(!left.isMatrix() && right.isMatrix());
5242 return left.isVector() && left.getNominalSize() == right.getRows() &&
5243 left.getNominalSize() == right.getCols();
5244 case EOpMatrixTimesVector:
5245 return left.getCols() == right.getNominalSize();
5246 case EOpMatrixTimesScalar:
5247 return true;
5248 case EOpMatrixTimesScalarAssign:
5249 ASSERT(left.isMatrix() && !right.isMatrix());
5250 return !right.isVector();
5251 case EOpMatrixTimesMatrix:
5252 return left.getCols() == right.getRows();
5253 case EOpMatrixTimesMatrixAssign:
5254 ASSERT(left.isMatrix() && right.isMatrix());
5255 // We need to check two things:
5256 // 1. The matrix multiplication step is valid.
5257 // 2. The result will have the same number of columns as the lvalue.
5258 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5259
5260 default:
5261 UNREACHABLE();
5262 return false;
5263 }
5264}
5265
Jamie Madillb98c3a82015-07-23 14:26:04 -04005266TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5267 TIntermTyped *left,
5268 TIntermTyped *right,
5269 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005270{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005271 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005272 return nullptr;
5273
Olli Etuahofc1806e2015-03-17 13:03:11 +02005274 switch (op)
5275 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005276 case EOpEqual:
5277 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005278 case EOpLessThan:
5279 case EOpGreaterThan:
5280 case EOpLessThanEqual:
5281 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005282 break;
5283 case EOpLogicalOr:
5284 case EOpLogicalXor:
5285 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005286 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5287 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005288 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005289 {
5290 return nullptr;
5291 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005292 // Basic types matching should have been already checked.
5293 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005294 break;
5295 case EOpAdd:
5296 case EOpSub:
5297 case EOpDiv:
5298 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005299 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5300 !right->getType().getStruct());
5301 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005302 {
5303 return nullptr;
5304 }
5305 break;
5306 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005307 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5308 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005309 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005310 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005311 {
5312 return nullptr;
5313 }
5314 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005315 default:
5316 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005317 }
5318
Olli Etuaho1dded802016-08-18 18:13:13 +03005319 if (op == EOpMul)
5320 {
5321 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5322 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5323 {
5324 return nullptr;
5325 }
5326 }
5327
Olli Etuaho3fdec912016-08-18 15:08:06 +03005328 TIntermBinary *node = new TIntermBinary(op, left, right);
5329 node->setLine(loc);
5330
Olli Etuaho3fdec912016-08-18 15:08:06 +03005331 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005332 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005333}
5334
Jamie Madillb98c3a82015-07-23 14:26:04 -04005335TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5336 TIntermTyped *left,
5337 TIntermTyped *right,
5338 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005339{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005340 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005341 if (node == 0)
5342 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005343 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5344 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005345 return left;
5346 }
5347 return node;
5348}
5349
Jamie Madillb98c3a82015-07-23 14:26:04 -04005350TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5351 TIntermTyped *left,
5352 TIntermTyped *right,
5353 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005354{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005355 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005356 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005357 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005358 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5359 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005360 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005361 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005362 }
5363 return node;
5364}
5365
Olli Etuaho13389b62016-10-16 11:48:18 +01005366TIntermBinary *TParseContext::createAssign(TOperator op,
5367 TIntermTyped *left,
5368 TIntermTyped *right,
5369 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005370{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005371 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005372 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005373 if (op == EOpMulAssign)
5374 {
5375 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5376 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5377 {
5378 return nullptr;
5379 }
5380 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005381 TIntermBinary *node = new TIntermBinary(op, left, right);
5382 node->setLine(loc);
5383
Olli Etuaho3fdec912016-08-18 15:08:06 +03005384 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005385 }
5386 return nullptr;
5387}
5388
Jamie Madillb98c3a82015-07-23 14:26:04 -04005389TIntermTyped *TParseContext::addAssign(TOperator op,
5390 TIntermTyped *left,
5391 TIntermTyped *right,
5392 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005393{
Olli Etuahocce89652017-06-19 16:04:09 +03005394 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005395 TIntermTyped *node = createAssign(op, left, right, loc);
5396 if (node == nullptr)
5397 {
5398 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005399 return left;
5400 }
5401 return node;
5402}
5403
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005404TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5405 TIntermTyped *right,
5406 const TSourceLoc &loc)
5407{
Corentin Wallez0d959252016-07-12 17:26:32 -04005408 // WebGL2 section 5.26, the following results in an error:
5409 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005410 if (mShaderSpec == SH_WEBGL2_SPEC &&
5411 (left->isArray() || left->getBasicType() == EbtVoid ||
5412 left->getType().isStructureContainingArrays() || right->isArray() ||
5413 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005414 {
5415 error(loc,
5416 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5417 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005418 }
5419
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005420 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5421 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5422 commaNode->getTypePointer()->setQualifier(resultQualifier);
5423 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005424}
5425
Olli Etuaho49300862015-02-20 14:54:49 +02005426TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5427{
5428 switch (op)
5429 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005430 case EOpContinue:
5431 if (mLoopNestingLevel <= 0)
5432 {
5433 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005434 }
5435 break;
5436 case EOpBreak:
5437 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5438 {
5439 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005440 }
5441 break;
5442 case EOpReturn:
5443 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5444 {
5445 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005446 }
5447 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005448 case EOpKill:
5449 if (mShaderType != GL_FRAGMENT_SHADER)
5450 {
5451 error(loc, "discard supported in fragment shaders only", "discard");
5452 }
5453 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005455 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005456 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005457 }
Olli Etuahocce89652017-06-19 16:04:09 +03005458 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005459}
5460
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005462 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005463 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005464{
Olli Etuahocce89652017-06-19 16:04:09 +03005465 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005466 {
Olli Etuahocce89652017-06-19 16:04:09 +03005467 ASSERT(op == EOpReturn);
5468 mFunctionReturnsValue = true;
5469 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5470 {
5471 error(loc, "void function cannot return a value", "return");
5472 }
5473 else if (*mCurrentFunctionType != expression->getType())
5474 {
5475 error(loc, "function return is not matching type:", "return");
5476 }
Olli Etuaho49300862015-02-20 14:54:49 +02005477 }
Olli Etuahocce89652017-06-19 16:04:09 +03005478 TIntermBranch *node = new TIntermBranch(op, expression);
5479 node->setLine(loc);
5480 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005481}
5482
Martin Radev84aa2dc2017-09-11 15:51:02 +03005483void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5484{
5485 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
5486 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5487 bool isTextureGather = (name == "textureGather");
5488 bool isTextureGatherOffset = (name == "textureGatherOffset");
5489 if (isTextureGather || isTextureGatherOffset)
5490 {
5491 TIntermNode *componentNode = nullptr;
5492 TIntermSequence *arguments = functionCall->getSequence();
5493 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5494 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5495 ASSERT(sampler != nullptr);
5496 switch (sampler->getBasicType())
5497 {
5498 case EbtSampler2D:
5499 case EbtISampler2D:
5500 case EbtUSampler2D:
5501 case EbtSampler2DArray:
5502 case EbtISampler2DArray:
5503 case EbtUSampler2DArray:
5504 if ((isTextureGather && arguments->size() == 3u) ||
5505 (isTextureGatherOffset && arguments->size() == 4u))
5506 {
5507 componentNode = arguments->back();
5508 }
5509 break;
5510 case EbtSamplerCube:
5511 case EbtISamplerCube:
5512 case EbtUSamplerCube:
5513 ASSERT(!isTextureGatherOffset);
5514 if (arguments->size() == 3u)
5515 {
5516 componentNode = arguments->back();
5517 }
5518 break;
5519 case EbtSampler2DShadow:
5520 case EbtSampler2DArrayShadow:
5521 case EbtSamplerCubeShadow:
5522 break;
5523 default:
5524 UNREACHABLE();
5525 break;
5526 }
5527 if (componentNode)
5528 {
5529 const TIntermConstantUnion *componentConstantUnion =
5530 componentNode->getAsConstantUnion();
5531 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5532 {
5533 error(functionCall->getLine(), "Texture component must be a constant expression",
5534 name.c_str());
5535 }
5536 else
5537 {
5538 int component = componentConstantUnion->getIConst(0);
5539 if (component < 0 || component > 3)
5540 {
5541 error(functionCall->getLine(), "Component must be in the range [0;3]",
5542 name.c_str());
5543 }
5544 }
5545 }
5546 }
5547}
5548
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005549void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5550{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005551 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005552 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005553 TIntermNode *offset = nullptr;
5554 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005555 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005556 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5557 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5558 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005559 {
5560 offset = arguments->back();
5561 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005562 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005563 {
5564 // A bias parameter might follow the offset parameter.
5565 ASSERT(arguments->size() >= 3);
5566 offset = (*arguments)[2];
5567 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005568 else if (name == "textureGatherOffset")
5569 {
5570 ASSERT(arguments->size() >= 3u);
5571 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5572 ASSERT(sampler != nullptr);
5573 switch (sampler->getBasicType())
5574 {
5575 case EbtSampler2D:
5576 case EbtISampler2D:
5577 case EbtUSampler2D:
5578 case EbtSampler2DArray:
5579 case EbtISampler2DArray:
5580 case EbtUSampler2DArray:
5581 offset = (*arguments)[2];
5582 break;
5583 case EbtSampler2DShadow:
5584 case EbtSampler2DArrayShadow:
5585 offset = (*arguments)[3];
5586 break;
5587 default:
5588 UNREACHABLE();
5589 break;
5590 }
5591 useTextureGatherOffsetConstraints = true;
5592 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005593 if (offset != nullptr)
5594 {
5595 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5596 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5597 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005598 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005599 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005600 }
5601 else
5602 {
5603 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5604 size_t size = offsetConstantUnion->getType().getObjectSize();
5605 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005606 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5607 : mMinProgramTexelOffset;
5608 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5609 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005610 for (size_t i = 0u; i < size; ++i)
5611 {
5612 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005613 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005614 {
5615 std::stringstream tokenStream;
5616 tokenStream << offsetValue;
5617 std::string token = tokenStream.str();
5618 error(offset->getLine(), "Texture offset value out of valid range",
5619 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005620 }
5621 }
5622 }
5623 }
5624}
5625
Jiajia Qina3106c52017-11-03 09:39:39 +08005626void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5627{
5628 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5629 if (IsAtomicBuiltin(name))
5630 {
5631 TIntermSequence *arguments = functionCall->getSequence();
5632 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5633
5634 if (IsBufferOrSharedVariable(memNode))
5635 {
5636 return;
5637 }
5638
5639 while (memNode->getAsBinaryNode())
5640 {
5641 memNode = memNode->getAsBinaryNode()->getLeft();
5642 if (IsBufferOrSharedVariable(memNode))
5643 {
5644 return;
5645 }
5646 }
5647
5648 error(memNode->getLine(),
5649 "The value passed to the mem argument of an atomic memory function does not "
5650 "correspond to a buffer or shared variable.",
5651 functionCall->getFunctionSymbolInfo()->getName().c_str());
5652 }
5653}
5654
Martin Radev2cc85b32016-08-05 16:22:53 +03005655// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5656void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5657{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005658 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005659 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5660
5661 if (name.compare(0, 5, "image") == 0)
5662 {
5663 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005664 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005665
Olli Etuaho485eefd2017-02-14 17:40:06 +00005666 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005667
5668 if (name.compare(5, 5, "Store") == 0)
5669 {
5670 if (memoryQualifier.readonly)
5671 {
5672 error(imageNode->getLine(),
5673 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005674 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005675 }
5676 }
5677 else if (name.compare(5, 4, "Load") == 0)
5678 {
5679 if (memoryQualifier.writeonly)
5680 {
5681 error(imageNode->getLine(),
5682 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005683 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005684 }
5685 }
5686 }
5687}
5688
5689// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5690void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5691 const TFunction *functionDefinition,
5692 const TIntermAggregate *functionCall)
5693{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005694 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005695
5696 const TIntermSequence &arguments = *functionCall->getSequence();
5697
5698 ASSERT(functionDefinition->getParamCount() == arguments.size());
5699
5700 for (size_t i = 0; i < arguments.size(); ++i)
5701 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005702 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5703 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005704 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5705 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5706
5707 if (IsImage(functionArgumentType.getBasicType()))
5708 {
5709 const TMemoryQualifier &functionArgumentMemoryQualifier =
5710 functionArgumentType.getMemoryQualifier();
5711 const TMemoryQualifier &functionParameterMemoryQualifier =
5712 functionParameterType.getMemoryQualifier();
5713 if (functionArgumentMemoryQualifier.readonly &&
5714 !functionParameterMemoryQualifier.readonly)
5715 {
5716 error(functionCall->getLine(),
5717 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005718 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005719 }
5720
5721 if (functionArgumentMemoryQualifier.writeonly &&
5722 !functionParameterMemoryQualifier.writeonly)
5723 {
5724 error(functionCall->getLine(),
5725 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005726 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005727 }
Martin Radev049edfa2016-11-11 14:35:37 +02005728
5729 if (functionArgumentMemoryQualifier.coherent &&
5730 !functionParameterMemoryQualifier.coherent)
5731 {
5732 error(functionCall->getLine(),
5733 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005734 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005735 }
5736
5737 if (functionArgumentMemoryQualifier.volatileQualifier &&
5738 !functionParameterMemoryQualifier.volatileQualifier)
5739 {
5740 error(functionCall->getLine(),
5741 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005742 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005743 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005744 }
5745 }
5746}
5747
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005748TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005749{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005750 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005751}
5752
5753TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005754 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005755 TIntermNode *thisNode,
5756 const TSourceLoc &loc)
5757{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005758 if (thisNode != nullptr)
5759 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005760 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005761 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005762
5763 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005764 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005765 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005766 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005767 }
5768 else
5769 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005770 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005771 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005772 }
5773}
5774
Olli Etuaho0c371002017-12-13 17:00:25 +04005775TIntermTyped *TParseContext::addMethod(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005776 TIntermSequence *arguments,
5777 TIntermNode *thisNode,
5778 const TSourceLoc &loc)
5779{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005780 TIntermTyped *typedThis = thisNode->getAsTyped();
5781 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5782 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5783 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005784 // So accessing fnCall->name() below is safe.
Olli Etuaho0c371002017-12-13 17:00:25 +04005785 if (*name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005786 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005787 error(loc, "invalid method", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005788 }
5789 else if (!arguments->empty())
5790 {
5791 error(loc, "method takes no parameters", "length");
5792 }
5793 else if (typedThis == nullptr || !typedThis->isArray())
5794 {
5795 error(loc, "length can only be called on arrays", "length");
5796 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005797 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5798 mGeometryShaderInputPrimitiveType == EptUndefined)
5799 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005800 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005801 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5802 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005803 else
5804 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005805 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5806 node->setLine(loc);
5807 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005808 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005809 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005810}
5811
Olli Etuaho0c371002017-12-13 17:00:25 +04005812TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString *name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005813 TIntermSequence *arguments,
5814 const TSourceLoc &loc)
5815{
Olli Etuaho0c371002017-12-13 17:00:25 +04005816 ASSERT(name);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005817 // First find by unmangled name to check whether the function name has been
5818 // hidden by a variable name or struct typename.
5819 // If a function is found, check for one with a matching argument list.
5820 bool builtIn;
Olli Etuaho0c371002017-12-13 17:00:25 +04005821 const TSymbol *symbol = symbolTable.find(*name, mShaderVersion, &builtIn);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 if (symbol != nullptr && !symbol->isFunction())
5823 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005824 error(loc, "function name expected", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005825 }
5826 else
5827 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005828 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(*name, *arguments),
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005829 mShaderVersion, &builtIn);
5830 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005831 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005832 error(loc, "no matching overloaded function found", name->c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005833 }
5834 else
5835 {
5836 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005837 //
5838 // A declared function.
5839 //
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005840 if (builtIn && fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005841 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005842 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005844 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 if (builtIn && op != EOpNull)
5846 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005848 if (fnCandidate->getParamCount() == 1)
5849 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005850 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 TIntermNode *unaryParamNode = arguments->front();
5852 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005853 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005854 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005855 }
5856 else
5857 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005858 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005859 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005860 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005861
5862 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005863 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305864
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005865 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305866 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005867 // See if we can constant fold a built-in. Note that this may be possible
5868 // even if it is not const-qualified.
5869 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305870 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005871 else
5872 {
5873 return callNode;
5874 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005875 }
5876 }
5877 else
5878 {
5879 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005880 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005881
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005882 // If builtIn == false, the function is user defined - could be an overloaded
5883 // built-in as well.
5884 // if builtIn == true, it's a builtIn function with no op associated with it.
5885 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005886 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005887 {
Olli Etuahofe486322017-03-21 09:30:54 +00005888 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005889 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005890 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005892 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005893 }
5894 else
5895 {
Olli Etuahofe486322017-03-21 09:30:54 +00005896 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005898 }
5899
Jiajia Qinbc585152017-06-23 15:42:17 +08005900 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005901
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005902 callNode->setLine(loc);
5903
5904 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005905 }
5906 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005907 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005908
5909 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005910 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005911}
5912
Jamie Madillb98c3a82015-07-23 14:26:04 -04005913TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005914 TIntermTyped *trueExpression,
5915 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005916 const TSourceLoc &loc)
5917{
Olli Etuaho56229f12017-07-10 14:16:33 +03005918 if (!checkIsScalarBool(loc, cond))
5919 {
5920 return falseExpression;
5921 }
Olli Etuaho52901742015-04-15 13:42:45 +03005922
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005923 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005924 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005925 std::stringstream reasonStream;
5926 reasonStream << "mismatching ternary operator operand types '"
5927 << trueExpression->getCompleteString() << " and '"
5928 << falseExpression->getCompleteString() << "'";
5929 std::string reason = reasonStream.str();
5930 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005931 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005932 }
Olli Etuahode318b22016-10-25 16:18:25 +01005933 if (IsOpaqueType(trueExpression->getBasicType()))
5934 {
5935 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005936 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005937 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5938 // Note that structs containing opaque types don't need to be checked as structs are
5939 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005940 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005941 return falseExpression;
5942 }
5943
Jiajia Qinbc585152017-06-23 15:42:17 +08005944 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5945 falseExpression->getMemoryQualifier().writeonly)
5946 {
5947 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5948 return falseExpression;
5949 }
5950
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005951 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005952 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 // ESSL 3.00.6 section 5.7:
5954 // Ternary operator support is optional for arrays. No certainty that it works across all
5955 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5956 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005957 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005958 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005959 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005960 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005961 }
Olli Etuaho94050052017-05-08 14:17:44 +03005962 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5963 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005964 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005965 return falseExpression;
5966 }
5967
Corentin Wallez0d959252016-07-12 17:26:32 -04005968 // WebGL2 section 5.26, the following results in an error:
5969 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005970 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005971 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005972 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005973 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005974 }
5975
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005976 // Note that the node resulting from here can be a constant union without being qualified as
5977 // constant.
5978 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5979 node->setLine(loc);
5980
5981 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005982}
Olli Etuaho49300862015-02-20 14:54:49 +02005983
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005984//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005985// Parse an array of strings using yyparse.
5986//
5987// Returns 0 for success.
5988//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005989int PaParseStrings(size_t count,
5990 const char *const string[],
5991 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305992 TParseContext *context)
5993{
Yunchao He4f285442017-04-21 12:15:49 +08005994 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005995 return 1;
5996
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005997 if (glslang_initialize(context))
5998 return 1;
5999
alokp@chromium.org408c45e2012-04-05 15:54:43 +00006000 int error = glslang_scan(count, string, length, context);
6001 if (!error)
6002 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006003
alokp@chromium.org73bc2982012-06-19 18:48:05 +00006004 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00006005
alokp@chromium.org6b495712012-06-29 00:06:58 +00006006 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00006007}
Jamie Madill45bcc782016-11-07 13:58:48 -05006008
6009} // namespace sh