blob: 4842f890e6ca2e85682a321b663b5bf38e373b41 [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"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
35bool ContainsSampler(const TType &type)
36{
37 if (IsSampler(type.getBasicType()))
38 return true;
39
jchen10cc2a10e2017-05-03 14:05:12 +080040 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030041 {
42 const TFieldList &fields = type.getStruct()->fields();
43 for (unsigned int i = 0; i < fields.size(); ++i)
44 {
45 if (ContainsSampler(*fields[i]->type()))
46 return true;
47 }
48 }
49
50 return false;
51}
52
Olli Etuaho485eefd2017-02-14 17:40:06 +000053// Get a token from an image argument to use as an error message token.
54const char *GetImageArgumentToken(TIntermTyped *imageNode)
55{
56 ASSERT(IsImage(imageNode->getBasicType()));
57 while (imageNode->getAsBinaryNode() &&
58 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
59 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
60 {
61 imageNode = imageNode->getAsBinaryNode()->getLeft();
62 }
63 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
64 if (imageSymbol)
65 {
66 return imageSymbol->getSymbol().c_str();
67 }
68 return "image";
69}
70
Olli Etuahocce89652017-06-19 16:04:09 +030071bool CanSetDefaultPrecisionOnType(const TPublicType &type)
72{
73 if (!SupportsPrecision(type.getBasicType()))
74 {
75 return false;
76 }
77 if (type.getBasicType() == EbtUInt)
78 {
79 // ESSL 3.00.4 section 4.5.4
80 return false;
81 }
82 if (type.isAggregate())
83 {
84 // Not allowed to set for aggregate types
85 return false;
86 }
87 return true;
88}
89
Martin Radev2cc85b32016-08-05 16:22:53 +030090} // namespace
91
jchen104cdac9e2017-05-08 11:01:20 +080092// This tracks each binding point's current default offset for inheritance of subsequent
93// variables using the same binding, and keeps offsets unique and non overlapping.
94// See GLSL ES 3.1, section 4.4.6.
95class TParseContext::AtomicCounterBindingState
96{
97 public:
98 AtomicCounterBindingState() : mDefaultOffset(0) {}
99 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
100 // newly inserted span.
101 int insertSpan(int start, size_t length)
102 {
103 gl::RangeI newSpan(start, start + static_cast<int>(length));
104 for (const auto &span : mSpans)
105 {
106 if (newSpan.intersects(span))
107 {
108 return -1;
109 }
110 }
111 mSpans.push_back(newSpan);
112 mDefaultOffset = newSpan.high();
113 return start;
114 }
115 // Inserts a new span starting from the default offset.
116 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
117 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
118
119 private:
120 int mDefaultOffset;
121 std::vector<gl::RangeI> mSpans;
122};
123
Jamie Madillacb4b812016-11-07 13:50:29 -0500124TParseContext::TParseContext(TSymbolTable &symt,
125 TExtensionBehavior &ext,
126 sh::GLenum type,
127 ShShaderSpec spec,
128 ShCompileOptions options,
129 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000130 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500131 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300132 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300133 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500134 mShaderType(type),
135 mShaderSpec(spec),
136 mCompileOptions(options),
137 mShaderVersion(100),
138 mTreeRoot(nullptr),
139 mLoopNestingLevel(0),
140 mStructNestingLevel(0),
141 mSwitchNestingLevel(0),
142 mCurrentFunctionType(nullptr),
143 mFunctionReturnsValue(false),
144 mChecksPrecisionErrors(checksPrecErrors),
145 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800146 mDefaultUniformMatrixPacking(EmpColumnMajor),
147 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
148 mDefaultBufferMatrixPacking(EmpColumnMajor),
149 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000150 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500151 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000152 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500153 mShaderVersion,
154 mShaderType,
155 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000156 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500157 mScanner(nullptr),
158 mUsesFragData(false),
159 mUsesFragColor(false),
160 mUsesSecondaryOutputs(false),
161 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
162 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000163 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500164 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000165 mNumViews(-1),
166 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000167 mMaxImageUnits(resources.MaxImageUnits),
168 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000169 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800170 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800171 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800172 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Jamie Madillacb4b812016-11-07 13:50:29 -0500173 mDeclaringFunction(false)
174{
175 mComputeShaderLocalSize.fill(-1);
176}
177
jchen104cdac9e2017-05-08 11:01:20 +0800178TParseContext::~TParseContext()
179{
180}
181
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300182bool TParseContext::parseVectorFields(const TSourceLoc &line,
183 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400184 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300185 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300187 ASSERT(fieldOffsets);
188 size_t fieldCount = compString.size();
189 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530190 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000191 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000192 return false;
193 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300194 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195
Jamie Madillb98c3a82015-07-23 14:26:04 -0400196 enum
197 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000198 exyzw,
199 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000200 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000201 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300203 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530204 {
205 switch (compString[i])
206 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400207 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300208 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400209 fieldSet[i] = exyzw;
210 break;
211 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300212 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400213 fieldSet[i] = ergba;
214 break;
215 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300216 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400217 fieldSet[i] = estpq;
218 break;
219 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300220 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 fieldSet[i] = exyzw;
222 break;
223 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400225 fieldSet[i] = ergba;
226 break;
227 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300228 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 fieldSet[i] = estpq;
230 break;
231 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300232 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 fieldSet[i] = exyzw;
234 break;
235 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300236 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400237 fieldSet[i] = ergba;
238 break;
239 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300240 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400241 fieldSet[i] = estpq;
242 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530243
Jamie Madillb98c3a82015-07-23 14:26:04 -0400244 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 fieldSet[i] = exyzw;
247 break;
248 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400250 fieldSet[i] = ergba;
251 break;
252 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400254 fieldSet[i] = estpq;
255 break;
256 default:
257 error(line, "illegal vector field selection", compString.c_str());
258 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000259 }
260 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300262 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300264 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530265 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000267 return false;
268 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269
Arun Patole7e7e68d2015-05-22 12:02:25 +0530270 if (i > 0)
271 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 error(line, "illegal - vector component fields not from the same set",
275 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000276 return false;
277 }
278 }
279 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000281 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282}
283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284///////////////////////////////////////////////////////////////////////
285//
286// Errors
287//
288////////////////////////////////////////////////////////////////////////
289
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290//
291// Used by flex/bison to output all syntax and parsing errors.
292//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000293void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000295 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296}
297
Olli Etuaho4de340a2016-12-16 09:32:03 +0000298void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530299{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000300 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000301}
302
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200303void TParseContext::outOfRangeError(bool isError,
304 const TSourceLoc &loc,
305 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000306 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200307{
308 if (isError)
309 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000310 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200311 }
312 else
313 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000314 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200315 }
316}
317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318//
319// Same error message for all places assignments don't work.
320//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000322{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000323 std::stringstream reasonStream;
324 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
325 std::string reason = reasonStream.str();
326 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327}
328
329//
330// Same error message for all places unary operations don't work.
331//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530332void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000334 std::stringstream reasonStream;
335 reasonStream << "wrong operand type - no operation '" << op
336 << "' exists that takes an operand of type " << operand
337 << " (or there is no acceptable conversion)";
338 std::string reason = reasonStream.str();
339 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340}
341
342//
343// Same error message for all binary operations don't work.
344//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400345void TParseContext::binaryOpError(const TSourceLoc &line,
346 const char *op,
347 TString left,
348 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000350 std::stringstream reasonStream;
351 reasonStream << "wrong operand types - no operation '" << op
352 << "' exists that takes a left-hand operand of type '" << left
353 << "' and a right operand of type '" << right
354 << "' (or there is no acceptable conversion)";
355 std::string reason = reasonStream.str();
356 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
Olli Etuaho856c4972016-08-08 11:38:39 +0300359void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
360 TPrecision precision,
361 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530362{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400363 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300364 return;
Martin Radev70866b82016-07-22 15:27:42 +0300365
366 if (precision != EbpUndefined && !SupportsPrecision(type))
367 {
368 error(line, "illegal type for precision qualifier", getBasicString(type));
369 }
370
Olli Etuaho183d7e22015-11-20 15:59:09 +0200371 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200373 switch (type)
374 {
375 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400376 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300377 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200378 case EbtInt:
379 case EbtUInt:
380 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400381 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300382 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200383 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800384 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200385 {
jchen10cc2a10e2017-05-03 14:05:12 +0800386 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300387 return;
388 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200389 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000390 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000391}
392
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393// Both test and if necessary, spit out an error, to see if the node is really
394// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300395bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500397 TIntermSymbol *symNode = node->getAsSymbolNode();
398 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100399 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
400
401 if (swizzleNode)
402 {
403 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
404 if (ok && swizzleNode->hasDuplicateOffsets())
405 {
406 error(line, " l-value of swizzle cannot have duplicate components", op);
407 return false;
408 }
409 return ok;
410 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412 if (binaryNode)
413 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400414 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530415 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400416 case EOpIndexDirect:
417 case EOpIndexIndirect:
418 case EOpIndexDirectStruct:
419 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300420 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400421 default:
422 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000423 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000424 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300425 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000426 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427
jchen10cc2a10e2017-05-03 14:05:12 +0800428 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530429 switch (node->getQualifier())
430 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400431 case EvqConst:
432 message = "can't modify a const";
433 break;
434 case EvqConstReadOnly:
435 message = "can't modify a const";
436 break;
437 case EvqAttribute:
438 message = "can't modify an attribute";
439 break;
440 case EvqFragmentIn:
441 message = "can't modify an input";
442 break;
443 case EvqVertexIn:
444 message = "can't modify an input";
445 break;
446 case EvqUniform:
447 message = "can't modify a uniform";
448 break;
449 case EvqVaryingIn:
450 message = "can't modify a varying";
451 break;
452 case EvqFragCoord:
453 message = "can't modify gl_FragCoord";
454 break;
455 case EvqFrontFacing:
456 message = "can't modify gl_FrontFacing";
457 break;
458 case EvqPointCoord:
459 message = "can't modify gl_PointCoord";
460 break;
Martin Radevb0883602016-08-04 17:48:58 +0300461 case EvqNumWorkGroups:
462 message = "can't modify gl_NumWorkGroups";
463 break;
464 case EvqWorkGroupSize:
465 message = "can't modify gl_WorkGroupSize";
466 break;
467 case EvqWorkGroupID:
468 message = "can't modify gl_WorkGroupID";
469 break;
470 case EvqLocalInvocationID:
471 message = "can't modify gl_LocalInvocationID";
472 break;
473 case EvqGlobalInvocationID:
474 message = "can't modify gl_GlobalInvocationID";
475 break;
476 case EvqLocalInvocationIndex:
477 message = "can't modify gl_LocalInvocationIndex";
478 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300479 case EvqViewIDOVR:
480 message = "can't modify gl_ViewID_OVR";
481 break;
Martin Radev802abe02016-08-04 17:48:32 +0300482 case EvqComputeIn:
483 message = "can't modify work group size variable";
484 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 //
487 // Type that can't be written to?
488 //
489 if (node->getBasicType() == EbtVoid)
490 {
491 message = "can't modify void";
492 }
jchen10cc2a10e2017-05-03 14:05:12 +0800493 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400494 {
jchen10cc2a10e2017-05-03 14:05:12 +0800495 message = "can't modify a variable with type ";
496 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300497 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800498 else if (node->getMemoryQualifier().readonly)
499 {
500 message = "can't modify a readonly variable";
501 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
jchen10cc2a10e2017-05-03 14:05:12 +0800504 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000506 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507
Olli Etuaho8a176262016-08-16 14:23:01 +0300508 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000511 //
512 // Everything else is okay, no error.
513 //
jchen10cc2a10e2017-05-03 14:05:12 +0800514 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300515 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 //
518 // If we get here, we have an error and a message.
519 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530520 if (symNode)
521 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000522 const char *symbol = symNode->getSymbol().c_str();
523 std::stringstream reasonStream;
524 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
525 std::string reason = reasonStream.str();
526 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000527 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530528 else
529 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000530 std::stringstream reasonStream;
531 reasonStream << "l-value required (" << message << ")";
532 std::string reason = reasonStream.str();
533 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000534 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535
Olli Etuaho8a176262016-08-16 14:23:01 +0300536 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537}
538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539// Both test, and if necessary spit out an error, to see if the node is really
540// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300541void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542{
Olli Etuaho383b7912016-08-05 11:22:59 +0300543 if (node->getQualifier() != EvqConst)
544 {
545 error(node->getLine(), "constant expression required", "");
546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547}
548
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549// Both test, and if necessary spit out an error, to see if the node is really
550// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300551void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552{
Olli Etuaho383b7912016-08-05 11:22:59 +0300553 if (!node->isScalarInt())
554 {
555 error(node->getLine(), "integer expression required", token);
556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557}
558
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559// Both test, and if necessary spit out an error, to see if we are currently
560// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800561bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562{
Olli Etuaho856c4972016-08-08 11:38:39 +0300563 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300564 {
565 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800566 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300567 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800568 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569}
570
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300571// ESSL 3.00.5 sections 3.8 and 3.9.
572// If it starts "gl_" or contains two consecutive underscores, it's reserved.
573// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300574bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530576 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300577 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530578 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300579 error(line, reservedErrMsg, "gl_");
580 return false;
581 }
582 if (sh::IsWebGLBasedSpec(mShaderSpec))
583 {
584 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300586 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300587 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300589 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300591 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300592 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 }
594 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300595 if (identifier.find("__") != TString::npos)
596 {
597 error(line,
598 "identifiers containing two consecutive underscores (__) are reserved as "
599 "possible future keywords",
600 identifier.c_str());
601 return false;
602 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300603 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604}
605
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300606// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300607bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800608 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300609 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800611 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200613 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300614 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300617 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300619 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200620 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300621 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200622 {
jchen10cc2a10e2017-05-03 14:05:12 +0800623 std::string reason("cannot convert a variable with type ");
624 reason += getBasicString(argTyped->getBasicType());
625 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300626 return false;
627 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800628 else if (argTyped->getMemoryQualifier().writeonly)
629 {
630 error(line, "cannot convert a variable with writeonly", "constructor");
631 return false;
632 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200633 if (argTyped->getBasicType() == EbtVoid)
634 {
635 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300636 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200637 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639
Olli Etuaho856c4972016-08-08 11:38:39 +0300640 if (type.isArray())
641 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300642 // The size of an unsized constructor should already have been determined.
643 ASSERT(!type.isUnsizedArray());
644 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
645 {
646 error(line, "array constructor needs one argument per array element", "constructor");
647 return false;
648 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300649 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
650 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800651 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300652 {
653 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500654 if (argType.isArray())
655 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300656 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500657 return false;
658 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300659 if (!argType.sameElementType(type))
660 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000661 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300662 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300663 }
664 }
665 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300666 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300667 {
668 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300669 if (fields.size() != arguments->size())
670 {
671 error(line,
672 "Number of constructor parameters does not match the number of structure fields",
673 "constructor");
674 return false;
675 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300676
677 for (size_t i = 0; i < fields.size(); i++)
678 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800679 if (i >= arguments->size() ||
680 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300681 {
682 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000683 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300684 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300685 }
686 }
687 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300688 else
689 {
690 // We're constructing a scalar, vector, or matrix.
691
692 // Note: It's okay to have too many components available, but not okay to have unused
693 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
694 // there is an extra argument, so 'overFull' will become true.
695
696 size_t size = 0;
697 bool full = false;
698 bool overFull = false;
699 bool matrixArg = false;
700 for (TIntermNode *arg : *arguments)
701 {
702 const TIntermTyped *argTyped = arg->getAsTyped();
703 ASSERT(argTyped != nullptr);
704
Olli Etuaho487b63a2017-05-23 15:55:09 +0300705 if (argTyped->getBasicType() == EbtStruct)
706 {
707 error(line, "a struct cannot be used as a constructor argument for this type",
708 "constructor");
709 return false;
710 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300711 if (argTyped->getType().isArray())
712 {
713 error(line, "constructing from a non-dereferenced array", "constructor");
714 return false;
715 }
716 if (argTyped->getType().isMatrix())
717 {
718 matrixArg = true;
719 }
720
721 size += argTyped->getType().getObjectSize();
722 if (full)
723 {
724 overFull = true;
725 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300726 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300727 {
728 full = true;
729 }
730 }
731
732 if (type.isMatrix() && matrixArg)
733 {
734 if (arguments->size() != 1)
735 {
736 error(line, "constructing matrix from matrix can only take one argument",
737 "constructor");
738 return false;
739 }
740 }
741 else
742 {
743 if (size != 1 && size < type.getObjectSize())
744 {
745 error(line, "not enough data provided for construction", "constructor");
746 return false;
747 }
748 if (overFull)
749 {
750 error(line, "too many arguments", "constructor");
751 return false;
752 }
753 }
754 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300755
Olli Etuaho8a176262016-08-16 14:23:01 +0300756 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757}
758
Jamie Madillb98c3a82015-07-23 14:26:04 -0400759// This function checks to see if a void variable has been declared and raise an error message for
760// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761//
762// returns true in case of an error
763//
Olli Etuaho856c4972016-08-08 11:38:39 +0300764bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400765 const TString &identifier,
766 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300768 if (type == EbtVoid)
769 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000770 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300772 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775}
776
Jamie Madillb98c3a82015-07-23 14:26:04 -0400777// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300778// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300779bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300781 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530782 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000783 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300784 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530785 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300786 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787}
788
Jamie Madillb98c3a82015-07-23 14:26:04 -0400789// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300790// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300791void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792{
Martin Radev4a9cd802016-09-01 16:51:51 +0300793 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530794 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000795 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797}
798
jchen10cc2a10e2017-05-03 14:05:12 +0800799bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
800 const TTypeSpecifierNonArray &pType,
801 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530803 if (pType.type == EbtStruct)
804 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300805 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530806 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000807 std::stringstream reasonStream;
808 reasonStream << reason << " (structure contains a sampler)";
809 std::string reasonStr = reasonStream.str();
810 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300811 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 }
jchen10cc2a10e2017-05-03 14:05:12 +0800813 // only samplers need to be checked from structs, since other opaque types can't be struct
814 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300815 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530816 }
jchen10cc2a10e2017-05-03 14:05:12 +0800817 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530818 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000819 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300820 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
Olli Etuaho8a176262016-08-16 14:23:01 +0300823 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824}
825
Olli Etuaho856c4972016-08-08 11:38:39 +0300826void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
827 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400828{
829 if (pType.layoutQualifier.location != -1)
830 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400831 error(line, "location must only be specified for a single input or output variable",
832 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400833 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400834}
835
Olli Etuaho856c4972016-08-08 11:38:39 +0300836void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
837 const TLayoutQualifier &layoutQualifier)
838{
839 if (layoutQualifier.location != -1)
840 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000841 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
842 if (mShaderVersion >= 310)
843 {
844 errorMsg =
845 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
846 }
847 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300848 }
849}
850
Martin Radev2cc85b32016-08-05 16:22:53 +0300851void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
852 TQualifier qualifier,
853 const TType &type)
854{
Martin Radev2cc85b32016-08-05 16:22:53 +0300855 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800856 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530857 {
jchen10cc2a10e2017-05-03 14:05:12 +0800858 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300863unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530865 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000866
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200867 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
868 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
869 // fold as array size.
870 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000871 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000872 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300873 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000874 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875
Olli Etuaho856c4972016-08-08 11:38:39 +0300876 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400877
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000878 if (constant->getBasicType() == EbtUInt)
879 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000881 }
882 else
883 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300884 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000885
Olli Etuaho856c4972016-08-08 11:38:39 +0300886 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000887 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400888 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300889 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000890 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400891
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400893 }
894
Olli Etuaho856c4972016-08-08 11:38:39 +0300895 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400896 {
897 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300898 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400899 }
900
901 // The size of arrays is restricted here to prevent issues further down the
902 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
903 // 4096 registers so this should be reasonable even for aggressively optimizable code.
904 const unsigned int sizeLimit = 65536;
905
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400907 {
908 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300909 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000910 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300911
912 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300916bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
917 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918{
Olli Etuaho8a176262016-08-16 14:23:01 +0300919 if ((elementQualifier.qualifier == EvqAttribute) ||
920 (elementQualifier.qualifier == EvqVertexIn) ||
921 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300922 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400923 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 TType(elementQualifier).getQualifierString());
925 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000926 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
Olli Etuaho8a176262016-08-16 14:23:01 +0300928 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929}
930
Olli Etuaho8a176262016-08-16 14:23:01 +0300931// See if this element type can be formed into an array.
932bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000934 //
935 // Can the type be an array?
936 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300937 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400938 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300939 error(line, "cannot declare arrays of arrays",
940 TType(elementType).getCompleteString().c_str());
941 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000942 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300943 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
944 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
945 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300946 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300947 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300948 {
949 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300950 TType(elementType).getCompleteString().c_str());
951 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300952 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953
Olli Etuaho8a176262016-08-16 14:23:01 +0300954 return true;
955}
956
957// Check if this qualified element type can be formed into an array.
958bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
959 const TPublicType &elementType)
960{
961 if (checkIsValidTypeForArray(indexLocation, elementType))
962 {
963 return checkIsValidQualifierForArray(indexLocation, elementType);
964 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000965 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
967
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300969void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
970 const TString &identifier,
971 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972{
Olli Etuaho3739d232015-04-08 12:23:44 +0300973 ASSERT(type != nullptr);
974 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000975 {
976 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300977 type->qualifier = EvqTemporary;
978
979 // Generate informative error messages for ESSL1.
980 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400981 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000982 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530983 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400984 "structures containing arrays may not be declared constant since they cannot be "
985 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530986 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000987 }
988 else
989 {
990 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
991 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300992 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000993 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300994 if (type->isUnsizedArray())
995 {
996 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300997 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
Olli Etuaho2935c582015-04-08 14:32:06 +03001000// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001// and update the symbol table.
1002//
Olli Etuaho2935c582015-04-08 14:32:06 +03001003// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001005bool TParseContext::declareVariable(const TSourceLoc &line,
1006 const TString &identifier,
1007 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001008 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009{
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011
Olli Etuaho43364892017-02-13 16:00:12 +00001012 checkBindingIsValid(line, type);
1013
Olli Etuaho856c4972016-08-08 11:38:39 +03001014 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015
Olli Etuaho2935c582015-04-08 14:32:06 +03001016 // gl_LastFragData may be redeclared with a new precision qualifier
1017 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1020 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001021 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001022 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001023 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001024 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001025 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001026 }
1027 }
1028 else
1029 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001030 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1031 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001032 return false;
1033 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
Olli Etuaho8a176262016-08-16 14:23:01 +03001036 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001037 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
Olli Etuaho2935c582015-04-08 14:32:06 +03001039 (*variable) = new TVariable(&identifier, type);
1040 if (!symbolTable.declare(*variable))
1041 {
1042 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001043 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001044 return false;
1045 }
1046
Olli Etuaho8a176262016-08-16 14:23:01 +03001047 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001048 return false;
1049
1050 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001051}
1052
Martin Radev70866b82016-07-22 15:27:42 +03001053void TParseContext::checkIsParameterQualifierValid(
1054 const TSourceLoc &line,
1055 const TTypeQualifierBuilder &typeQualifierBuilder,
1056 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301057{
Olli Etuahocce89652017-06-19 16:04:09 +03001058 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001059 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001060
1061 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301062 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001063 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1064 }
1065
1066 if (!IsImage(type->getBasicType()))
1067 {
Olli Etuaho43364892017-02-13 16:00:12 +00001068 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001069 }
1070 else
1071 {
1072 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074
Martin Radev70866b82016-07-22 15:27:42 +03001075 type->setQualifier(typeQualifier.qualifier);
1076
1077 if (typeQualifier.precision != EbpUndefined)
1078 {
1079 type->setPrecision(typeQualifier.precision);
1080 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
Olli Etuaho856c4972016-08-08 11:38:39 +03001083bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001084{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001085 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001086 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087 if (iter == extBehavior.end())
1088 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001089 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001090 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001091 }
zmo@google.comf5450912011-09-09 01:37:19 +00001092 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1094 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001095 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1096 // associated with more than one extension.
1097 if (extension == "GL_OVR_multiview")
1098 {
1099 return checkCanUseExtension(line, "GL_OVR_multiview2");
1100 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001101 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001102 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001103 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104 if (iter->second == EBhWarn)
1105 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001106 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001107 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109
Olli Etuaho8a176262016-08-16 14:23:01 +03001110 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001111}
1112
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001113// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1114// compile-time or link-time errors are the same whether or not the declaration is empty".
1115// This function implements all the checks that are done on qualifiers regardless of if the
1116// declaration is empty.
1117void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1118 const sh::TLayoutQualifier &layoutQualifier,
1119 const TSourceLoc &location)
1120{
1121 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1122 {
1123 error(location, "Shared memory declarations cannot have layout specified", "layout");
1124 }
1125
1126 if (layoutQualifier.matrixPacking != EmpUnspecified)
1127 {
1128 error(location, "layout qualifier only valid for interface blocks",
1129 getMatrixPackingString(layoutQualifier.matrixPacking));
1130 return;
1131 }
1132
1133 if (layoutQualifier.blockStorage != EbsUnspecified)
1134 {
1135 error(location, "layout qualifier only valid for interface blocks",
1136 getBlockStorageString(layoutQualifier.blockStorage));
1137 return;
1138 }
1139
1140 if (qualifier == EvqFragmentOut)
1141 {
1142 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1143 {
1144 error(location, "invalid layout qualifier combination", "yuv");
1145 return;
1146 }
1147 }
1148 else
1149 {
1150 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1151 }
1152
Olli Etuaho95468d12017-05-04 11:14:34 +03001153 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1154 // parsing steps. So it needs to be checked here.
1155 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1156 {
1157 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1158 }
1159
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001160 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1161 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1162 {
1163 canHaveLocation = true;
1164 // We're not checking whether the uniform location is in range here since that depends on
1165 // the type of the variable.
1166 // The type can only be fully determined for non-empty declarations.
1167 }
1168 if (!canHaveLocation)
1169 {
1170 checkLocationIsNotSpecified(location, layoutQualifier);
1171 }
1172}
1173
jchen104cdac9e2017-05-08 11:01:20 +08001174void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1175 const TSourceLoc &location)
1176{
1177 if (publicType.precision != EbpHigh)
1178 {
1179 error(location, "Can only be highp", "atomic counter");
1180 }
1181 // dEQP enforces compile error if location is specified. See uniform_location.test.
1182 if (publicType.layoutQualifier.location != -1)
1183 {
1184 error(location, "location must not be set for atomic_uint", "layout");
1185 }
1186 if (publicType.layoutQualifier.binding == -1)
1187 {
1188 error(location, "no binding specified", "atomic counter");
1189 }
1190}
1191
Martin Radevb8b01222016-11-20 23:25:53 +02001192void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1193 const TSourceLoc &location)
1194{
1195 if (publicType.isUnsizedArray())
1196 {
1197 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1198 // error. It is assumed that this applies to empty declarations as well.
1199 error(location, "empty array declaration needs to specify a size", "");
1200 }
Martin Radevb8b01222016-11-20 23:25:53 +02001201}
1202
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001203// These checks are done for all declarations that are non-empty. They're done for non-empty
1204// declarations starting a declarator list, and declarators that follow an empty declaration.
1205void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1206 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001207{
Olli Etuahofa33d582015-04-09 14:33:12 +03001208 switch (publicType.qualifier)
1209 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001210 case EvqVaryingIn:
1211 case EvqVaryingOut:
1212 case EvqAttribute:
1213 case EvqVertexIn:
1214 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001215 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001216 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001217 {
1218 error(identifierLocation, "cannot be used with a structure",
1219 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001220 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001221 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001222 break;
1223 case EvqBuffer:
1224 if (publicType.getBasicType() != EbtInterfaceBlock)
1225 {
1226 error(identifierLocation,
1227 "cannot declare buffer variables at global scope(outside a block)",
1228 getQualifierString(publicType.qualifier));
1229 return;
1230 }
1231 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001232 default:
1233 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001234 }
jchen10cc2a10e2017-05-03 14:05:12 +08001235 std::string reason(getBasicString(publicType.getBasicType()));
1236 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001237 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001238 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001239 {
1240 return;
1241 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001242
Andrei Volykhina5527072017-03-22 16:46:30 +03001243 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1244 publicType.qualifier != EvqConst) &&
1245 publicType.getBasicType() == EbtYuvCscStandardEXT)
1246 {
1247 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1248 getQualifierString(publicType.qualifier));
1249 return;
1250 }
1251
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001252 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1253 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001254 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1255 // But invalid shaders may still reach here with an unsized array declaration.
1256 if (!publicType.isUnsizedArray())
1257 {
1258 TType type(publicType);
1259 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1260 publicType.layoutQualifier);
1261 }
1262 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001263
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001264 // check for layout qualifier issues
1265 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001266
Martin Radev2cc85b32016-08-05 16:22:53 +03001267 if (IsImage(publicType.getBasicType()))
1268 {
1269
1270 switch (layoutQualifier.imageInternalFormat)
1271 {
1272 case EiifRGBA32F:
1273 case EiifRGBA16F:
1274 case EiifR32F:
1275 case EiifRGBA8:
1276 case EiifRGBA8_SNORM:
1277 if (!IsFloatImage(publicType.getBasicType()))
1278 {
1279 error(identifierLocation,
1280 "internal image format requires a floating image type",
1281 getBasicString(publicType.getBasicType()));
1282 return;
1283 }
1284 break;
1285 case EiifRGBA32I:
1286 case EiifRGBA16I:
1287 case EiifRGBA8I:
1288 case EiifR32I:
1289 if (!IsIntegerImage(publicType.getBasicType()))
1290 {
1291 error(identifierLocation,
1292 "internal image format requires an integer image type",
1293 getBasicString(publicType.getBasicType()));
1294 return;
1295 }
1296 break;
1297 case EiifRGBA32UI:
1298 case EiifRGBA16UI:
1299 case EiifRGBA8UI:
1300 case EiifR32UI:
1301 if (!IsUnsignedImage(publicType.getBasicType()))
1302 {
1303 error(identifierLocation,
1304 "internal image format requires an unsigned image type",
1305 getBasicString(publicType.getBasicType()));
1306 return;
1307 }
1308 break;
1309 case EiifUnspecified:
1310 error(identifierLocation, "layout qualifier", "No image internal format specified");
1311 return;
1312 default:
1313 error(identifierLocation, "layout qualifier", "unrecognized token");
1314 return;
1315 }
1316
1317 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1318 switch (layoutQualifier.imageInternalFormat)
1319 {
1320 case EiifR32F:
1321 case EiifR32I:
1322 case EiifR32UI:
1323 break;
1324 default:
1325 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1326 {
1327 error(identifierLocation, "layout qualifier",
1328 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1329 "image variables must be qualified readonly and/or writeonly");
1330 return;
1331 }
1332 break;
1333 }
1334 }
1335 else
1336 {
Olli Etuaho43364892017-02-13 16:00:12 +00001337 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001338 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1339 }
jchen104cdac9e2017-05-08 11:01:20 +08001340
1341 if (IsAtomicCounter(publicType.getBasicType()))
1342 {
1343 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1344 }
1345 else
1346 {
1347 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1348 }
Olli Etuaho43364892017-02-13 16:00:12 +00001349}
Martin Radev2cc85b32016-08-05 16:22:53 +03001350
Olli Etuaho43364892017-02-13 16:00:12 +00001351void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1352{
1353 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1354 int arraySize = type.isArray() ? type.getArraySize() : 1;
1355 if (IsImage(type.getBasicType()))
1356 {
1357 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1358 }
1359 else if (IsSampler(type.getBasicType()))
1360 {
1361 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1362 }
jchen104cdac9e2017-05-08 11:01:20 +08001363 else if (IsAtomicCounter(type.getBasicType()))
1364 {
1365 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1366 }
Olli Etuaho43364892017-02-13 16:00:12 +00001367 else
1368 {
1369 ASSERT(!IsOpaqueType(type.getBasicType()));
1370 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001371 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001372}
1373
Olli Etuaho856c4972016-08-08 11:38:39 +03001374void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1375 const TString &layoutQualifierName,
1376 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001377{
1378
1379 if (mShaderVersion < versionRequired)
1380 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001381 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001382 }
1383}
1384
Olli Etuaho856c4972016-08-08 11:38:39 +03001385bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1386 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001387{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001388 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001389 for (size_t i = 0u; i < localSize.size(); ++i)
1390 {
1391 if (localSize[i] != -1)
1392 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001393 error(location,
1394 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1395 "global layout declaration",
1396 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001397 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001398 }
1399 }
1400
Olli Etuaho8a176262016-08-16 14:23:01 +03001401 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001402}
1403
Olli Etuaho43364892017-02-13 16:00:12 +00001404void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001405 TLayoutImageInternalFormat internalFormat)
1406{
1407 if (internalFormat != EiifUnspecified)
1408 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001409 error(location, "invalid layout qualifier: only valid when used with images",
1410 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001411 }
Olli Etuaho43364892017-02-13 16:00:12 +00001412}
1413
1414void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1415{
1416 if (binding != -1)
1417 {
1418 error(location,
1419 "invalid layout qualifier: only valid when used with opaque types or blocks",
1420 "binding");
1421 }
1422}
1423
jchen104cdac9e2017-05-08 11:01:20 +08001424void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1425{
1426 if (offset != -1)
1427 {
1428 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1429 "offset");
1430 }
1431}
1432
Olli Etuaho43364892017-02-13 16:00:12 +00001433void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1434{
1435 // Expects arraySize to be 1 when setting binding for only a single variable.
1436 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1437 {
1438 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1439 }
1440}
1441
1442void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1443 int binding,
1444 int arraySize)
1445{
1446 // Expects arraySize to be 1 when setting binding for only a single variable.
1447 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1448 {
1449 error(location, "sampler binding greater than maximum texture units", "binding");
1450 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001451}
1452
Jiajia Qinbc585152017-06-23 15:42:17 +08001453void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1454 const TQualifier &qualifier,
1455 int binding,
1456 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001457{
1458 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001459 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001460 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001461 if (binding + size > mMaxUniformBufferBindings)
1462 {
1463 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1464 "binding");
1465 }
1466 }
1467 else if (qualifier == EvqBuffer)
1468 {
1469 if (binding + size > mMaxShaderStorageBufferBindings)
1470 {
1471 error(location,
1472 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1473 "binding");
1474 }
jchen10af713a22017-04-19 09:10:56 +08001475 }
1476}
jchen104cdac9e2017-05-08 11:01:20 +08001477void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1478{
1479 if (binding >= mMaxAtomicCounterBindings)
1480 {
1481 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1482 "binding");
1483 }
1484}
jchen10af713a22017-04-19 09:10:56 +08001485
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001486void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1487 int objectLocationCount,
1488 const TLayoutQualifier &layoutQualifier)
1489{
1490 int loc = layoutQualifier.location;
1491 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1492 {
1493 error(location, "Uniform location out of range", "location");
1494 }
1495}
1496
Andrei Volykhina5527072017-03-22 16:46:30 +03001497void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1498{
1499 if (yuv != false)
1500 {
1501 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1502 }
1503}
1504
Jiajia Qinbc585152017-06-23 15:42:17 +08001505void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1506 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001507{
1508 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1509 {
1510 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001511 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1512 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1513 qual == EvqInOut || qual == EvqConstReadOnly))
1514 {
1515 if (argument->getMemoryQualifier().writeonly)
1516 {
1517 error(argument->getLine(),
1518 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1519 fnCall->getFunctionSymbolInfo()->getName().c_str());
1520 return;
1521 }
1522 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001523 if (qual == EvqOut || qual == EvqInOut)
1524 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001525 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001526 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001527 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001528 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001529 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001530 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001531 }
1532 }
1533 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001534}
1535
Martin Radev70866b82016-07-22 15:27:42 +03001536void TParseContext::checkInvariantVariableQualifier(bool invariant,
1537 const TQualifier qualifier,
1538 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001539{
Martin Radev70866b82016-07-22 15:27:42 +03001540 if (!invariant)
1541 return;
1542
1543 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001544 {
Martin Radev70866b82016-07-22 15:27:42 +03001545 // input variables in the fragment shader can be also qualified as invariant
1546 if (!sh::CanBeInvariantESSL1(qualifier))
1547 {
1548 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1549 }
1550 }
1551 else
1552 {
1553 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1554 {
1555 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1556 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001557 }
1558}
1559
Arun Patole7e7e68d2015-05-22 12:02:25 +05301560bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001561{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001562 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001563 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1564 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001565}
1566
Arun Patole7e7e68d2015-05-22 12:02:25 +05301567bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001568{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001569 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001570}
1571
Jamie Madillb98c3a82015-07-23 14:26:04 -04001572void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1573 const char *extName,
1574 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001575{
1576 pp::SourceLocation srcLoc;
1577 srcLoc.file = loc.first_file;
1578 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001579 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001580}
1581
Jamie Madillb98c3a82015-07-23 14:26:04 -04001582void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1583 const char *name,
1584 const char *value,
1585 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001586{
1587 pp::SourceLocation srcLoc;
1588 srcLoc.file = loc.first_file;
1589 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001590 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001591}
1592
Martin Radev4c4c8e72016-08-04 12:25:34 +03001593sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001594{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001595 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001596 for (size_t i = 0u; i < result.size(); ++i)
1597 {
1598 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1599 {
1600 result[i] = 1;
1601 }
1602 else
1603 {
1604 result[i] = mComputeShaderLocalSize[i];
1605 }
1606 }
1607 return result;
1608}
1609
Olli Etuaho56229f12017-07-10 14:16:33 +03001610TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1611 const TSourceLoc &line)
1612{
1613 TIntermConstantUnion *node = new TIntermConstantUnion(
1614 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1615 node->setLine(line);
1616 return node;
1617}
1618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619/////////////////////////////////////////////////////////////////////////////////
1620//
1621// Non-Errors.
1622//
1623/////////////////////////////////////////////////////////////////////////////////
1624
Jamie Madill5c097022014-08-20 16:38:32 -04001625const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1626 const TString *name,
1627 const TSymbol *symbol)
1628{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001629 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001630
1631 if (!symbol)
1632 {
1633 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001634 }
1635 else if (!symbol->isVariable())
1636 {
1637 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001638 }
1639 else
1640 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001641 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001642
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001643 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001644 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001645 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001646 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001647 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001648
1649 // Reject shaders using both gl_FragData and gl_FragColor
1650 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001651 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001652 {
1653 mUsesFragData = true;
1654 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001655 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001656 {
1657 mUsesFragColor = true;
1658 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001659 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1660 {
1661 mUsesSecondaryOutputs = true;
1662 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001663
1664 // This validation is not quite correct - it's only an error to write to
1665 // both FragData and FragColor. For simplicity, and because users shouldn't
1666 // be rewarded for reading from undefined varaibles, return an error
1667 // if they are both referenced, rather than assigned.
1668 if (mUsesFragData && mUsesFragColor)
1669 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001670 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1671 if (mUsesSecondaryOutputs)
1672 {
1673 errorMessage =
1674 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1675 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1676 }
1677 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001678 }
Martin Radevb0883602016-08-04 17:48:58 +03001679
1680 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1681 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1682 qualifier == EvqWorkGroupSize)
1683 {
1684 error(location,
1685 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1686 "gl_WorkGroupSize");
1687 }
Jamie Madill5c097022014-08-20 16:38:32 -04001688 }
1689
1690 if (!variable)
1691 {
1692 TType type(EbtFloat, EbpUndefined);
1693 TVariable *fakeVariable = new TVariable(name, type);
1694 symbolTable.declare(fakeVariable);
1695 variable = fakeVariable;
1696 }
1697
1698 return variable;
1699}
1700
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001701TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1702 const TString *name,
1703 const TSymbol *symbol)
1704{
1705 const TVariable *variable = getNamedVariable(location, name, symbol);
1706
Olli Etuaho09b04a22016-12-15 13:30:26 +00001707 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1708 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1709 {
1710 // WEBGL_multiview spec
1711 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1712 "gl_ViewID_OVR");
1713 }
1714
Olli Etuaho56229f12017-07-10 14:16:33 +03001715 TIntermTyped *node = nullptr;
1716
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001717 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001718 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001719 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001720 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001721 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001722 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1723 mComputeShaderLocalSizeDeclared)
1724 {
1725 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1726 // needs to be added to the AST as a constant and not as a symbol.
1727 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1728 TConstantUnion *constArray = new TConstantUnion[3];
1729 for (size_t i = 0; i < 3; ++i)
1730 {
1731 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1732 }
1733
1734 ASSERT(variable->getType().getBasicType() == EbtUInt);
1735 ASSERT(variable->getType().getObjectSize() == 3);
1736
1737 TType type(variable->getType());
1738 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001739 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001740 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001741 else
1742 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001743 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001744 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001745 ASSERT(node != nullptr);
1746 node->setLine(location);
1747 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001748}
1749
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750// Initializers show up in several places in the grammar. Have one set of
1751// code to handle them here.
1752//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001753// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001754bool TParseContext::executeInitializer(const TSourceLoc &line,
1755 const TString &identifier,
1756 const TPublicType &pType,
1757 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001758 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759{
Olli Etuaho13389b62016-10-16 11:48:18 +01001760 ASSERT(initNode != nullptr);
1761 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001762 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
Olli Etuaho2935c582015-04-08 14:32:06 +03001764 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001765 if (type.isUnsizedArray())
1766 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001767 // We have not checked yet whether the initializer actually is an array or not.
1768 if (initializer->isArray())
1769 {
1770 type.setArraySize(initializer->getArraySize());
1771 }
1772 else
1773 {
1774 // Having a non-array initializer for an unsized array will result in an error later,
1775 // so we don't generate an error message here.
1776 type.setArraySize(1u);
1777 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001778 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001779 if (!declareVariable(line, identifier, type, &variable))
1780 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001781 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783
Olli Etuahob0c645e2015-05-12 14:25:36 +03001784 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001785 if (symbolTable.atGlobalLevel() &&
1786 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001787 {
1788 // Error message does not completely match behavior with ESSL 1.00, but
1789 // we want to steer developers towards only using constant expressions.
1790 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001791 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001792 }
1793 if (globalInitWarning)
1794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 warning(
1796 line,
1797 "global variable initializers should be constant expressions "
1798 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1799 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001800 }
1801
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001802 //
1803 // identifier must be of type constant, a global, or a temporary
1804 //
1805 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301806 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1807 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001808 error(line, " cannot initialize this type of qualifier ",
1809 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001810 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001811 }
1812 //
1813 // test for and propagate constant
1814 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815
Arun Patole7e7e68d2015-05-22 12:02:25 +05301816 if (qualifier == EvqConst)
1817 {
1818 if (qualifier != initializer->getType().getQualifier())
1819 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001820 std::stringstream reasonStream;
1821 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1822 << "'";
1823 std::string reason = reasonStream.str();
1824 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001825 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001826 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301828 if (type != initializer->getType())
1829 {
1830 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001831 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001832 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001833 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001834 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001835
1836 // Save the constant folded value to the variable if possible. For example array
1837 // initializers are not folded, since that way copying the array literal to multiple places
1838 // in the shader is avoided.
1839 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1840 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301841 if (initializer->getAsConstantUnion())
1842 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001843 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001844 ASSERT(*initNode == nullptr);
1845 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301846 }
1847 else if (initializer->getAsSymbolNode())
1848 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001849 const TSymbol *symbol =
1850 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1851 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001852
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001853 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001854 if (constArray)
1855 {
1856 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001857 ASSERT(*initNode == nullptr);
1858 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001859 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001860 }
1861 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001862
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001863 TIntermSymbol *intermSymbol =
1864 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1865 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001866 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1867 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001868 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001869 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001870 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001871 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872
Olli Etuaho914b79a2017-06-19 16:03:19 +03001873 return true;
1874}
1875
1876TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1877 const TString &identifier,
1878 TIntermTyped *initializer,
1879 const TSourceLoc &loc)
1880{
1881 checkIsScalarBool(loc, pType);
1882 TIntermBinary *initNode = nullptr;
1883 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1884 {
1885 // The initializer is valid. The init condition needs to have a node - either the
1886 // initializer node, or a constant node in case the initialized variable is const and won't
1887 // be recorded in the AST.
1888 if (initNode == nullptr)
1889 {
1890 return initializer;
1891 }
1892 else
1893 {
1894 TIntermDeclaration *declaration = new TIntermDeclaration();
1895 declaration->appendDeclarator(initNode);
1896 return declaration;
1897 }
1898 }
1899 return nullptr;
1900}
1901
1902TIntermNode *TParseContext::addLoop(TLoopType type,
1903 TIntermNode *init,
1904 TIntermNode *cond,
1905 TIntermTyped *expr,
1906 TIntermNode *body,
1907 const TSourceLoc &line)
1908{
1909 TIntermNode *node = nullptr;
1910 TIntermTyped *typedCond = nullptr;
1911 if (cond)
1912 {
1913 typedCond = cond->getAsTyped();
1914 }
1915 if (cond == nullptr || typedCond)
1916 {
Olli Etuahocce89652017-06-19 16:04:09 +03001917 if (type == ELoopDoWhile)
1918 {
1919 checkIsScalarBool(line, typedCond);
1920 }
1921 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1922 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1923 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1924 !typedCond->isVector()));
1925
Olli Etuaho3ec75682017-07-05 17:02:55 +03001926 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001927 node->setLine(line);
1928 return node;
1929 }
1930
Olli Etuahocce89652017-06-19 16:04:09 +03001931 ASSERT(type != ELoopDoWhile);
1932
Olli Etuaho914b79a2017-06-19 16:03:19 +03001933 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1934 ASSERT(declaration);
1935 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1936 ASSERT(declarator->getLeft()->getAsSymbolNode());
1937
1938 // The condition is a declaration. In the AST representation we don't support declarations as
1939 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1940 // the loop.
1941 TIntermBlock *block = new TIntermBlock();
1942
1943 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1944 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1945 block->appendStatement(declareCondition);
1946
1947 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1948 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03001949 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001950 block->appendStatement(loop);
1951 loop->setLine(line);
1952 block->setLine(line);
1953 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954}
1955
Olli Etuahocce89652017-06-19 16:04:09 +03001956TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1957 TIntermNodePair code,
1958 const TSourceLoc &loc)
1959{
Olli Etuaho56229f12017-07-10 14:16:33 +03001960 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03001961
1962 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03001963 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03001964 {
1965 if (cond->getAsConstantUnion()->getBConst(0) == true)
1966 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001967 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03001968 }
1969 else
1970 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001971 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03001972 }
1973 }
1974
Olli Etuaho3ec75682017-07-05 17:02:55 +03001975 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03001976 node->setLine(loc);
1977
1978 return node;
1979}
1980
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001981void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1982{
1983 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1984 typeSpecifier->getBasicType());
1985
1986 if (mShaderVersion < 300 && typeSpecifier->array)
1987 {
1988 error(typeSpecifier->getLine(), "not supported", "first-class array");
1989 typeSpecifier->clearArrayness();
1990 }
1991}
1992
Martin Radev70866b82016-07-22 15:27:42 +03001993TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301994 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001995{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001996 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001997
Martin Radev70866b82016-07-22 15:27:42 +03001998 TPublicType returnType = typeSpecifier;
1999 returnType.qualifier = typeQualifier.qualifier;
2000 returnType.invariant = typeQualifier.invariant;
2001 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002002 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002003 returnType.precision = typeSpecifier.precision;
2004
2005 if (typeQualifier.precision != EbpUndefined)
2006 {
2007 returnType.precision = typeQualifier.precision;
2008 }
2009
Martin Radev4a9cd802016-09-01 16:51:51 +03002010 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2011 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002012
Martin Radev4a9cd802016-09-01 16:51:51 +03002013 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2014 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002015
Martin Radev4a9cd802016-09-01 16:51:51 +03002016 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002017
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002018 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002019 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002020 if (typeSpecifier.array)
2021 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002022 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002023 returnType.clearArrayness();
2024 }
2025
Martin Radev70866b82016-07-22 15:27:42 +03002026 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002027 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002028 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002029 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002030 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002031 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002032
Martin Radev70866b82016-07-22 15:27:42 +03002033 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002034 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002035 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002036 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002037 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002038 }
2039 }
2040 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002041 {
Martin Radev70866b82016-07-22 15:27:42 +03002042 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002043 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002044 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002045 }
Martin Radev70866b82016-07-22 15:27:42 +03002046 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2047 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002048 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002049 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2050 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002051 }
Martin Radev70866b82016-07-22 15:27:42 +03002052 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002053 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002054 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002055 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002056 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002057 }
2058
2059 return returnType;
2060}
2061
Olli Etuaho856c4972016-08-08 11:38:39 +03002062void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2063 const TPublicType &type,
2064 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002065{
2066 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002067 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002068 {
2069 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002070 }
2071
2072 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2073 switch (qualifier)
2074 {
2075 case EvqVertexIn:
2076 // ESSL 3.00 section 4.3.4
2077 if (type.array)
2078 {
2079 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002080 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002081 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002082 return;
2083 case EvqFragmentOut:
2084 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002085 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002086 {
2087 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002088 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002089 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002090 return;
2091 default:
2092 break;
2093 }
2094
2095 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2096 // restrictions.
2097 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002098 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2099 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002100 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2101 {
2102 error(qualifierLocation, "must use 'flat' interpolation here",
2103 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002104 }
2105
Martin Radev4a9cd802016-09-01 16:51:51 +03002106 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002107 {
2108 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2109 // These restrictions are only implied by the ESSL 3.00 spec, but
2110 // the ESSL 3.10 spec lists these restrictions explicitly.
2111 if (type.array)
2112 {
2113 error(qualifierLocation, "cannot be an array of structures",
2114 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002115 }
2116 if (type.isStructureContainingArrays())
2117 {
2118 error(qualifierLocation, "cannot be a structure containing an array",
2119 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002120 }
2121 if (type.isStructureContainingType(EbtStruct))
2122 {
2123 error(qualifierLocation, "cannot be a structure containing a structure",
2124 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002125 }
2126 if (type.isStructureContainingType(EbtBool))
2127 {
2128 error(qualifierLocation, "cannot be a structure containing a bool",
2129 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002130 }
2131 }
2132}
2133
Martin Radev2cc85b32016-08-05 16:22:53 +03002134void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2135{
2136 if (qualifier.getType() == QtStorage)
2137 {
2138 const TStorageQualifierWrapper &storageQualifier =
2139 static_cast<const TStorageQualifierWrapper &>(qualifier);
2140 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2141 !symbolTable.atGlobalLevel())
2142 {
2143 error(storageQualifier.getLine(),
2144 "Local variables can only use the const storage qualifier.",
2145 storageQualifier.getQualifierString().c_str());
2146 }
2147 }
2148}
2149
Olli Etuaho43364892017-02-13 16:00:12 +00002150void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002151 const TSourceLoc &location)
2152{
Jiajia Qinbc585152017-06-23 15:42:17 +08002153 const std::string reason(
2154 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2155 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002156 if (memoryQualifier.readonly)
2157 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002158 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002159 }
2160 if (memoryQualifier.writeonly)
2161 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002162 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002163 }
Martin Radev049edfa2016-11-11 14:35:37 +02002164 if (memoryQualifier.coherent)
2165 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002166 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002167 }
2168 if (memoryQualifier.restrictQualifier)
2169 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002170 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002171 }
2172 if (memoryQualifier.volatileQualifier)
2173 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002174 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002175 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002176}
2177
jchen104cdac9e2017-05-08 11:01:20 +08002178// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2179// intermediate tree.
2180void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2181 size_t size,
2182 bool forceAppend,
2183 const TSourceLoc &loc,
2184 TType &type)
2185{
2186 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2187 int offset;
2188 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2189 {
2190 offset = bindingState.appendSpan(size);
2191 }
2192 else
2193 {
2194 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2195 }
2196 if (offset == -1)
2197 {
2198 error(loc, "Offset overlapping", "atomic counter");
2199 return;
2200 }
2201 TLayoutQualifier qualifier = type.getLayoutQualifier();
2202 qualifier.offset = offset;
2203 type.setLayoutQualifier(qualifier);
2204}
2205
Olli Etuaho13389b62016-10-16 11:48:18 +01002206TIntermDeclaration *TParseContext::parseSingleDeclaration(
2207 TPublicType &publicType,
2208 const TSourceLoc &identifierOrTypeLocation,
2209 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002210{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002211 TType type(publicType);
2212 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2213 mDirectiveHandler.pragma().stdgl.invariantAll)
2214 {
2215 TQualifier qualifier = type.getQualifier();
2216
2217 // The directive handler has already taken care of rejecting invalid uses of this pragma
2218 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2219 // affected variable declarations:
2220 //
2221 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2222 // elsewhere, in TranslatorGLSL.)
2223 //
2224 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2225 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2226 // the way this is currently implemented we have to enable this compiler option before
2227 // parsing the shader and determining the shading language version it uses. If this were
2228 // implemented as a post-pass, the workaround could be more targeted.
2229 //
2230 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2231 // the specification, but there are desktop OpenGL drivers that expect that this is the
2232 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2233 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2234 {
2235 type.setInvariant(true);
2236 }
2237 }
2238
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002239 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2240 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002241
Olli Etuahobab4c082015-04-24 16:38:49 +03002242 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002243 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002244
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002245 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002246 if (emptyDeclaration)
2247 {
Martin Radevb8b01222016-11-20 23:25:53 +02002248 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002249 // In most cases we don't need to create a symbol node for an empty declaration.
2250 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2251 if (type.getBasicType() == EbtStruct)
2252 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002253 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002254 }
jchen104cdac9e2017-05-08 11:01:20 +08002255 else if (IsAtomicCounter(publicType.getBasicType()))
2256 {
2257 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2258 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002259 }
2260 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002261 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002262 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002263
Olli Etuaho856c4972016-08-08 11:38:39 +03002264 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002265
jchen104cdac9e2017-05-08 11:01:20 +08002266 if (IsAtomicCounter(publicType.getBasicType()))
2267 {
2268
2269 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2270 identifierOrTypeLocation, type);
2271 }
2272
Olli Etuaho2935c582015-04-08 14:32:06 +03002273 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002274 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002275
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002276 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002277 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002278 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002279 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002280 }
2281
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002282 TIntermDeclaration *declaration = new TIntermDeclaration();
2283 declaration->setLine(identifierOrTypeLocation);
2284 if (symbol)
2285 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002286 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002287 declaration->appendDeclarator(symbol);
2288 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002289 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002290}
2291
Olli Etuaho13389b62016-10-16 11:48:18 +01002292TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2293 const TSourceLoc &identifierLocation,
2294 const TString &identifier,
2295 const TSourceLoc &indexLocation,
2296 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002297{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002298 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002299
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002300 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2301 identifierLocation);
2302
2303 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002304
Olli Etuaho856c4972016-08-08 11:38:39 +03002305 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002306
Olli Etuaho8a176262016-08-16 14:23:01 +03002307 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002308
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002309 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002310
Olli Etuaho856c4972016-08-08 11:38:39 +03002311 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002312 // Make the type an array even if size check failed.
2313 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2314 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002315
jchen104cdac9e2017-05-08 11:01:20 +08002316 if (IsAtomicCounter(publicType.getBasicType()))
2317 {
2318 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2319 identifierLocation, arrayType);
2320 }
2321
Olli Etuaho2935c582015-04-08 14:32:06 +03002322 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002323 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002324
Olli Etuaho13389b62016-10-16 11:48:18 +01002325 TIntermDeclaration *declaration = new TIntermDeclaration();
2326 declaration->setLine(identifierLocation);
2327
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002328 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002329 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002330 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2331 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002332 declaration->appendDeclarator(symbol);
2333 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002334
Olli Etuaho13389b62016-10-16 11:48:18 +01002335 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002336}
2337
Olli Etuaho13389b62016-10-16 11:48:18 +01002338TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2339 const TSourceLoc &identifierLocation,
2340 const TString &identifier,
2341 const TSourceLoc &initLocation,
2342 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002343{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002344 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002345
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002346 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2347 identifierLocation);
2348
2349 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002350
Olli Etuaho13389b62016-10-16 11:48:18 +01002351 TIntermDeclaration *declaration = new TIntermDeclaration();
2352 declaration->setLine(identifierLocation);
2353
2354 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002355 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002356 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002357 if (initNode)
2358 {
2359 declaration->appendDeclarator(initNode);
2360 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002361 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002362 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002363}
2364
Olli Etuaho13389b62016-10-16 11:48:18 +01002365TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002366 TPublicType &publicType,
2367 const TSourceLoc &identifierLocation,
2368 const TString &identifier,
2369 const TSourceLoc &indexLocation,
2370 TIntermTyped *indexExpression,
2371 const TSourceLoc &initLocation,
2372 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002373{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002374 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002375
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002376 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2377 identifierLocation);
2378
2379 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002380
Olli Etuaho8a176262016-08-16 14:23:01 +03002381 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002382
2383 TPublicType arrayType(publicType);
2384
Olli Etuaho856c4972016-08-08 11:38:39 +03002385 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002386 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2387 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002388 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002389 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002390 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002391 }
2392 // Make the type an array even if size check failed.
2393 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2394 arrayType.setArraySize(size);
2395
Olli Etuaho13389b62016-10-16 11:48:18 +01002396 TIntermDeclaration *declaration = new TIntermDeclaration();
2397 declaration->setLine(identifierLocation);
2398
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002399 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002400 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002401 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002402 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002403 if (initNode)
2404 {
2405 declaration->appendDeclarator(initNode);
2406 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002407 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002408
2409 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002410}
2411
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002412TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002413 const TTypeQualifierBuilder &typeQualifierBuilder,
2414 const TSourceLoc &identifierLoc,
2415 const TString *identifier,
2416 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002417{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002418 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002419
Martin Radev70866b82016-07-22 15:27:42 +03002420 if (!typeQualifier.invariant)
2421 {
2422 error(identifierLoc, "Expected invariant", identifier->c_str());
2423 return nullptr;
2424 }
2425 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2426 {
2427 return nullptr;
2428 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002429 if (!symbol)
2430 {
2431 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002432 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002433 }
Martin Radev70866b82016-07-22 15:27:42 +03002434 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002435 {
Martin Radev70866b82016-07-22 15:27:42 +03002436 error(identifierLoc, "invariant declaration specifies qualifier",
2437 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002438 }
Martin Radev70866b82016-07-22 15:27:42 +03002439 if (typeQualifier.precision != EbpUndefined)
2440 {
2441 error(identifierLoc, "invariant declaration specifies precision",
2442 getPrecisionString(typeQualifier.precision));
2443 }
2444 if (!typeQualifier.layoutQualifier.isEmpty())
2445 {
2446 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2447 }
2448
2449 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2450 ASSERT(variable);
2451 const TType &type = variable->getType();
2452
2453 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2454 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002455 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002456
2457 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2458
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002459 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2460 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002461
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002462 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002463}
2464
Olli Etuaho13389b62016-10-16 11:48:18 +01002465void TParseContext::parseDeclarator(TPublicType &publicType,
2466 const TSourceLoc &identifierLocation,
2467 const TString &identifier,
2468 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002469{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002470 // If the declaration starting this declarator list was empty (example: int,), some checks were
2471 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002472 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002473 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002474 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2475 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002476 }
2477
Olli Etuaho856c4972016-08-08 11:38:39 +03002478 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002479
Olli Etuaho856c4972016-08-08 11:38:39 +03002480 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002481
Olli Etuaho2935c582015-04-08 14:32:06 +03002482 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002483 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002484 if (IsAtomicCounter(publicType.getBasicType()))
2485 {
2486 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2487 identifierLocation, type);
2488 }
Olli Etuaho43364892017-02-13 16:00:12 +00002489 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002490
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002491 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002492 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002493 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2494 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002495 declarationOut->appendDeclarator(symbol);
2496 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002497}
2498
Olli Etuaho13389b62016-10-16 11:48:18 +01002499void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2500 const TSourceLoc &identifierLocation,
2501 const TString &identifier,
2502 const TSourceLoc &arrayLocation,
2503 TIntermTyped *indexExpression,
2504 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002505{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002506 // If the declaration starting this declarator list was empty (example: int,), some checks were
2507 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002508 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002509 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002510 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2511 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002512 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002513
Olli Etuaho856c4972016-08-08 11:38:39 +03002514 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002515
Olli Etuaho856c4972016-08-08 11:38:39 +03002516 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002517
Olli Etuaho8a176262016-08-16 14:23:01 +03002518 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002519 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002520 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002521 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002522 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002523
jchen104cdac9e2017-05-08 11:01:20 +08002524 if (IsAtomicCounter(publicType.getBasicType()))
2525 {
2526 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2527 true, identifierLocation, arrayType);
2528 }
2529
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002530 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002531 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002532
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002533 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002534 {
2535 TIntermSymbol *symbol =
2536 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2537 symbol->setLine(identifierLocation);
2538 declarationOut->appendDeclarator(symbol);
2539 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002540 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002541}
2542
Olli Etuaho13389b62016-10-16 11:48:18 +01002543void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2544 const TSourceLoc &identifierLocation,
2545 const TString &identifier,
2546 const TSourceLoc &initLocation,
2547 TIntermTyped *initializer,
2548 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002549{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002550 // If the declaration starting this declarator list was empty (example: int,), some checks were
2551 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002552 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002553 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002554 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2555 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002556 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002557
Olli Etuaho856c4972016-08-08 11:38:39 +03002558 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002559
Olli Etuaho13389b62016-10-16 11:48:18 +01002560 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002561 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002562 {
2563 //
2564 // build the intermediate representation
2565 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002566 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002567 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002568 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002569 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002570 }
2571}
2572
Olli Etuaho13389b62016-10-16 11:48:18 +01002573void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2574 const TSourceLoc &identifierLocation,
2575 const TString &identifier,
2576 const TSourceLoc &indexLocation,
2577 TIntermTyped *indexExpression,
2578 const TSourceLoc &initLocation,
2579 TIntermTyped *initializer,
2580 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002581{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582 // If the declaration starting this declarator list was empty (example: int,), some checks were
2583 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002584 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002585 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002586 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2587 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002588 }
2589
Olli Etuaho856c4972016-08-08 11:38:39 +03002590 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002591
Olli Etuaho8a176262016-08-16 14:23:01 +03002592 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002593
2594 TPublicType arrayType(publicType);
2595
Olli Etuaho856c4972016-08-08 11:38:39 +03002596 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002597 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2598 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002599 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002600 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002601 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002602 }
2603 // Make the type an array even if size check failed.
2604 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2605 arrayType.setArraySize(size);
2606
2607 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002608 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002609 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002610 {
2611 if (initNode)
2612 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002613 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002614 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002615 }
2616}
2617
jchen104cdac9e2017-05-08 11:01:20 +08002618void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2619 const TSourceLoc &location)
2620{
2621 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2622 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2623 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2624 {
2625 error(location, "Requires both binding and offset", "layout");
2626 return;
2627 }
2628 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2629}
2630
Olli Etuahocce89652017-06-19 16:04:09 +03002631void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2632 const TPublicType &type,
2633 const TSourceLoc &loc)
2634{
2635 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2636 !getFragmentPrecisionHigh())
2637 {
2638 error(loc, "precision is not supported in fragment shader", "highp");
2639 }
2640
2641 if (!CanSetDefaultPrecisionOnType(type))
2642 {
2643 error(loc, "illegal type argument for default precision qualifier",
2644 getBasicString(type.getBasicType()));
2645 return;
2646 }
2647 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2648}
2649
Martin Radev70866b82016-07-22 15:27:42 +03002650void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002651{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002652 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002653 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002654
Martin Radev70866b82016-07-22 15:27:42 +03002655 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2656 typeQualifier.line);
2657
Jamie Madillc2128ff2016-07-04 10:26:17 -04002658 // It should never be the case, but some strange parser errors can send us here.
2659 if (layoutQualifier.isEmpty())
2660 {
2661 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002662 return;
2663 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002664
Martin Radev802abe02016-08-04 17:48:32 +03002665 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002666 {
Olli Etuaho43364892017-02-13 16:00:12 +00002667 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002668 return;
2669 }
2670
Olli Etuaho43364892017-02-13 16:00:12 +00002671 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2672
2673 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002674
2675 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2676
Andrei Volykhina5527072017-03-22 16:46:30 +03002677 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2678
jchen104cdac9e2017-05-08 11:01:20 +08002679 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2680
Martin Radev802abe02016-08-04 17:48:32 +03002681 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002682 {
Martin Radev802abe02016-08-04 17:48:32 +03002683 if (mComputeShaderLocalSizeDeclared &&
2684 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2685 {
2686 error(typeQualifier.line, "Work group size does not match the previous declaration",
2687 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002688 return;
2689 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002690
Martin Radev802abe02016-08-04 17:48:32 +03002691 if (mShaderVersion < 310)
2692 {
2693 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002694 return;
2695 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002696
Martin Radev4c4c8e72016-08-04 12:25:34 +03002697 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002698 {
2699 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002700 return;
2701 }
2702
2703 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2704 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2705
2706 const TConstantUnion *maxComputeWorkGroupSizeData =
2707 maxComputeWorkGroupSize->getConstPointer();
2708
2709 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2710 {
2711 if (layoutQualifier.localSize[i] != -1)
2712 {
2713 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2714 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2715 if (mComputeShaderLocalSize[i] < 1 ||
2716 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002718 std::stringstream reasonStream;
2719 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2720 << maxComputeWorkGroupSizeValue;
2721 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002722
Olli Etuaho4de340a2016-12-16 09:32:03 +00002723 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002724 return;
2725 }
2726 }
2727 }
2728
2729 mComputeShaderLocalSizeDeclared = true;
2730 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002731 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002732 {
2733 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2734 // specification.
2735 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2736 {
2737 error(typeQualifier.line, "Number of views does not match the previous declaration",
2738 "layout");
2739 return;
2740 }
2741
2742 if (layoutQualifier.numViews == -1)
2743 {
2744 error(typeQualifier.line, "No num_views specified", "layout");
2745 return;
2746 }
2747
2748 if (layoutQualifier.numViews > mMaxNumViews)
2749 {
2750 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2751 "layout");
2752 return;
2753 }
2754
2755 mNumViews = layoutQualifier.numViews;
2756 }
Martin Radev802abe02016-08-04 17:48:32 +03002757 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002758 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002759 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002760 {
Martin Radev802abe02016-08-04 17:48:32 +03002761 return;
2762 }
2763
Jiajia Qinbc585152017-06-23 15:42:17 +08002764 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002765 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002766 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002767 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002768 return;
2769 }
2770
2771 if (mShaderVersion < 300)
2772 {
2773 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2774 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002775 return;
2776 }
2777
Olli Etuaho09b04a22016-12-15 13:30:26 +00002778 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002779
2780 if (layoutQualifier.matrixPacking != EmpUnspecified)
2781 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002782 if (typeQualifier.qualifier == EvqUniform)
2783 {
2784 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
2785 }
2786 else if (typeQualifier.qualifier == EvqBuffer)
2787 {
2788 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
2789 }
Martin Radev802abe02016-08-04 17:48:32 +03002790 }
2791
2792 if (layoutQualifier.blockStorage != EbsUnspecified)
2793 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002794 if (typeQualifier.qualifier == EvqUniform)
2795 {
2796 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
2797 }
2798 else if (typeQualifier.qualifier == EvqBuffer)
2799 {
2800 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
2801 }
Martin Radev802abe02016-08-04 17:48:32 +03002802 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002803 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002804}
2805
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002806TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2807 const TFunction &function,
2808 const TSourceLoc &location,
2809 bool insertParametersToSymbolTable)
2810{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002811 checkIsNotReserved(location, function.getName());
2812
Olli Etuahofe486322017-03-21 09:30:54 +00002813 TIntermFunctionPrototype *prototype =
2814 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002815 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2816 // point to the data that already exists in the symbol table.
2817 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2818 prototype->setLine(location);
2819
2820 for (size_t i = 0; i < function.getParamCount(); i++)
2821 {
2822 const TConstParameter &param = function.getParam(i);
2823
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002824 TIntermSymbol *symbol = nullptr;
2825
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002826 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2827 // be used for unused args).
2828 if (param.name != nullptr)
2829 {
2830 TVariable *variable = new TVariable(param.name, *param.type);
2831
2832 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002833 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002834 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002835 if (symbolTable.declare(variable))
2836 {
2837 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2838 variable->getType());
2839 }
2840 else
2841 {
2842 error(location, "redefinition", variable->getName().c_str());
2843 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002844 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002845 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002846 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002847 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002848 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2849 // symbol.
2850 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002851 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002852 symbol->setLine(location);
2853 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002854 }
2855 return prototype;
2856}
2857
Olli Etuaho16c745a2017-01-16 17:02:27 +00002858TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2859 const TFunction &parsedFunction,
2860 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002861{
Olli Etuaho476197f2016-10-11 13:59:08 +01002862 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2863 // first declaration. Either way the instance in the symbol table is used to track whether the
2864 // function is declared multiple times.
2865 TFunction *function = static_cast<TFunction *>(
2866 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2867 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002868 {
2869 // ESSL 1.00.17 section 4.2.7.
2870 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2871 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002872 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002873 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002874
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002875 TIntermFunctionPrototype *prototype =
2876 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002877
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002878 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002879
2880 if (!symbolTable.atGlobalLevel())
2881 {
2882 // ESSL 3.00.4 section 4.2.4.
2883 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002884 }
2885
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002886 return prototype;
2887}
2888
Olli Etuaho336b1472016-10-05 16:37:55 +01002889TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002890 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002891 TIntermBlock *functionBody,
2892 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002893{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002894 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002895 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2896 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002897 error(location, "function does not return a value:",
2898 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002899 }
2900
Olli Etuahof51fdd22016-10-03 10:03:40 +01002901 if (functionBody == nullptr)
2902 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002903 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002904 functionBody->setLine(location);
2905 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002906 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002907 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002908 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002909
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002910 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002911 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002912}
2913
Olli Etuaho476197f2016-10-11 13:59:08 +01002914void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2915 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002916 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002917{
Olli Etuaho476197f2016-10-11 13:59:08 +01002918 ASSERT(function);
2919 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002920 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002921 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002922
2923 if (builtIn)
2924 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002925 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002926 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002927 else
Jamie Madill185fb402015-06-12 15:48:48 -04002928 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002929 TFunction *prevDec = static_cast<TFunction *>(
2930 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2931
2932 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2933 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2934 // occurance.
2935 if (*function != prevDec)
2936 {
2937 // Swap the parameters of the previous declaration to the parameters of the function
2938 // definition (parameter names may differ).
2939 prevDec->swapParameters(**function);
2940
2941 // The function definition will share the same symbol as any previous declaration.
2942 *function = prevDec;
2943 }
2944
2945 if ((*function)->isDefined())
2946 {
2947 error(location, "function already has a body", (*function)->getName().c_str());
2948 }
2949
2950 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002951 }
Jamie Madill185fb402015-06-12 15:48:48 -04002952
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002953 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002954 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002955 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002956
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002957 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002958 setLoopNestingLevel(0);
2959}
2960
Jamie Madillb98c3a82015-07-23 14:26:04 -04002961TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002962{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002963 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002964 // We don't know at this point whether this is a function definition or a prototype.
2965 // The definition production code will check for redefinitions.
2966 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002967 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002968 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2969 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002970 //
2971 TFunction *prevDec =
2972 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302973
Martin Radevda6254b2016-12-14 17:00:36 +02002974 if (getShaderVersion() >= 300 &&
2975 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2976 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302977 {
Martin Radevda6254b2016-12-14 17:00:36 +02002978 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302979 // Therefore overloading or redefining builtin functions is an error.
2980 error(location, "Name of a built-in function cannot be redeclared as function",
2981 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302982 }
2983 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002984 {
2985 if (prevDec->getReturnType() != function->getReturnType())
2986 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002987 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002988 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002989 }
2990 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2991 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002992 if (prevDec->getParam(i).type->getQualifier() !=
2993 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002994 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002995 error(location,
2996 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002997 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002998 }
2999 }
3000 }
3001
3002 //
3003 // Check for previously declared variables using the same name.
3004 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003005 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003006 if (prevSym)
3007 {
3008 if (!prevSym->isFunction())
3009 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003010 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003011 }
3012 }
3013 else
3014 {
3015 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003016 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003017 }
3018
3019 // We're at the inner scope level of the function's arguments and body statement.
3020 // Add the function prototype to the surrounding scope instead.
3021 symbolTable.getOuterLevel()->insert(function);
3022
Olli Etuaho78d13742017-01-18 13:06:10 +00003023 // Raise error message if main function takes any parameters or return anything other than void
3024 if (function->getName() == "main")
3025 {
3026 if (function->getParamCount() > 0)
3027 {
3028 error(location, "function cannot take any parameter(s)", "main");
3029 }
3030 if (function->getReturnType().getBasicType() != EbtVoid)
3031 {
3032 error(location, "main function cannot return a value",
3033 function->getReturnType().getBasicString());
3034 }
3035 }
3036
Jamie Madill185fb402015-06-12 15:48:48 -04003037 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003038 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3039 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003040 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3041 //
3042 return function;
3043}
3044
Olli Etuaho9de84a52016-06-14 17:36:01 +03003045TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3046 const TString *name,
3047 const TSourceLoc &location)
3048{
3049 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3050 {
3051 error(location, "no qualifiers allowed for function return",
3052 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003053 }
3054 if (!type.layoutQualifier.isEmpty())
3055 {
3056 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003057 }
jchen10cc2a10e2017-05-03 14:05:12 +08003058 // make sure an opaque type is not involved as well...
3059 std::string reason(getBasicString(type.getBasicType()));
3060 reason += "s can't be function return values";
3061 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003062 if (mShaderVersion < 300)
3063 {
3064 // Array return values are forbidden, but there's also no valid syntax for declaring array
3065 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003066 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003067
3068 if (type.isStructureContainingArrays())
3069 {
3070 // ESSL 1.00.17 section 6.1 Function Definitions
3071 error(location, "structures containing arrays can't be function return values",
3072 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003073 }
3074 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003075
3076 // Add the function as a prototype after parsing it (we do not support recursion)
3077 return new TFunction(name, new TType(type));
3078}
3079
Olli Etuahocce89652017-06-19 16:04:09 +03003080TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3081{
Olli Etuahocce89652017-06-19 16:04:09 +03003082 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3083 return new TFunction(name, returnType);
3084}
3085
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003086TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003087{
Olli Etuahocce89652017-06-19 16:04:09 +03003088 if (mShaderVersion < 300 && publicType.array)
3089 {
3090 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3091 "[]");
3092 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003093 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003094 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003095 error(publicType.getLine(), "constructor can't be a structure definition",
3096 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003097 }
3098
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003099 TType *type = new TType(publicType);
3100 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003101 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003102 error(publicType.getLine(), "cannot construct this type",
3103 getBasicString(publicType.getBasicType()));
3104 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003105 }
3106
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003107 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003108}
3109
Olli Etuahocce89652017-06-19 16:04:09 +03003110TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3111 const TString *name,
3112 const TSourceLoc &nameLoc)
3113{
3114 if (publicType.getBasicType() == EbtVoid)
3115 {
3116 error(nameLoc, "illegal use of type 'void'", name->c_str());
3117 }
3118 checkIsNotReserved(nameLoc, *name);
3119 TType *type = new TType(publicType);
3120 TParameter param = {name, type};
3121 return param;
3122}
3123
3124TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3125 const TSourceLoc &identifierLoc,
3126 TIntermTyped *arraySize,
3127 const TSourceLoc &arrayLoc,
3128 TPublicType *type)
3129{
3130 checkIsValidTypeForArray(arrayLoc, *type);
3131 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3132 type->setArraySize(size);
3133 return parseParameterDeclarator(*type, identifier, identifierLoc);
3134}
3135
Jamie Madillb98c3a82015-07-23 14:26:04 -04003136// This function is used to test for the correctness of the parameters passed to various constructor
3137// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003138//
Olli Etuaho856c4972016-08-08 11:38:39 +03003139// 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 +00003140//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003141TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003142 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303143 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003144{
Olli Etuaho856c4972016-08-08 11:38:39 +03003145 if (type.isUnsizedArray())
3146 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003147 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003148 {
3149 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3150 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003151 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003152 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003153 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003154 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003155
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003156 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003157 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003158 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003159 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003160
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003161 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003162 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003163
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003164 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3165 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003166 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003167 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003168 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003169 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003170}
3171
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003172//
3173// Interface/uniform blocks
3174//
Olli Etuaho13389b62016-10-16 11:48:18 +01003175TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003176 const TTypeQualifierBuilder &typeQualifierBuilder,
3177 const TSourceLoc &nameLine,
3178 const TString &blockName,
3179 TFieldList *fieldList,
3180 const TString *instanceName,
3181 const TSourceLoc &instanceLine,
3182 TIntermTyped *arrayIndex,
3183 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003184{
Olli Etuaho856c4972016-08-08 11:38:39 +03003185 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003186
Olli Etuaho77ba4082016-12-16 12:01:18 +00003187 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003188
Jiajia Qinbc585152017-06-23 15:42:17 +08003189 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003190 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003191 error(typeQualifier.line,
3192 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3193 "3.10",
3194 getQualifierString(typeQualifier.qualifier));
3195 }
3196 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3197 {
3198 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003199 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003200 }
3201
Martin Radev70866b82016-07-22 15:27:42 +03003202 if (typeQualifier.invariant)
3203 {
3204 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3205 }
3206
Jiajia Qinbc585152017-06-23 15:42:17 +08003207 if (typeQualifier.qualifier != EvqBuffer)
3208 {
3209 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3210 }
Olli Etuaho43364892017-02-13 16:00:12 +00003211
jchen10af713a22017-04-19 09:10:56 +08003212 // add array index
3213 unsigned int arraySize = 0;
3214 if (arrayIndex != nullptr)
3215 {
3216 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3217 }
3218
3219 if (mShaderVersion < 310)
3220 {
3221 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3222 }
3223 else
3224 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003225 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3226 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003227 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003228
Andrei Volykhina5527072017-03-22 16:46:30 +03003229 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3230
Jamie Madill099c0f32013-06-20 11:55:52 -04003231 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003232 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003233
Jamie Madill099c0f32013-06-20 11:55:52 -04003234 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3235 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003236 if (typeQualifier.qualifier == EvqUniform)
3237 {
3238 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3239 }
3240 else if (typeQualifier.qualifier == EvqBuffer)
3241 {
3242 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3243 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003244 }
3245
Jamie Madill1566ef72013-06-20 11:55:54 -04003246 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3247 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003248 if (typeQualifier.qualifier == EvqUniform)
3249 {
3250 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3251 }
3252 else if (typeQualifier.qualifier == EvqBuffer)
3253 {
3254 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3255 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003256 }
3257
Olli Etuaho856c4972016-08-08 11:38:39 +03003258 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003259
Martin Radev2cc85b32016-08-05 16:22:53 +03003260 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3261
Arun Patole7e7e68d2015-05-22 12:02:25 +05303262 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3263 if (!symbolTable.declare(blockNameSymbol))
3264 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003265 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003266 }
3267
Jamie Madill98493dd2013-07-08 14:39:03 -04003268 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303269 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003271 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303272 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003273 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303274 {
jchen10cc2a10e2017-05-03 14:05:12 +08003275 std::string reason("unsupported type - ");
3276 reason += fieldType->getBasicString();
3277 reason += " types are not allowed in interface blocks";
3278 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003279 }
3280
Jamie Madill98493dd2013-07-08 14:39:03 -04003281 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003282 switch (qualifier)
3283 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003284 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003285 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003286 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003287 if (typeQualifier.qualifier == EvqBuffer)
3288 {
3289 error(field->line(), "invalid qualifier on shader storage block member",
3290 getQualifierString(qualifier));
3291 }
3292 break;
3293 case EvqBuffer:
3294 if (typeQualifier.qualifier == EvqUniform)
3295 {
3296 error(field->line(), "invalid qualifier on uniform block member",
3297 getQualifierString(qualifier));
3298 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003299 break;
3300 default:
3301 error(field->line(), "invalid qualifier on interface block member",
3302 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003303 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003304 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003305
Martin Radev70866b82016-07-22 15:27:42 +03003306 if (fieldType->isInvariant())
3307 {
3308 error(field->line(), "invalid qualifier on interface block member", "invariant");
3309 }
3310
Jamie Madilla5efff92013-06-06 11:56:47 -04003311 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003312 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003313 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003314 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003315
Jamie Madill98493dd2013-07-08 14:39:03 -04003316 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003317 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003318 error(field->line(), "invalid layout qualifier: cannot be used here",
3319 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003320 }
3321
Jamie Madill98493dd2013-07-08 14:39:03 -04003322 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003323 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003324 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003325 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003326 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003327 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003328 warning(field->line(),
3329 "extraneous layout qualifier: only has an effect on matrix types",
3330 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003331 }
3332
Jamie Madill98493dd2013-07-08 14:39:03 -04003333 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003334
3335 if (typeQualifier.qualifier == EvqBuffer)
3336 {
3337 // set memory qualifiers
3338 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3339 // qualified with a memory qualifier, it is as if all of its members were declared with
3340 // the same memory qualifier.
3341 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3342 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3343 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3344 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3345 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3346 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3347 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3348 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3349 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3350 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3351 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003352 }
3353
Jamie Madillb98c3a82015-07-23 14:26:04 -04003354 TInterfaceBlock *interfaceBlock =
3355 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3356 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3357 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003358
3359 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003360 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003361
Jamie Madill98493dd2013-07-08 14:39:03 -04003362 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003363 {
3364 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003365 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3366 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003367 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303368 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003369
3370 // set parent pointer of the field variable
3371 fieldType->setInterfaceBlock(interfaceBlock);
3372
Arun Patole7e7e68d2015-05-22 12:02:25 +05303373 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003374 fieldVariable->setQualifier(typeQualifier.qualifier);
3375
Arun Patole7e7e68d2015-05-22 12:02:25 +05303376 if (!symbolTable.declare(fieldVariable))
3377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003378 error(field->line(), "redefinition of an interface block member name",
3379 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003380 }
3381 }
3382 }
3383 else
3384 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003385 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003386
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003387 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303388 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003389 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003390
Arun Patole7e7e68d2015-05-22 12:02:25 +05303391 if (!symbolTable.declare(instanceTypeDef))
3392 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003393 error(instanceLine, "redefinition of an interface block instance name",
3394 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003395 }
3396
Jamie Madillb98c3a82015-07-23 14:26:04 -04003397 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003398 symbolName = instanceTypeDef->getName();
3399 }
3400
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003401 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3402 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003403 TIntermDeclaration *declaration = new TIntermDeclaration();
3404 declaration->appendDeclarator(blockSymbol);
3405 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003406
3407 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003408 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003409}
3410
Olli Etuaho383b7912016-08-05 11:22:59 +03003411void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003412{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003413 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003414
3415 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003416 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303417 if (mStructNestingLevel > 1)
3418 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003419 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003420 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003421}
3422
3423void TParseContext::exitStructDeclaration()
3424{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003425 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003426}
3427
Olli Etuaho8a176262016-08-16 14:23:01 +03003428void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003429{
Jamie Madillacb4b812016-11-07 13:50:29 -05003430 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303431 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003432 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003433 }
3434
Arun Patole7e7e68d2015-05-22 12:02:25 +05303435 if (field.type()->getBasicType() != EbtStruct)
3436 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003437 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003438 }
3439
3440 // We're already inside a structure definition at this point, so add
3441 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303442 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3443 {
Jamie Madill41a49272014-03-18 16:10:13 -04003444 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003445 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3446 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003447 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003448 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003449 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003450 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003451}
3452
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003453//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003454// Parse an array index expression
3455//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003456TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3457 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303458 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003459{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003460 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3461 {
3462 if (baseExpression->getAsSymbolNode())
3463 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303464 error(location, " left of '[' is not of type array, matrix, or vector ",
3465 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003466 }
3467 else
3468 {
3469 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3470 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003471
Olli Etuaho3ec75682017-07-05 17:02:55 +03003472 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003473 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003474
Jamie Madill21c1e452014-12-29 11:33:41 -05003475 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3476
Olli Etuaho36b05142015-11-12 13:10:42 +02003477 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3478 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3479 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3480 // index is a constant expression.
3481 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3482 {
3483 if (baseExpression->isInterfaceBlock())
3484 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003485 error(location,
3486 "array indexes for interface blocks arrays must be constant integral expressions",
3487 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003488 }
3489 else if (baseExpression->getQualifier() == EvqFragmentOut)
3490 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003491 error(location,
3492 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003493 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003494 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3495 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003496 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003497 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003498 }
3499
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003500 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003501 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003502 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3503 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3504 // constant fold expressions that are not constant expressions). The most compatible way to
3505 // handle this case is to report a warning instead of an error and force the index to be in
3506 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003507 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003508 int index = 0;
3509 if (indexConstantUnion->getBasicType() == EbtInt)
3510 {
3511 index = indexConstantUnion->getIConst(0);
3512 }
3513 else if (indexConstantUnion->getBasicType() == EbtUInt)
3514 {
3515 index = static_cast<int>(indexConstantUnion->getUConst(0));
3516 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003517
3518 int safeIndex = -1;
3519
3520 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003521 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003522 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003523 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003524 if (mShaderSpec == SH_WEBGL2_SPEC)
3525 {
3526 // Error has been already generated if index is not const.
3527 if (indexExpression->getQualifier() == EvqConst)
3528 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003529 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003530 }
3531 safeIndex = 0;
3532 }
3533 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3534 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003535 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003536 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003537 "GL_EXT_draw_buffers is disabled",
3538 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003539 safeIndex = 0;
3540 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003541 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003542 // Only do generic out-of-range check if similar error hasn't already been reported.
3543 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003544 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003545 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3546 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003547 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003548 }
3549 }
3550 else if (baseExpression->isMatrix())
3551 {
3552 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003553 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003554 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003555 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003556 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003557 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003558 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3559 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003560 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003561 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003562
3563 ASSERT(safeIndex >= 0);
3564 // Data of constant unions can't be changed, because it may be shared with other
3565 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3566 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003567 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003568 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003569 TConstantUnion *safeConstantUnion = new TConstantUnion();
3570 safeConstantUnion->setIConst(safeIndex);
3571 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003572 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003573 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003574
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003575 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3576 node->setLine(location);
3577 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003578 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003579 else
3580 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003581 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3582 node->setLine(location);
3583 // Indirect indexing can never be constant folded.
3584 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003585 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003586}
3587
Olli Etuaho90892fb2016-07-14 14:44:51 +03003588int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3589 const TSourceLoc &location,
3590 int index,
3591 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003592 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003593{
3594 if (index >= arraySize || index < 0)
3595 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003596 std::stringstream reasonStream;
3597 reasonStream << reason << " '" << index << "'";
3598 std::string token = reasonStream.str();
3599 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003600 if (index < 0)
3601 {
3602 return 0;
3603 }
3604 else
3605 {
3606 return arraySize - 1;
3607 }
3608 }
3609 return index;
3610}
3611
Jamie Madillb98c3a82015-07-23 14:26:04 -04003612TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3613 const TSourceLoc &dotLocation,
3614 const TString &fieldString,
3615 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003616{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003617 if (baseExpression->isArray())
3618 {
3619 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003620 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003621 }
3622
3623 if (baseExpression->isVector())
3624 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003625 TVector<int> fieldOffsets;
3626 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3627 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003628 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003629 fieldOffsets.resize(1);
3630 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003631 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003632 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3633 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003634
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003635 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003636 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003637 else if (baseExpression->getBasicType() == EbtStruct)
3638 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303639 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003640 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003641 {
3642 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003643 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003644 }
3645 else
3646 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003647 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003648 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003649 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003650 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003651 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003652 {
3653 fieldFound = true;
3654 break;
3655 }
3656 }
3657 if (fieldFound)
3658 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003659 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003660 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003661 TIntermBinary *node =
3662 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3663 node->setLine(dotLocation);
3664 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003665 }
3666 else
3667 {
3668 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003669 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003670 }
3671 }
3672 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003674 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303675 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003676 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003677 {
3678 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003679 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003680 }
3681 else
3682 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003683 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003684 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003685 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003686 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003687 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003688 {
3689 fieldFound = true;
3690 break;
3691 }
3692 }
3693 if (fieldFound)
3694 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003695 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003696 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003697 TIntermBinary *node =
3698 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3699 node->setLine(dotLocation);
3700 // Indexing interface blocks can never be constant folded.
3701 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003702 }
3703 else
3704 {
3705 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003706 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003707 }
3708 }
3709 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003710 else
3711 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003712 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003713 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003714 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003716 }
3717 else
3718 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303719 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003720 " field selection requires structure, vector, or interface block on left hand "
3721 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303722 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003724 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003725 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003726}
3727
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3729 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003730{
Martin Radev802abe02016-08-04 17:48:32 +03003731 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003732
3733 if (qualifierType == "shared")
3734 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003735 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003736 {
3737 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3738 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003739 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003740 }
3741 else if (qualifierType == "packed")
3742 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003743 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003744 {
3745 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3746 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003747 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003748 }
3749 else if (qualifierType == "std140")
3750 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003751 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003752 }
3753 else if (qualifierType == "row_major")
3754 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003755 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003756 }
3757 else if (qualifierType == "column_major")
3758 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003759 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003760 }
3761 else if (qualifierType == "location")
3762 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003763 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3764 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003765 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003766 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3767 mShaderType == GL_FRAGMENT_SHADER)
3768 {
3769 qualifier.yuv = true;
3770 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003771 else if (qualifierType == "rgba32f")
3772 {
3773 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3774 qualifier.imageInternalFormat = EiifRGBA32F;
3775 }
3776 else if (qualifierType == "rgba16f")
3777 {
3778 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3779 qualifier.imageInternalFormat = EiifRGBA16F;
3780 }
3781 else if (qualifierType == "r32f")
3782 {
3783 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3784 qualifier.imageInternalFormat = EiifR32F;
3785 }
3786 else if (qualifierType == "rgba8")
3787 {
3788 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3789 qualifier.imageInternalFormat = EiifRGBA8;
3790 }
3791 else if (qualifierType == "rgba8_snorm")
3792 {
3793 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3794 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3795 }
3796 else if (qualifierType == "rgba32i")
3797 {
3798 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3799 qualifier.imageInternalFormat = EiifRGBA32I;
3800 }
3801 else if (qualifierType == "rgba16i")
3802 {
3803 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3804 qualifier.imageInternalFormat = EiifRGBA16I;
3805 }
3806 else if (qualifierType == "rgba8i")
3807 {
3808 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3809 qualifier.imageInternalFormat = EiifRGBA8I;
3810 }
3811 else if (qualifierType == "r32i")
3812 {
3813 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3814 qualifier.imageInternalFormat = EiifR32I;
3815 }
3816 else if (qualifierType == "rgba32ui")
3817 {
3818 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3819 qualifier.imageInternalFormat = EiifRGBA32UI;
3820 }
3821 else if (qualifierType == "rgba16ui")
3822 {
3823 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3824 qualifier.imageInternalFormat = EiifRGBA16UI;
3825 }
3826 else if (qualifierType == "rgba8ui")
3827 {
3828 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3829 qualifier.imageInternalFormat = EiifRGBA8UI;
3830 }
3831 else if (qualifierType == "r32ui")
3832 {
3833 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3834 qualifier.imageInternalFormat = EiifR32UI;
3835 }
3836
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003837 else
3838 {
3839 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003840 }
3841
Jamie Madilla5efff92013-06-06 11:56:47 -04003842 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003843}
3844
Martin Radev802abe02016-08-04 17:48:32 +03003845void TParseContext::parseLocalSize(const TString &qualifierType,
3846 const TSourceLoc &qualifierTypeLine,
3847 int intValue,
3848 const TSourceLoc &intValueLine,
3849 const std::string &intValueString,
3850 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003851 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003852{
Olli Etuaho856c4972016-08-08 11:38:39 +03003853 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003854 if (intValue < 1)
3855 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003856 std::stringstream reasonStream;
3857 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3858 std::string reason = reasonStream.str();
3859 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003860 }
3861 (*localSize)[index] = intValue;
3862}
3863
Olli Etuaho09b04a22016-12-15 13:30:26 +00003864void TParseContext::parseNumViews(int intValue,
3865 const TSourceLoc &intValueLine,
3866 const std::string &intValueString,
3867 int *numViews)
3868{
3869 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3870 // specification.
3871 if (intValue < 1)
3872 {
3873 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3874 }
3875 *numViews = intValue;
3876}
3877
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3879 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003880 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303881 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003882{
Martin Radev802abe02016-08-04 17:48:32 +03003883 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003884
Martin Radev802abe02016-08-04 17:48:32 +03003885 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003886
Martin Radev802abe02016-08-04 17:48:32 +03003887 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003888 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003889 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003890 if (intValue < 0)
3891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 error(intValueLine, "out of range: location must be non-negative",
3893 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003894 }
3895 else
3896 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003897 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003898 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003899 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003900 }
Olli Etuaho43364892017-02-13 16:00:12 +00003901 else if (qualifierType == "binding")
3902 {
3903 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3904 if (intValue < 0)
3905 {
3906 error(intValueLine, "out of range: binding must be non-negative",
3907 intValueString.c_str());
3908 }
3909 else
3910 {
3911 qualifier.binding = intValue;
3912 }
3913 }
jchen104cdac9e2017-05-08 11:01:20 +08003914 else if (qualifierType == "offset")
3915 {
3916 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3917 if (intValue < 0)
3918 {
3919 error(intValueLine, "out of range: offset must be non-negative",
3920 intValueString.c_str());
3921 }
3922 else
3923 {
3924 qualifier.offset = intValue;
3925 }
3926 }
Martin Radev802abe02016-08-04 17:48:32 +03003927 else if (qualifierType == "local_size_x")
3928 {
3929 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3930 &qualifier.localSize);
3931 }
3932 else if (qualifierType == "local_size_y")
3933 {
3934 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3935 &qualifier.localSize);
3936 }
3937 else if (qualifierType == "local_size_z")
3938 {
3939 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3940 &qualifier.localSize);
3941 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003942 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003943 mShaderType == GL_VERTEX_SHADER)
3944 {
3945 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3946 }
Martin Radev802abe02016-08-04 17:48:32 +03003947 else
3948 {
3949 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003950 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003951
Jamie Madilla5efff92013-06-06 11:56:47 -04003952 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003953}
3954
Olli Etuaho613b9592016-09-05 12:05:53 +03003955TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3956{
3957 return new TTypeQualifierBuilder(
3958 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3959 mShaderVersion);
3960}
3961
Olli Etuahocce89652017-06-19 16:04:09 +03003962TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3963 const TSourceLoc &loc)
3964{
3965 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3966 return new TStorageQualifierWrapper(qualifier, loc);
3967}
3968
3969TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3970{
3971 if (getShaderType() == GL_VERTEX_SHADER)
3972 {
3973 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3974 }
3975 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3976}
3977
3978TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3979{
3980 if (declaringFunction())
3981 {
3982 return new TStorageQualifierWrapper(EvqIn, loc);
3983 }
3984 if (getShaderType() == GL_FRAGMENT_SHADER)
3985 {
3986 if (mShaderVersion < 300)
3987 {
3988 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3989 }
3990 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3991 }
3992 if (getShaderType() == GL_VERTEX_SHADER)
3993 {
3994 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3995 {
3996 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3997 }
3998 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3999 }
4000 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4001}
4002
4003TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4004{
4005 if (declaringFunction())
4006 {
4007 return new TStorageQualifierWrapper(EvqOut, loc);
4008 }
4009 if (mShaderVersion < 300)
4010 {
4011 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4012 }
4013 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
4014 {
4015 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
4016 }
4017 if (getShaderType() == GL_VERTEX_SHADER)
4018 {
4019 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4020 }
4021 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4022}
4023
4024TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4025{
4026 if (!declaringFunction())
4027 {
4028 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4029 }
4030 return new TStorageQualifierWrapper(EvqInOut, loc);
4031}
4032
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004034 TLayoutQualifier rightQualifier,
4035 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004036{
Martin Radevc28888b2016-07-22 15:27:42 +03004037 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004038 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004039}
4040
Olli Etuahocce89652017-06-19 16:04:09 +03004041TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4042{
4043 checkIsNotReserved(loc, *identifier);
4044 TType *type = new TType(EbtVoid, EbpUndefined);
4045 return new TField(type, identifier, loc);
4046}
4047
4048TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4049 const TSourceLoc &loc,
4050 TIntermTyped *arraySize,
4051 const TSourceLoc &arraySizeLoc)
4052{
4053 checkIsNotReserved(loc, *identifier);
4054
4055 TType *type = new TType(EbtVoid, EbpUndefined);
4056 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
4057 type->setArraySize(size);
4058
4059 return new TField(type, identifier, loc);
4060}
4061
Olli Etuaho4de340a2016-12-16 09:32:03 +00004062TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4063 const TFieldList *newlyAddedFields,
4064 const TSourceLoc &location)
4065{
4066 for (TField *field : *newlyAddedFields)
4067 {
4068 for (TField *oldField : *processedFields)
4069 {
4070 if (oldField->name() == field->name())
4071 {
4072 error(location, "duplicate field name in structure", field->name().c_str());
4073 }
4074 }
4075 processedFields->push_back(field);
4076 }
4077 return processedFields;
4078}
4079
Martin Radev70866b82016-07-22 15:27:42 +03004080TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4081 const TTypeQualifierBuilder &typeQualifierBuilder,
4082 TPublicType *typeSpecifier,
4083 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004084{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004085 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004086
Martin Radev70866b82016-07-22 15:27:42 +03004087 typeSpecifier->qualifier = typeQualifier.qualifier;
4088 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004089 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004090 typeSpecifier->invariant = typeQualifier.invariant;
4091 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304092 {
Martin Radev70866b82016-07-22 15:27:42 +03004093 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004094 }
Martin Radev70866b82016-07-22 15:27:42 +03004095 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004096}
4097
Jamie Madillb98c3a82015-07-23 14:26:04 -04004098TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
4099 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100{
Martin Radev4a9cd802016-09-01 16:51:51 +03004101 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4102 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004103
Martin Radev4a9cd802016-09-01 16:51:51 +03004104 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105
Martin Radev4a9cd802016-09-01 16:51:51 +03004106 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004107
Arun Patole7e7e68d2015-05-22 12:02:25 +05304108 for (unsigned int i = 0; i < fieldList->size(); ++i)
4109 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 //
4111 // Careful not to replace already known aspects of type, like array-ness
4112 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304113 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03004114 type->setBasicType(typeSpecifier.getBasicType());
4115 type->setPrimarySize(typeSpecifier.getPrimarySize());
4116 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 type->setPrecision(typeSpecifier.precision);
4118 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004119 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004120 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004121 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122
4123 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304124 if (type->isArray())
4125 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004126 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 }
4128 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004129 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004130 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304131 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004132 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 }
4134
Martin Radev4a9cd802016-09-01 16:51:51 +03004135 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 }
4137
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139}
4140
Martin Radev4a9cd802016-09-01 16:51:51 +03004141TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4142 const TSourceLoc &nameLine,
4143 const TString *structName,
4144 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304146 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004147 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004148
Jamie Madill9b820842015-02-12 10:40:10 -05004149 // Store a bool in the struct if we're at global scope, to allow us to
4150 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004151 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004152
Jamie Madill98493dd2013-07-08 14:39:03 -04004153 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004154 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004155 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304156 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4157 if (!symbolTable.declare(userTypeDef))
4158 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004159 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004160 }
4161 }
4162
4163 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004164 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004165 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004167 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004168 switch (qualifier)
4169 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004170 case EvqGlobal:
4171 case EvqTemporary:
4172 break;
4173 default:
4174 error(field.line(), "invalid qualifier on struct member",
4175 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004176 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004177 }
Martin Radev70866b82016-07-22 15:27:42 +03004178 if (field.type()->isInvariant())
4179 {
4180 error(field.line(), "invalid qualifier on struct member", "invariant");
4181 }
jchen104cdac9e2017-05-08 11:01:20 +08004182 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4183 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004184 {
4185 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4186 }
4187
Olli Etuaho43364892017-02-13 16:00:12 +00004188 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4189
4190 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004191
4192 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004193 }
4194
Martin Radev4a9cd802016-09-01 16:51:51 +03004195 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004196 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004197 exitStructDeclaration();
4198
Martin Radev4a9cd802016-09-01 16:51:51 +03004199 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004200}
4201
Jamie Madillb98c3a82015-07-23 14:26:04 -04004202TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004203 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004204 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004205{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004206 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004207 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004208 init->isVector())
4209 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004210 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4211 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004212 return nullptr;
4213 }
4214
Olli Etuahoac5274d2015-02-20 10:19:08 +02004215 if (statementList)
4216 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004217 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004218 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004219 return nullptr;
4220 }
4221 }
4222
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004223 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4224 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004225 return node;
4226}
4227
4228TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4229{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004230 if (mSwitchNestingLevel == 0)
4231 {
4232 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004233 return nullptr;
4234 }
4235 if (condition == nullptr)
4236 {
4237 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004238 return nullptr;
4239 }
4240 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004241 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004242 {
4243 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004244 }
4245 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004246 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4247 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4248 // fold in case labels.
4249 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004250 {
4251 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004252 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004253 TIntermCase *node = new TIntermCase(condition);
4254 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004255 return node;
4256}
4257
4258TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4259{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004260 if (mSwitchNestingLevel == 0)
4261 {
4262 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004263 return nullptr;
4264 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004265 TIntermCase *node = new TIntermCase(nullptr);
4266 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004267 return node;
4268}
4269
Jamie Madillb98c3a82015-07-23 14:26:04 -04004270TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4271 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004272 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004273{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004274 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004275
4276 switch (op)
4277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004278 case EOpLogicalNot:
4279 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4280 child->isVector())
4281 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004282 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004283 return nullptr;
4284 }
4285 break;
4286 case EOpBitwiseNot:
4287 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4288 child->isMatrix() || child->isArray())
4289 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004290 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004291 return nullptr;
4292 }
4293 break;
4294 case EOpPostIncrement:
4295 case EOpPreIncrement:
4296 case EOpPostDecrement:
4297 case EOpPreDecrement:
4298 case EOpNegative:
4299 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004300 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4301 child->getBasicType() == EbtBool || child->isArray() ||
4302 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004303 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004304 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004305 return nullptr;
4306 }
4307 // Operators for built-ins are already type checked against their prototype.
4308 default:
4309 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004310 }
4311
Jiajia Qinbc585152017-06-23 15:42:17 +08004312 if (child->getMemoryQualifier().writeonly)
4313 {
4314 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4315 return nullptr;
4316 }
4317
Olli Etuahof119a262016-08-19 15:54:22 +03004318 TIntermUnary *node = new TIntermUnary(op, child);
4319 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004320
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004321 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004322}
4323
Olli Etuaho09b22472015-02-11 11:47:26 +02004324TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4325{
Olli Etuahocce89652017-06-19 16:04:09 +03004326 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004327 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004328 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004329 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004330 return child;
4331 }
4332 return node;
4333}
4334
Jamie Madillb98c3a82015-07-23 14:26:04 -04004335TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4336 TIntermTyped *child,
4337 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004338{
Olli Etuaho856c4972016-08-08 11:38:39 +03004339 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004340 return addUnaryMath(op, child, loc);
4341}
4342
Jamie Madillb98c3a82015-07-23 14:26:04 -04004343bool TParseContext::binaryOpCommonCheck(TOperator op,
4344 TIntermTyped *left,
4345 TIntermTyped *right,
4346 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004347{
jchen10b4cf5652017-05-05 18:51:17 +08004348 // Check opaque types are not allowed to be operands in expressions other than array indexing
4349 // and structure member selection.
4350 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4351 {
4352 switch (op)
4353 {
4354 case EOpIndexDirect:
4355 case EOpIndexIndirect:
4356 break;
4357 case EOpIndexDirectStruct:
4358 UNREACHABLE();
4359
4360 default:
4361 error(loc, "Invalid operation for variables with an opaque type",
4362 GetOperatorString(op));
4363 return false;
4364 }
4365 }
jchen10cc2a10e2017-05-03 14:05:12 +08004366
Jiajia Qinbc585152017-06-23 15:42:17 +08004367 if (right->getMemoryQualifier().writeonly)
4368 {
4369 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4370 return false;
4371 }
4372
4373 if (left->getMemoryQualifier().writeonly)
4374 {
4375 switch (op)
4376 {
4377 case EOpAssign:
4378 case EOpInitialize:
4379 case EOpIndexDirect:
4380 case EOpIndexIndirect:
4381 case EOpIndexDirectStruct:
4382 case EOpIndexDirectInterfaceBlock:
4383 break;
4384 default:
4385 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4386 return false;
4387 }
4388 }
4389
Olli Etuaho244be012016-08-18 15:26:02 +03004390 if (left->getType().getStruct() || right->getType().getStruct())
4391 {
4392 switch (op)
4393 {
4394 case EOpIndexDirectStruct:
4395 ASSERT(left->getType().getStruct());
4396 break;
4397 case EOpEqual:
4398 case EOpNotEqual:
4399 case EOpAssign:
4400 case EOpInitialize:
4401 if (left->getType() != right->getType())
4402 {
4403 return false;
4404 }
4405 break;
4406 default:
4407 error(loc, "Invalid operation for structs", GetOperatorString(op));
4408 return false;
4409 }
4410 }
4411
Olli Etuaho94050052017-05-08 14:17:44 +03004412 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4413 {
4414 switch (op)
4415 {
4416 case EOpIndexDirectInterfaceBlock:
4417 ASSERT(left->getType().getInterfaceBlock());
4418 break;
4419 default:
4420 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4421 return false;
4422 }
4423 }
4424
Olli Etuahod6b14282015-03-17 14:31:35 +02004425 if (left->isArray() || right->isArray())
4426 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004427 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004428 {
4429 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4430 return false;
4431 }
4432
4433 if (left->isArray() != right->isArray())
4434 {
4435 error(loc, "array / non-array mismatch", GetOperatorString(op));
4436 return false;
4437 }
4438
4439 switch (op)
4440 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004441 case EOpEqual:
4442 case EOpNotEqual:
4443 case EOpAssign:
4444 case EOpInitialize:
4445 break;
4446 default:
4447 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4448 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004449 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004450 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004451 if (left->getArraySize() != right->getArraySize())
4452 {
4453 error(loc, "array size mismatch", GetOperatorString(op));
4454 return false;
4455 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004456 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004457
4458 // Check ops which require integer / ivec parameters
4459 bool isBitShift = false;
4460 switch (op)
4461 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004462 case EOpBitShiftLeft:
4463 case EOpBitShiftRight:
4464 case EOpBitShiftLeftAssign:
4465 case EOpBitShiftRightAssign:
4466 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4467 // check that the basic type is an integer type.
4468 isBitShift = true;
4469 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4470 {
4471 return false;
4472 }
4473 break;
4474 case EOpBitwiseAnd:
4475 case EOpBitwiseXor:
4476 case EOpBitwiseOr:
4477 case EOpBitwiseAndAssign:
4478 case EOpBitwiseXorAssign:
4479 case EOpBitwiseOrAssign:
4480 // It is enough to check the type of only one operand, since later it
4481 // is checked that the operand types match.
4482 if (!IsInteger(left->getBasicType()))
4483 {
4484 return false;
4485 }
4486 break;
4487 default:
4488 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004489 }
4490
4491 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4492 // So the basic type should usually match.
4493 if (!isBitShift && left->getBasicType() != right->getBasicType())
4494 {
4495 return false;
4496 }
4497
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004498 // Check that:
4499 // 1. Type sizes match exactly on ops that require that.
4500 // 2. Restrictions for structs that contain arrays or samplers are respected.
4501 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004502 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004503 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004504 case EOpAssign:
4505 case EOpInitialize:
4506 case EOpEqual:
4507 case EOpNotEqual:
4508 // ESSL 1.00 sections 5.7, 5.8, 5.9
4509 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4510 {
4511 error(loc, "undefined operation for structs containing arrays",
4512 GetOperatorString(op));
4513 return false;
4514 }
4515 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4516 // we interpret the spec so that this extends to structs containing samplers,
4517 // similarly to ESSL 1.00 spec.
4518 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4519 left->getType().isStructureContainingSamplers())
4520 {
4521 error(loc, "undefined operation for structs containing samplers",
4522 GetOperatorString(op));
4523 return false;
4524 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004525
Olli Etuahoe1805592017-01-02 16:41:20 +00004526 if ((left->getNominalSize() != right->getNominalSize()) ||
4527 (left->getSecondarySize() != right->getSecondarySize()))
4528 {
4529 error(loc, "dimension mismatch", GetOperatorString(op));
4530 return false;
4531 }
4532 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004533 case EOpLessThan:
4534 case EOpGreaterThan:
4535 case EOpLessThanEqual:
4536 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004537 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004538 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004539 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004540 return false;
4541 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004542 break;
4543 case EOpAdd:
4544 case EOpSub:
4545 case EOpDiv:
4546 case EOpIMod:
4547 case EOpBitShiftLeft:
4548 case EOpBitShiftRight:
4549 case EOpBitwiseAnd:
4550 case EOpBitwiseXor:
4551 case EOpBitwiseOr:
4552 case EOpAddAssign:
4553 case EOpSubAssign:
4554 case EOpDivAssign:
4555 case EOpIModAssign:
4556 case EOpBitShiftLeftAssign:
4557 case EOpBitShiftRightAssign:
4558 case EOpBitwiseAndAssign:
4559 case EOpBitwiseXorAssign:
4560 case EOpBitwiseOrAssign:
4561 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4562 {
4563 return false;
4564 }
4565
4566 // Are the sizes compatible?
4567 if (left->getNominalSize() != right->getNominalSize() ||
4568 left->getSecondarySize() != right->getSecondarySize())
4569 {
4570 // If the nominal sizes of operands do not match:
4571 // One of them must be a scalar.
4572 if (!left->isScalar() && !right->isScalar())
4573 return false;
4574
4575 // In the case of compound assignment other than multiply-assign,
4576 // the right side needs to be a scalar. Otherwise a vector/matrix
4577 // would be assigned to a scalar. A scalar can't be shifted by a
4578 // vector either.
4579 if (!right->isScalar() &&
4580 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4581 return false;
4582 }
4583 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004584 default:
4585 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004586 }
4587
Olli Etuahod6b14282015-03-17 14:31:35 +02004588 return true;
4589}
4590
Olli Etuaho1dded802016-08-18 18:13:13 +03004591bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4592 const TType &left,
4593 const TType &right)
4594{
4595 switch (op)
4596 {
4597 case EOpMul:
4598 case EOpMulAssign:
4599 return left.getNominalSize() == right.getNominalSize() &&
4600 left.getSecondarySize() == right.getSecondarySize();
4601 case EOpVectorTimesScalar:
4602 return true;
4603 case EOpVectorTimesScalarAssign:
4604 ASSERT(!left.isMatrix() && !right.isMatrix());
4605 return left.isVector() && !right.isVector();
4606 case EOpVectorTimesMatrix:
4607 return left.getNominalSize() == right.getRows();
4608 case EOpVectorTimesMatrixAssign:
4609 ASSERT(!left.isMatrix() && right.isMatrix());
4610 return left.isVector() && left.getNominalSize() == right.getRows() &&
4611 left.getNominalSize() == right.getCols();
4612 case EOpMatrixTimesVector:
4613 return left.getCols() == right.getNominalSize();
4614 case EOpMatrixTimesScalar:
4615 return true;
4616 case EOpMatrixTimesScalarAssign:
4617 ASSERT(left.isMatrix() && !right.isMatrix());
4618 return !right.isVector();
4619 case EOpMatrixTimesMatrix:
4620 return left.getCols() == right.getRows();
4621 case EOpMatrixTimesMatrixAssign:
4622 ASSERT(left.isMatrix() && right.isMatrix());
4623 // We need to check two things:
4624 // 1. The matrix multiplication step is valid.
4625 // 2. The result will have the same number of columns as the lvalue.
4626 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4627
4628 default:
4629 UNREACHABLE();
4630 return false;
4631 }
4632}
4633
Jamie Madillb98c3a82015-07-23 14:26:04 -04004634TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4635 TIntermTyped *left,
4636 TIntermTyped *right,
4637 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004638{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004639 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004640 return nullptr;
4641
Olli Etuahofc1806e2015-03-17 13:03:11 +02004642 switch (op)
4643 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004644 case EOpEqual:
4645 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004646 case EOpLessThan:
4647 case EOpGreaterThan:
4648 case EOpLessThanEqual:
4649 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004650 break;
4651 case EOpLogicalOr:
4652 case EOpLogicalXor:
4653 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004654 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4655 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004656 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004657 {
4658 return nullptr;
4659 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004660 // Basic types matching should have been already checked.
4661 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004662 break;
4663 case EOpAdd:
4664 case EOpSub:
4665 case EOpDiv:
4666 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004667 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4668 !right->getType().getStruct());
4669 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004670 {
4671 return nullptr;
4672 }
4673 break;
4674 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004675 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4676 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004677 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004678 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004679 {
4680 return nullptr;
4681 }
4682 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683 default:
4684 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004685 }
4686
Olli Etuaho1dded802016-08-18 18:13:13 +03004687 if (op == EOpMul)
4688 {
4689 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4690 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4691 {
4692 return nullptr;
4693 }
4694 }
4695
Olli Etuaho3fdec912016-08-18 15:08:06 +03004696 TIntermBinary *node = new TIntermBinary(op, left, right);
4697 node->setLine(loc);
4698
Olli Etuaho3fdec912016-08-18 15:08:06 +03004699 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004700 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004701}
4702
Jamie Madillb98c3a82015-07-23 14:26:04 -04004703TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4704 TIntermTyped *left,
4705 TIntermTyped *right,
4706 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004707{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004708 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004709 if (node == 0)
4710 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004711 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4712 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004713 return left;
4714 }
4715 return node;
4716}
4717
Jamie Madillb98c3a82015-07-23 14:26:04 -04004718TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4719 TIntermTyped *left,
4720 TIntermTyped *right,
4721 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004722{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004723 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004724 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004725 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004726 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4727 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03004728 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03004729 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004730 }
4731 return node;
4732}
4733
Olli Etuaho13389b62016-10-16 11:48:18 +01004734TIntermBinary *TParseContext::createAssign(TOperator op,
4735 TIntermTyped *left,
4736 TIntermTyped *right,
4737 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004738{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004739 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004740 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004741 if (op == EOpMulAssign)
4742 {
4743 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4744 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4745 {
4746 return nullptr;
4747 }
4748 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004749 TIntermBinary *node = new TIntermBinary(op, left, right);
4750 node->setLine(loc);
4751
Olli Etuaho3fdec912016-08-18 15:08:06 +03004752 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004753 }
4754 return nullptr;
4755}
4756
Jamie Madillb98c3a82015-07-23 14:26:04 -04004757TIntermTyped *TParseContext::addAssign(TOperator op,
4758 TIntermTyped *left,
4759 TIntermTyped *right,
4760 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004761{
Olli Etuahocce89652017-06-19 16:04:09 +03004762 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004763 TIntermTyped *node = createAssign(op, left, right, loc);
4764 if (node == nullptr)
4765 {
4766 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004767 return left;
4768 }
4769 return node;
4770}
4771
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004772TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4773 TIntermTyped *right,
4774 const TSourceLoc &loc)
4775{
Corentin Wallez0d959252016-07-12 17:26:32 -04004776 // WebGL2 section 5.26, the following results in an error:
4777 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004778 if (mShaderSpec == SH_WEBGL2_SPEC &&
4779 (left->isArray() || left->getBasicType() == EbtVoid ||
4780 left->getType().isStructureContainingArrays() || right->isArray() ||
4781 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004782 {
4783 error(loc,
4784 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4785 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004786 }
4787
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004788 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4789 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4790 commaNode->getTypePointer()->setQualifier(resultQualifier);
4791 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004792}
4793
Olli Etuaho49300862015-02-20 14:54:49 +02004794TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4795{
4796 switch (op)
4797 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004798 case EOpContinue:
4799 if (mLoopNestingLevel <= 0)
4800 {
4801 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004802 }
4803 break;
4804 case EOpBreak:
4805 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4806 {
4807 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004808 }
4809 break;
4810 case EOpReturn:
4811 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4812 {
4813 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004814 }
4815 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004816 case EOpKill:
4817 if (mShaderType != GL_FRAGMENT_SHADER)
4818 {
4819 error(loc, "discard supported in fragment shaders only", "discard");
4820 }
4821 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004822 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004823 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004824 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004825 }
Olli Etuahocce89652017-06-19 16:04:09 +03004826 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004827}
4828
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004830 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004831 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004832{
Olli Etuahocce89652017-06-19 16:04:09 +03004833 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004834 {
Olli Etuahocce89652017-06-19 16:04:09 +03004835 ASSERT(op == EOpReturn);
4836 mFunctionReturnsValue = true;
4837 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4838 {
4839 error(loc, "void function cannot return a value", "return");
4840 }
4841 else if (*mCurrentFunctionType != expression->getType())
4842 {
4843 error(loc, "function return is not matching type:", "return");
4844 }
Olli Etuaho49300862015-02-20 14:54:49 +02004845 }
Olli Etuahocce89652017-06-19 16:04:09 +03004846 TIntermBranch *node = new TIntermBranch(op, expression);
4847 node->setLine(loc);
4848 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004849}
4850
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004851void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4852{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004853 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004854 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004855 TIntermNode *offset = nullptr;
4856 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004857 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4858 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4859 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004860 {
4861 offset = arguments->back();
4862 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004863 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004864 {
4865 // A bias parameter might follow the offset parameter.
4866 ASSERT(arguments->size() >= 3);
4867 offset = (*arguments)[2];
4868 }
4869 if (offset != nullptr)
4870 {
4871 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4872 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4873 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004874 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004875 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004876 }
4877 else
4878 {
4879 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4880 size_t size = offsetConstantUnion->getType().getObjectSize();
4881 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4882 for (size_t i = 0u; i < size; ++i)
4883 {
4884 int offsetValue = values[i].getIConst();
4885 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4886 {
4887 std::stringstream tokenStream;
4888 tokenStream << offsetValue;
4889 std::string token = tokenStream.str();
4890 error(offset->getLine(), "Texture offset value out of valid range",
4891 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004892 }
4893 }
4894 }
4895 }
4896}
4897
Martin Radev2cc85b32016-08-05 16:22:53 +03004898// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4899void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4900{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004901 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004902 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4903
4904 if (name.compare(0, 5, "image") == 0)
4905 {
4906 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004907 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004908
Olli Etuaho485eefd2017-02-14 17:40:06 +00004909 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004910
4911 if (name.compare(5, 5, "Store") == 0)
4912 {
4913 if (memoryQualifier.readonly)
4914 {
4915 error(imageNode->getLine(),
4916 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004917 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004918 }
4919 }
4920 else if (name.compare(5, 4, "Load") == 0)
4921 {
4922 if (memoryQualifier.writeonly)
4923 {
4924 error(imageNode->getLine(),
4925 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004926 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004927 }
4928 }
4929 }
4930}
4931
4932// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4933void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4934 const TFunction *functionDefinition,
4935 const TIntermAggregate *functionCall)
4936{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004937 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004938
4939 const TIntermSequence &arguments = *functionCall->getSequence();
4940
4941 ASSERT(functionDefinition->getParamCount() == arguments.size());
4942
4943 for (size_t i = 0; i < arguments.size(); ++i)
4944 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004945 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4946 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004947 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4948 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4949
4950 if (IsImage(functionArgumentType.getBasicType()))
4951 {
4952 const TMemoryQualifier &functionArgumentMemoryQualifier =
4953 functionArgumentType.getMemoryQualifier();
4954 const TMemoryQualifier &functionParameterMemoryQualifier =
4955 functionParameterType.getMemoryQualifier();
4956 if (functionArgumentMemoryQualifier.readonly &&
4957 !functionParameterMemoryQualifier.readonly)
4958 {
4959 error(functionCall->getLine(),
4960 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004961 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004962 }
4963
4964 if (functionArgumentMemoryQualifier.writeonly &&
4965 !functionParameterMemoryQualifier.writeonly)
4966 {
4967 error(functionCall->getLine(),
4968 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004969 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004970 }
Martin Radev049edfa2016-11-11 14:35:37 +02004971
4972 if (functionArgumentMemoryQualifier.coherent &&
4973 !functionParameterMemoryQualifier.coherent)
4974 {
4975 error(functionCall->getLine(),
4976 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004977 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004978 }
4979
4980 if (functionArgumentMemoryQualifier.volatileQualifier &&
4981 !functionParameterMemoryQualifier.volatileQualifier)
4982 {
4983 error(functionCall->getLine(),
4984 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004985 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004986 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004987 }
4988 }
4989}
4990
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004991TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004992{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004993 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004994}
4995
4996TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004997 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004998 TIntermNode *thisNode,
4999 const TSourceLoc &loc)
5000{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005001 if (thisNode != nullptr)
5002 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005003 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005004 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005005
5006 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005007 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005008 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005009 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005010 }
5011 else
5012 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005013 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005014 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5015 }
5016}
5017
5018TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5019 TIntermSequence *arguments,
5020 TIntermNode *thisNode,
5021 const TSourceLoc &loc)
5022{
5023 TConstantUnion *unionArray = new TConstantUnion[1];
5024 int arraySize = 0;
5025 TIntermTyped *typedThis = thisNode->getAsTyped();
5026 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5027 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5028 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5029 // So accessing fnCall->getName() below is safe.
5030 if (fnCall->getName() != "length")
5031 {
5032 error(loc, "invalid method", fnCall->getName().c_str());
5033 }
5034 else if (!arguments->empty())
5035 {
5036 error(loc, "method takes no parameters", "length");
5037 }
5038 else if (typedThis == nullptr || !typedThis->isArray())
5039 {
5040 error(loc, "length can only be called on arrays", "length");
5041 }
5042 else
5043 {
5044 arraySize = typedThis->getArraySize();
5045 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005046 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005047 // This code path can be hit with expressions like these:
5048 // (a = b).length()
5049 // (func()).length()
5050 // (int[3](0, 1, 2)).length()
5051 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5052 // expression.
5053 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5054 // spec section 5.9 which allows "An array, vector or matrix expression with the
5055 // length method applied".
5056 error(loc, "length can only be called on array names, not on array expressions",
5057 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005058 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005059 }
5060 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005061 TIntermConstantUnion *node =
5062 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5063 node->setLine(loc);
5064 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005065}
5066
5067TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5068 TIntermSequence *arguments,
5069 const TSourceLoc &loc)
5070{
5071 // First find by unmangled name to check whether the function name has been
5072 // hidden by a variable name or struct typename.
5073 // If a function is found, check for one with a matching argument list.
5074 bool builtIn;
5075 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5076 if (symbol != nullptr && !symbol->isFunction())
5077 {
5078 error(loc, "function name expected", fnCall->getName().c_str());
5079 }
5080 else
5081 {
5082 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5083 mShaderVersion, &builtIn);
5084 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005085 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005086 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5087 }
5088 else
5089 {
5090 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005091 //
5092 // A declared function.
5093 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005094 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005095 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005096 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005097 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005098 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005099 if (builtIn && op != EOpNull)
5100 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005101 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005102 if (fnCandidate->getParamCount() == 1)
5103 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005104 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005105 TIntermNode *unaryParamNode = arguments->front();
5106 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005107 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005108 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005109 }
5110 else
5111 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005112 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005113 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005114 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005115
5116 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005117 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305118
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005119 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305120 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005121 // See if we can constant fold a built-in. Note that this may be possible
5122 // even if it is not const-qualified.
5123 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305124 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005125 else
5126 {
5127 return callNode;
5128 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005129 }
5130 }
5131 else
5132 {
5133 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005134 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005135
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005136 // If builtIn == false, the function is user defined - could be an overloaded
5137 // built-in as well.
5138 // if builtIn == true, it's a builtIn function with no op associated with it.
5139 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005140 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005141 {
Olli Etuahofe486322017-03-21 09:30:54 +00005142 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005143 checkTextureOffsetConst(callNode);
5144 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005145 }
5146 else
5147 {
Olli Etuahofe486322017-03-21 09:30:54 +00005148 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005149 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005150 }
5151
Jiajia Qinbc585152017-06-23 15:42:17 +08005152 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005153
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005154 callNode->setLine(loc);
5155
5156 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005157 }
5158 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005159 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005160
5161 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005162 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005163}
5164
Jamie Madillb98c3a82015-07-23 14:26:04 -04005165TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005166 TIntermTyped *trueExpression,
5167 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005168 const TSourceLoc &loc)
5169{
Olli Etuaho56229f12017-07-10 14:16:33 +03005170 if (!checkIsScalarBool(loc, cond))
5171 {
5172 return falseExpression;
5173 }
Olli Etuaho52901742015-04-15 13:42:45 +03005174
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005175 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005176 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005177 std::stringstream reasonStream;
5178 reasonStream << "mismatching ternary operator operand types '"
5179 << trueExpression->getCompleteString() << " and '"
5180 << falseExpression->getCompleteString() << "'";
5181 std::string reason = reasonStream.str();
5182 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005183 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005184 }
Olli Etuahode318b22016-10-25 16:18:25 +01005185 if (IsOpaqueType(trueExpression->getBasicType()))
5186 {
5187 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005188 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005189 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5190 // Note that structs containing opaque types don't need to be checked as structs are
5191 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005192 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005193 return falseExpression;
5194 }
5195
Jiajia Qinbc585152017-06-23 15:42:17 +08005196 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5197 falseExpression->getMemoryQualifier().writeonly)
5198 {
5199 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5200 return falseExpression;
5201 }
5202
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005203 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005204 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005205 // ESSL 3.00.6 section 5.7:
5206 // Ternary operator support is optional for arrays. No certainty that it works across all
5207 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5208 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005209 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005210 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005211 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005212 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005213 }
Olli Etuaho94050052017-05-08 14:17:44 +03005214 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5215 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005216 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005217 return falseExpression;
5218 }
5219
Corentin Wallez0d959252016-07-12 17:26:32 -04005220 // WebGL2 section 5.26, the following results in an error:
5221 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005222 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005223 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005224 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005225 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005226 }
5227
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005228 // Note that the node resulting from here can be a constant union without being qualified as
5229 // constant.
5230 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5231 node->setLine(loc);
5232
5233 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005234}
Olli Etuaho49300862015-02-20 14:54:49 +02005235
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005236//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005237// Parse an array of strings using yyparse.
5238//
5239// Returns 0 for success.
5240//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005241int PaParseStrings(size_t count,
5242 const char *const string[],
5243 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305244 TParseContext *context)
5245{
Yunchao He4f285442017-04-21 12:15:49 +08005246 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005247 return 1;
5248
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005249 if (glslang_initialize(context))
5250 return 1;
5251
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005252 int error = glslang_scan(count, string, length, context);
5253 if (!error)
5254 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005255
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005256 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005257
alokp@chromium.org6b495712012-06-29 00:06:58 +00005258 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005259}
Jamie Madill45bcc782016-11-07 13:58:48 -05005260
5261} // namespace sh