blob: cb41d00569eaab1e36a38b6e581fe7e56ce65830 [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 Etuahodaaff1c2017-07-05 18:03:26 +03003524 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003525 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003526 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003527 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003528 "GL_EXT_draw_buffers is disabled",
3529 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003530 safeIndex = 0;
3531 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003532 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003533 // Only do generic out-of-range check if similar error hasn't already been reported.
3534 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003535 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003536 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3537 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003538 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003539 }
3540 }
3541 else if (baseExpression->isMatrix())
3542 {
3543 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003544 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003545 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003546 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003547 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003548 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003549 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3550 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003551 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003552 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003553
3554 ASSERT(safeIndex >= 0);
3555 // Data of constant unions can't be changed, because it may be shared with other
3556 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3557 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003558 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003559 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003560 TConstantUnion *safeConstantUnion = new TConstantUnion();
3561 safeConstantUnion->setIConst(safeIndex);
3562 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003563 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003564 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003565
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003566 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3567 node->setLine(location);
3568 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003569 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003570 else
3571 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003572 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3573 node->setLine(location);
3574 // Indirect indexing can never be constant folded.
3575 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003576 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003577}
3578
Olli Etuaho90892fb2016-07-14 14:44:51 +03003579int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3580 const TSourceLoc &location,
3581 int index,
3582 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003583 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003584{
3585 if (index >= arraySize || index < 0)
3586 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003587 std::stringstream reasonStream;
3588 reasonStream << reason << " '" << index << "'";
3589 std::string token = reasonStream.str();
3590 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003591 if (index < 0)
3592 {
3593 return 0;
3594 }
3595 else
3596 {
3597 return arraySize - 1;
3598 }
3599 }
3600 return index;
3601}
3602
Jamie Madillb98c3a82015-07-23 14:26:04 -04003603TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3604 const TSourceLoc &dotLocation,
3605 const TString &fieldString,
3606 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003607{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003608 if (baseExpression->isArray())
3609 {
3610 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003611 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003612 }
3613
3614 if (baseExpression->isVector())
3615 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003616 TVector<int> fieldOffsets;
3617 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3618 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003619 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003620 fieldOffsets.resize(1);
3621 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003622 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003623 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3624 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003625
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003626 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003627 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003628 else if (baseExpression->getBasicType() == EbtStruct)
3629 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303630 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003631 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003632 {
3633 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003634 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003635 }
3636 else
3637 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003638 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003639 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003640 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003641 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003642 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003643 {
3644 fieldFound = true;
3645 break;
3646 }
3647 }
3648 if (fieldFound)
3649 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003650 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003651 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003652 TIntermBinary *node =
3653 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3654 node->setLine(dotLocation);
3655 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003656 }
3657 else
3658 {
3659 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003660 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003661 }
3662 }
3663 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003664 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003665 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303666 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003667 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003668 {
3669 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003670 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003671 }
3672 else
3673 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003674 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003675 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003676 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003677 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003678 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003679 {
3680 fieldFound = true;
3681 break;
3682 }
3683 }
3684 if (fieldFound)
3685 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003686 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003687 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003688 TIntermBinary *node =
3689 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3690 node->setLine(dotLocation);
3691 // Indexing interface blocks can never be constant folded.
3692 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003693 }
3694 else
3695 {
3696 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003697 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003698 }
3699 }
3700 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003701 else
3702 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003703 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003704 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003705 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303706 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003707 }
3708 else
3709 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303710 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003711 " field selection requires structure, vector, or interface block on left hand "
3712 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303713 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003714 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003715 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003716 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003717}
3718
Jamie Madillb98c3a82015-07-23 14:26:04 -04003719TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3720 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003721{
Martin Radev802abe02016-08-04 17:48:32 +03003722 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003723
3724 if (qualifierType == "shared")
3725 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003726 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003727 {
3728 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3729 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003730 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003731 }
3732 else if (qualifierType == "packed")
3733 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003734 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003735 {
3736 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3737 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003738 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003739 }
3740 else if (qualifierType == "std140")
3741 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003742 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003743 }
3744 else if (qualifierType == "row_major")
3745 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003746 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003747 }
3748 else if (qualifierType == "column_major")
3749 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003750 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003751 }
3752 else if (qualifierType == "location")
3753 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003754 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3755 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003756 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003757 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3758 mShaderType == GL_FRAGMENT_SHADER)
3759 {
3760 qualifier.yuv = true;
3761 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003762 else if (qualifierType == "rgba32f")
3763 {
3764 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3765 qualifier.imageInternalFormat = EiifRGBA32F;
3766 }
3767 else if (qualifierType == "rgba16f")
3768 {
3769 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3770 qualifier.imageInternalFormat = EiifRGBA16F;
3771 }
3772 else if (qualifierType == "r32f")
3773 {
3774 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3775 qualifier.imageInternalFormat = EiifR32F;
3776 }
3777 else if (qualifierType == "rgba8")
3778 {
3779 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3780 qualifier.imageInternalFormat = EiifRGBA8;
3781 }
3782 else if (qualifierType == "rgba8_snorm")
3783 {
3784 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3785 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3786 }
3787 else if (qualifierType == "rgba32i")
3788 {
3789 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3790 qualifier.imageInternalFormat = EiifRGBA32I;
3791 }
3792 else if (qualifierType == "rgba16i")
3793 {
3794 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3795 qualifier.imageInternalFormat = EiifRGBA16I;
3796 }
3797 else if (qualifierType == "rgba8i")
3798 {
3799 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3800 qualifier.imageInternalFormat = EiifRGBA8I;
3801 }
3802 else if (qualifierType == "r32i")
3803 {
3804 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3805 qualifier.imageInternalFormat = EiifR32I;
3806 }
3807 else if (qualifierType == "rgba32ui")
3808 {
3809 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3810 qualifier.imageInternalFormat = EiifRGBA32UI;
3811 }
3812 else if (qualifierType == "rgba16ui")
3813 {
3814 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3815 qualifier.imageInternalFormat = EiifRGBA16UI;
3816 }
3817 else if (qualifierType == "rgba8ui")
3818 {
3819 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3820 qualifier.imageInternalFormat = EiifRGBA8UI;
3821 }
3822 else if (qualifierType == "r32ui")
3823 {
3824 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3825 qualifier.imageInternalFormat = EiifR32UI;
3826 }
3827
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003828 else
3829 {
3830 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003831 }
3832
Jamie Madilla5efff92013-06-06 11:56:47 -04003833 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003834}
3835
Martin Radev802abe02016-08-04 17:48:32 +03003836void TParseContext::parseLocalSize(const TString &qualifierType,
3837 const TSourceLoc &qualifierTypeLine,
3838 int intValue,
3839 const TSourceLoc &intValueLine,
3840 const std::string &intValueString,
3841 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003842 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003843{
Olli Etuaho856c4972016-08-08 11:38:39 +03003844 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003845 if (intValue < 1)
3846 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003847 std::stringstream reasonStream;
3848 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3849 std::string reason = reasonStream.str();
3850 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003851 }
3852 (*localSize)[index] = intValue;
3853}
3854
Olli Etuaho09b04a22016-12-15 13:30:26 +00003855void TParseContext::parseNumViews(int intValue,
3856 const TSourceLoc &intValueLine,
3857 const std::string &intValueString,
3858 int *numViews)
3859{
3860 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3861 // specification.
3862 if (intValue < 1)
3863 {
3864 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3865 }
3866 *numViews = intValue;
3867}
3868
Jamie Madillb98c3a82015-07-23 14:26:04 -04003869TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3870 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303872 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003873{
Martin Radev802abe02016-08-04 17:48:32 +03003874 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003875
Martin Radev802abe02016-08-04 17:48:32 +03003876 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003877
Martin Radev802abe02016-08-04 17:48:32 +03003878 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003879 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003880 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003881 if (intValue < 0)
3882 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003883 error(intValueLine, "out of range: location must be non-negative",
3884 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003885 }
3886 else
3887 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003888 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003889 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003890 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003891 }
Olli Etuaho43364892017-02-13 16:00:12 +00003892 else if (qualifierType == "binding")
3893 {
3894 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3895 if (intValue < 0)
3896 {
3897 error(intValueLine, "out of range: binding must be non-negative",
3898 intValueString.c_str());
3899 }
3900 else
3901 {
3902 qualifier.binding = intValue;
3903 }
3904 }
jchen104cdac9e2017-05-08 11:01:20 +08003905 else if (qualifierType == "offset")
3906 {
3907 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3908 if (intValue < 0)
3909 {
3910 error(intValueLine, "out of range: offset must be non-negative",
3911 intValueString.c_str());
3912 }
3913 else
3914 {
3915 qualifier.offset = intValue;
3916 }
3917 }
Martin Radev802abe02016-08-04 17:48:32 +03003918 else if (qualifierType == "local_size_x")
3919 {
3920 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3921 &qualifier.localSize);
3922 }
3923 else if (qualifierType == "local_size_y")
3924 {
3925 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3926 &qualifier.localSize);
3927 }
3928 else if (qualifierType == "local_size_z")
3929 {
3930 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3931 &qualifier.localSize);
3932 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003933 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003934 mShaderType == GL_VERTEX_SHADER)
3935 {
3936 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3937 }
Martin Radev802abe02016-08-04 17:48:32 +03003938 else
3939 {
3940 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003941 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003942
Jamie Madilla5efff92013-06-06 11:56:47 -04003943 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003944}
3945
Olli Etuaho613b9592016-09-05 12:05:53 +03003946TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3947{
3948 return new TTypeQualifierBuilder(
3949 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3950 mShaderVersion);
3951}
3952
Olli Etuahocce89652017-06-19 16:04:09 +03003953TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3954 const TSourceLoc &loc)
3955{
3956 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3957 return new TStorageQualifierWrapper(qualifier, loc);
3958}
3959
3960TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3961{
3962 if (getShaderType() == GL_VERTEX_SHADER)
3963 {
3964 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3965 }
3966 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3967}
3968
3969TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3970{
3971 if (declaringFunction())
3972 {
3973 return new TStorageQualifierWrapper(EvqIn, loc);
3974 }
3975 if (getShaderType() == GL_FRAGMENT_SHADER)
3976 {
3977 if (mShaderVersion < 300)
3978 {
3979 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3980 }
3981 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3982 }
3983 if (getShaderType() == GL_VERTEX_SHADER)
3984 {
3985 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3986 {
3987 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3988 }
3989 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3990 }
3991 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3992}
3993
3994TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3995{
3996 if (declaringFunction())
3997 {
3998 return new TStorageQualifierWrapper(EvqOut, loc);
3999 }
4000 if (mShaderVersion < 300)
4001 {
4002 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4003 }
4004 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
4005 {
4006 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
4007 }
4008 if (getShaderType() == GL_VERTEX_SHADER)
4009 {
4010 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4011 }
4012 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4013}
4014
4015TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4016{
4017 if (!declaringFunction())
4018 {
4019 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4020 }
4021 return new TStorageQualifierWrapper(EvqInOut, loc);
4022}
4023
Jamie Madillb98c3a82015-07-23 14:26:04 -04004024TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004025 TLayoutQualifier rightQualifier,
4026 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004027{
Martin Radevc28888b2016-07-22 15:27:42 +03004028 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004029 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004030}
4031
Olli Etuahocce89652017-06-19 16:04:09 +03004032TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4033{
4034 checkIsNotReserved(loc, *identifier);
4035 TType *type = new TType(EbtVoid, EbpUndefined);
4036 return new TField(type, identifier, loc);
4037}
4038
4039TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4040 const TSourceLoc &loc,
4041 TIntermTyped *arraySize,
4042 const TSourceLoc &arraySizeLoc)
4043{
4044 checkIsNotReserved(loc, *identifier);
4045
4046 TType *type = new TType(EbtVoid, EbpUndefined);
4047 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
4048 type->setArraySize(size);
4049
4050 return new TField(type, identifier, loc);
4051}
4052
Olli Etuaho4de340a2016-12-16 09:32:03 +00004053TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4054 const TFieldList *newlyAddedFields,
4055 const TSourceLoc &location)
4056{
4057 for (TField *field : *newlyAddedFields)
4058 {
4059 for (TField *oldField : *processedFields)
4060 {
4061 if (oldField->name() == field->name())
4062 {
4063 error(location, "duplicate field name in structure", field->name().c_str());
4064 }
4065 }
4066 processedFields->push_back(field);
4067 }
4068 return processedFields;
4069}
4070
Martin Radev70866b82016-07-22 15:27:42 +03004071TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4072 const TTypeQualifierBuilder &typeQualifierBuilder,
4073 TPublicType *typeSpecifier,
4074 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004075{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004076 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004077
Martin Radev70866b82016-07-22 15:27:42 +03004078 typeSpecifier->qualifier = typeQualifier.qualifier;
4079 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004080 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004081 typeSpecifier->invariant = typeQualifier.invariant;
4082 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304083 {
Martin Radev70866b82016-07-22 15:27:42 +03004084 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004085 }
Martin Radev70866b82016-07-22 15:27:42 +03004086 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004087}
4088
Jamie Madillb98c3a82015-07-23 14:26:04 -04004089TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
4090 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004091{
Martin Radev4a9cd802016-09-01 16:51:51 +03004092 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4093 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004094
Martin Radev4a9cd802016-09-01 16:51:51 +03004095 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004096
Martin Radev4a9cd802016-09-01 16:51:51 +03004097 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004098
Arun Patole7e7e68d2015-05-22 12:02:25 +05304099 for (unsigned int i = 0; i < fieldList->size(); ++i)
4100 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 //
4102 // Careful not to replace already known aspects of type, like array-ness
4103 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304104 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03004105 type->setBasicType(typeSpecifier.getBasicType());
4106 type->setPrimarySize(typeSpecifier.getPrimarySize());
4107 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 type->setPrecision(typeSpecifier.precision);
4109 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004110 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004111 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004112 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004113
4114 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304115 if (type->isArray())
4116 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004117 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004118 }
4119 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004120 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004121 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304122 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004123 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004124 }
4125
Martin Radev4a9cd802016-09-01 16:51:51 +03004126 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 }
4128
Jamie Madill98493dd2013-07-08 14:39:03 -04004129 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004130}
4131
Martin Radev4a9cd802016-09-01 16:51:51 +03004132TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4133 const TSourceLoc &nameLine,
4134 const TString *structName,
4135 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304137 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004138 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139
Jamie Madill9b820842015-02-12 10:40:10 -05004140 // Store a bool in the struct if we're at global scope, to allow us to
4141 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004142 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004143
Jamie Madill98493dd2013-07-08 14:39:03 -04004144 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004146 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304147 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4148 if (!symbolTable.declare(userTypeDef))
4149 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004150 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004151 }
4152 }
4153
4154 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004155 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004156 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004157 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004158 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004159 switch (qualifier)
4160 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004161 case EvqGlobal:
4162 case EvqTemporary:
4163 break;
4164 default:
4165 error(field.line(), "invalid qualifier on struct member",
4166 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004167 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004168 }
Martin Radev70866b82016-07-22 15:27:42 +03004169 if (field.type()->isInvariant())
4170 {
4171 error(field.line(), "invalid qualifier on struct member", "invariant");
4172 }
jchen104cdac9e2017-05-08 11:01:20 +08004173 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4174 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004175 {
4176 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4177 }
4178
Olli Etuaho43364892017-02-13 16:00:12 +00004179 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4180
4181 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004182
4183 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004184 }
4185
Martin Radev4a9cd802016-09-01 16:51:51 +03004186 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004187 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004188 exitStructDeclaration();
4189
Martin Radev4a9cd802016-09-01 16:51:51 +03004190 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004191}
4192
Jamie Madillb98c3a82015-07-23 14:26:04 -04004193TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004194 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004195 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004196{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004197 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004198 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004199 init->isVector())
4200 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004201 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4202 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004203 return nullptr;
4204 }
4205
Olli Etuahoac5274d2015-02-20 10:19:08 +02004206 if (statementList)
4207 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004208 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004209 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004210 return nullptr;
4211 }
4212 }
4213
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004214 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4215 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004216 return node;
4217}
4218
4219TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4220{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004221 if (mSwitchNestingLevel == 0)
4222 {
4223 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004224 return nullptr;
4225 }
4226 if (condition == nullptr)
4227 {
4228 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004229 return nullptr;
4230 }
4231 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004232 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004233 {
4234 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004235 }
4236 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004237 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4238 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4239 // fold in case labels.
4240 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004241 {
4242 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004243 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004244 TIntermCase *node = new TIntermCase(condition);
4245 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004246 return node;
4247}
4248
4249TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4250{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004251 if (mSwitchNestingLevel == 0)
4252 {
4253 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004254 return nullptr;
4255 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004256 TIntermCase *node = new TIntermCase(nullptr);
4257 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004258 return node;
4259}
4260
Jamie Madillb98c3a82015-07-23 14:26:04 -04004261TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4262 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004263 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004264{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004265 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004266
4267 switch (op)
4268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004269 case EOpLogicalNot:
4270 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4271 child->isVector())
4272 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004273 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004274 return nullptr;
4275 }
4276 break;
4277 case EOpBitwiseNot:
4278 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4279 child->isMatrix() || child->isArray())
4280 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004281 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004282 return nullptr;
4283 }
4284 break;
4285 case EOpPostIncrement:
4286 case EOpPreIncrement:
4287 case EOpPostDecrement:
4288 case EOpPreDecrement:
4289 case EOpNegative:
4290 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004291 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4292 child->getBasicType() == EbtBool || child->isArray() ||
4293 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004294 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004295 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004296 return nullptr;
4297 }
4298 // Operators for built-ins are already type checked against their prototype.
4299 default:
4300 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004301 }
4302
Jiajia Qinbc585152017-06-23 15:42:17 +08004303 if (child->getMemoryQualifier().writeonly)
4304 {
4305 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4306 return nullptr;
4307 }
4308
Olli Etuahof119a262016-08-19 15:54:22 +03004309 TIntermUnary *node = new TIntermUnary(op, child);
4310 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004311
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004312 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004313}
4314
Olli Etuaho09b22472015-02-11 11:47:26 +02004315TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4316{
Olli Etuahocce89652017-06-19 16:04:09 +03004317 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004318 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004319 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004320 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004321 return child;
4322 }
4323 return node;
4324}
4325
Jamie Madillb98c3a82015-07-23 14:26:04 -04004326TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4327 TIntermTyped *child,
4328 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004329{
Olli Etuaho856c4972016-08-08 11:38:39 +03004330 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004331 return addUnaryMath(op, child, loc);
4332}
4333
Jamie Madillb98c3a82015-07-23 14:26:04 -04004334bool TParseContext::binaryOpCommonCheck(TOperator op,
4335 TIntermTyped *left,
4336 TIntermTyped *right,
4337 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004338{
jchen10b4cf5652017-05-05 18:51:17 +08004339 // Check opaque types are not allowed to be operands in expressions other than array indexing
4340 // and structure member selection.
4341 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4342 {
4343 switch (op)
4344 {
4345 case EOpIndexDirect:
4346 case EOpIndexIndirect:
4347 break;
4348 case EOpIndexDirectStruct:
4349 UNREACHABLE();
4350
4351 default:
4352 error(loc, "Invalid operation for variables with an opaque type",
4353 GetOperatorString(op));
4354 return false;
4355 }
4356 }
jchen10cc2a10e2017-05-03 14:05:12 +08004357
Jiajia Qinbc585152017-06-23 15:42:17 +08004358 if (right->getMemoryQualifier().writeonly)
4359 {
4360 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4361 return false;
4362 }
4363
4364 if (left->getMemoryQualifier().writeonly)
4365 {
4366 switch (op)
4367 {
4368 case EOpAssign:
4369 case EOpInitialize:
4370 case EOpIndexDirect:
4371 case EOpIndexIndirect:
4372 case EOpIndexDirectStruct:
4373 case EOpIndexDirectInterfaceBlock:
4374 break;
4375 default:
4376 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4377 return false;
4378 }
4379 }
4380
Olli Etuaho244be012016-08-18 15:26:02 +03004381 if (left->getType().getStruct() || right->getType().getStruct())
4382 {
4383 switch (op)
4384 {
4385 case EOpIndexDirectStruct:
4386 ASSERT(left->getType().getStruct());
4387 break;
4388 case EOpEqual:
4389 case EOpNotEqual:
4390 case EOpAssign:
4391 case EOpInitialize:
4392 if (left->getType() != right->getType())
4393 {
4394 return false;
4395 }
4396 break;
4397 default:
4398 error(loc, "Invalid operation for structs", GetOperatorString(op));
4399 return false;
4400 }
4401 }
4402
Olli Etuaho94050052017-05-08 14:17:44 +03004403 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4404 {
4405 switch (op)
4406 {
4407 case EOpIndexDirectInterfaceBlock:
4408 ASSERT(left->getType().getInterfaceBlock());
4409 break;
4410 default:
4411 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4412 return false;
4413 }
4414 }
4415
Olli Etuahod6b14282015-03-17 14:31:35 +02004416 if (left->isArray() || right->isArray())
4417 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004418 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004419 {
4420 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4421 return false;
4422 }
4423
4424 if (left->isArray() != right->isArray())
4425 {
4426 error(loc, "array / non-array mismatch", GetOperatorString(op));
4427 return false;
4428 }
4429
4430 switch (op)
4431 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004432 case EOpEqual:
4433 case EOpNotEqual:
4434 case EOpAssign:
4435 case EOpInitialize:
4436 break;
4437 default:
4438 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4439 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004440 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004441 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004442 if (left->getArraySize() != right->getArraySize())
4443 {
4444 error(loc, "array size mismatch", GetOperatorString(op));
4445 return false;
4446 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004447 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004448
4449 // Check ops which require integer / ivec parameters
4450 bool isBitShift = false;
4451 switch (op)
4452 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004453 case EOpBitShiftLeft:
4454 case EOpBitShiftRight:
4455 case EOpBitShiftLeftAssign:
4456 case EOpBitShiftRightAssign:
4457 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4458 // check that the basic type is an integer type.
4459 isBitShift = true;
4460 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4461 {
4462 return false;
4463 }
4464 break;
4465 case EOpBitwiseAnd:
4466 case EOpBitwiseXor:
4467 case EOpBitwiseOr:
4468 case EOpBitwiseAndAssign:
4469 case EOpBitwiseXorAssign:
4470 case EOpBitwiseOrAssign:
4471 // It is enough to check the type of only one operand, since later it
4472 // is checked that the operand types match.
4473 if (!IsInteger(left->getBasicType()))
4474 {
4475 return false;
4476 }
4477 break;
4478 default:
4479 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004480 }
4481
4482 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4483 // So the basic type should usually match.
4484 if (!isBitShift && left->getBasicType() != right->getBasicType())
4485 {
4486 return false;
4487 }
4488
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004489 // Check that:
4490 // 1. Type sizes match exactly on ops that require that.
4491 // 2. Restrictions for structs that contain arrays or samplers are respected.
4492 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004493 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004495 case EOpAssign:
4496 case EOpInitialize:
4497 case EOpEqual:
4498 case EOpNotEqual:
4499 // ESSL 1.00 sections 5.7, 5.8, 5.9
4500 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4501 {
4502 error(loc, "undefined operation for structs containing arrays",
4503 GetOperatorString(op));
4504 return false;
4505 }
4506 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4507 // we interpret the spec so that this extends to structs containing samplers,
4508 // similarly to ESSL 1.00 spec.
4509 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4510 left->getType().isStructureContainingSamplers())
4511 {
4512 error(loc, "undefined operation for structs containing samplers",
4513 GetOperatorString(op));
4514 return false;
4515 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004516
Olli Etuahoe1805592017-01-02 16:41:20 +00004517 if ((left->getNominalSize() != right->getNominalSize()) ||
4518 (left->getSecondarySize() != right->getSecondarySize()))
4519 {
4520 error(loc, "dimension mismatch", GetOperatorString(op));
4521 return false;
4522 }
4523 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004524 case EOpLessThan:
4525 case EOpGreaterThan:
4526 case EOpLessThanEqual:
4527 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004528 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004529 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004530 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004531 return false;
4532 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004533 break;
4534 case EOpAdd:
4535 case EOpSub:
4536 case EOpDiv:
4537 case EOpIMod:
4538 case EOpBitShiftLeft:
4539 case EOpBitShiftRight:
4540 case EOpBitwiseAnd:
4541 case EOpBitwiseXor:
4542 case EOpBitwiseOr:
4543 case EOpAddAssign:
4544 case EOpSubAssign:
4545 case EOpDivAssign:
4546 case EOpIModAssign:
4547 case EOpBitShiftLeftAssign:
4548 case EOpBitShiftRightAssign:
4549 case EOpBitwiseAndAssign:
4550 case EOpBitwiseXorAssign:
4551 case EOpBitwiseOrAssign:
4552 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4553 {
4554 return false;
4555 }
4556
4557 // Are the sizes compatible?
4558 if (left->getNominalSize() != right->getNominalSize() ||
4559 left->getSecondarySize() != right->getSecondarySize())
4560 {
4561 // If the nominal sizes of operands do not match:
4562 // One of them must be a scalar.
4563 if (!left->isScalar() && !right->isScalar())
4564 return false;
4565
4566 // In the case of compound assignment other than multiply-assign,
4567 // the right side needs to be a scalar. Otherwise a vector/matrix
4568 // would be assigned to a scalar. A scalar can't be shifted by a
4569 // vector either.
4570 if (!right->isScalar() &&
4571 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4572 return false;
4573 }
4574 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004575 default:
4576 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004577 }
4578
Olli Etuahod6b14282015-03-17 14:31:35 +02004579 return true;
4580}
4581
Olli Etuaho1dded802016-08-18 18:13:13 +03004582bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4583 const TType &left,
4584 const TType &right)
4585{
4586 switch (op)
4587 {
4588 case EOpMul:
4589 case EOpMulAssign:
4590 return left.getNominalSize() == right.getNominalSize() &&
4591 left.getSecondarySize() == right.getSecondarySize();
4592 case EOpVectorTimesScalar:
4593 return true;
4594 case EOpVectorTimesScalarAssign:
4595 ASSERT(!left.isMatrix() && !right.isMatrix());
4596 return left.isVector() && !right.isVector();
4597 case EOpVectorTimesMatrix:
4598 return left.getNominalSize() == right.getRows();
4599 case EOpVectorTimesMatrixAssign:
4600 ASSERT(!left.isMatrix() && right.isMatrix());
4601 return left.isVector() && left.getNominalSize() == right.getRows() &&
4602 left.getNominalSize() == right.getCols();
4603 case EOpMatrixTimesVector:
4604 return left.getCols() == right.getNominalSize();
4605 case EOpMatrixTimesScalar:
4606 return true;
4607 case EOpMatrixTimesScalarAssign:
4608 ASSERT(left.isMatrix() && !right.isMatrix());
4609 return !right.isVector();
4610 case EOpMatrixTimesMatrix:
4611 return left.getCols() == right.getRows();
4612 case EOpMatrixTimesMatrixAssign:
4613 ASSERT(left.isMatrix() && right.isMatrix());
4614 // We need to check two things:
4615 // 1. The matrix multiplication step is valid.
4616 // 2. The result will have the same number of columns as the lvalue.
4617 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4618
4619 default:
4620 UNREACHABLE();
4621 return false;
4622 }
4623}
4624
Jamie Madillb98c3a82015-07-23 14:26:04 -04004625TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4626 TIntermTyped *left,
4627 TIntermTyped *right,
4628 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004629{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004630 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004631 return nullptr;
4632
Olli Etuahofc1806e2015-03-17 13:03:11 +02004633 switch (op)
4634 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004635 case EOpEqual:
4636 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004637 case EOpLessThan:
4638 case EOpGreaterThan:
4639 case EOpLessThanEqual:
4640 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004641 break;
4642 case EOpLogicalOr:
4643 case EOpLogicalXor:
4644 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004645 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4646 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004647 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004648 {
4649 return nullptr;
4650 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004651 // Basic types matching should have been already checked.
4652 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004653 break;
4654 case EOpAdd:
4655 case EOpSub:
4656 case EOpDiv:
4657 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004658 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4659 !right->getType().getStruct());
4660 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004661 {
4662 return nullptr;
4663 }
4664 break;
4665 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004666 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4667 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004668 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004669 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004670 {
4671 return nullptr;
4672 }
4673 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004674 default:
4675 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004676 }
4677
Olli Etuaho1dded802016-08-18 18:13:13 +03004678 if (op == EOpMul)
4679 {
4680 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4681 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4682 {
4683 return nullptr;
4684 }
4685 }
4686
Olli Etuaho3fdec912016-08-18 15:08:06 +03004687 TIntermBinary *node = new TIntermBinary(op, left, right);
4688 node->setLine(loc);
4689
Olli Etuaho3fdec912016-08-18 15:08:06 +03004690 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004691 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004692}
4693
Jamie Madillb98c3a82015-07-23 14:26:04 -04004694TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4695 TIntermTyped *left,
4696 TIntermTyped *right,
4697 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004698{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004699 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004700 if (node == 0)
4701 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004702 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4703 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004704 return left;
4705 }
4706 return node;
4707}
4708
Jamie Madillb98c3a82015-07-23 14:26:04 -04004709TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4710 TIntermTyped *left,
4711 TIntermTyped *right,
4712 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004713{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004714 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004715 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004716 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004717 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4718 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03004719 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03004720 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004721 }
4722 return node;
4723}
4724
Olli Etuaho13389b62016-10-16 11:48:18 +01004725TIntermBinary *TParseContext::createAssign(TOperator op,
4726 TIntermTyped *left,
4727 TIntermTyped *right,
4728 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004729{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004730 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004731 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004732 if (op == EOpMulAssign)
4733 {
4734 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4735 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4736 {
4737 return nullptr;
4738 }
4739 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004740 TIntermBinary *node = new TIntermBinary(op, left, right);
4741 node->setLine(loc);
4742
Olli Etuaho3fdec912016-08-18 15:08:06 +03004743 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004744 }
4745 return nullptr;
4746}
4747
Jamie Madillb98c3a82015-07-23 14:26:04 -04004748TIntermTyped *TParseContext::addAssign(TOperator op,
4749 TIntermTyped *left,
4750 TIntermTyped *right,
4751 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004752{
Olli Etuahocce89652017-06-19 16:04:09 +03004753 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004754 TIntermTyped *node = createAssign(op, left, right, loc);
4755 if (node == nullptr)
4756 {
4757 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004758 return left;
4759 }
4760 return node;
4761}
4762
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004763TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4764 TIntermTyped *right,
4765 const TSourceLoc &loc)
4766{
Corentin Wallez0d959252016-07-12 17:26:32 -04004767 // WebGL2 section 5.26, the following results in an error:
4768 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004769 if (mShaderSpec == SH_WEBGL2_SPEC &&
4770 (left->isArray() || left->getBasicType() == EbtVoid ||
4771 left->getType().isStructureContainingArrays() || right->isArray() ||
4772 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004773 {
4774 error(loc,
4775 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4776 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004777 }
4778
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004779 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4780 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4781 commaNode->getTypePointer()->setQualifier(resultQualifier);
4782 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004783}
4784
Olli Etuaho49300862015-02-20 14:54:49 +02004785TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4786{
4787 switch (op)
4788 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004789 case EOpContinue:
4790 if (mLoopNestingLevel <= 0)
4791 {
4792 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004793 }
4794 break;
4795 case EOpBreak:
4796 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4797 {
4798 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004799 }
4800 break;
4801 case EOpReturn:
4802 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4803 {
4804 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004805 }
4806 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004807 case EOpKill:
4808 if (mShaderType != GL_FRAGMENT_SHADER)
4809 {
4810 error(loc, "discard supported in fragment shaders only", "discard");
4811 }
4812 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004813 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004814 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004815 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004816 }
Olli Etuahocce89652017-06-19 16:04:09 +03004817 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004818}
4819
Jamie Madillb98c3a82015-07-23 14:26:04 -04004820TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004821 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004822 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004823{
Olli Etuahocce89652017-06-19 16:04:09 +03004824 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004825 {
Olli Etuahocce89652017-06-19 16:04:09 +03004826 ASSERT(op == EOpReturn);
4827 mFunctionReturnsValue = true;
4828 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4829 {
4830 error(loc, "void function cannot return a value", "return");
4831 }
4832 else if (*mCurrentFunctionType != expression->getType())
4833 {
4834 error(loc, "function return is not matching type:", "return");
4835 }
Olli Etuaho49300862015-02-20 14:54:49 +02004836 }
Olli Etuahocce89652017-06-19 16:04:09 +03004837 TIntermBranch *node = new TIntermBranch(op, expression);
4838 node->setLine(loc);
4839 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004840}
4841
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004842void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4843{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004844 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004845 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004846 TIntermNode *offset = nullptr;
4847 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004848 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4849 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4850 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004851 {
4852 offset = arguments->back();
4853 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004854 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004855 {
4856 // A bias parameter might follow the offset parameter.
4857 ASSERT(arguments->size() >= 3);
4858 offset = (*arguments)[2];
4859 }
4860 if (offset != nullptr)
4861 {
4862 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4863 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4864 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004865 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004866 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004867 }
4868 else
4869 {
4870 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4871 size_t size = offsetConstantUnion->getType().getObjectSize();
4872 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4873 for (size_t i = 0u; i < size; ++i)
4874 {
4875 int offsetValue = values[i].getIConst();
4876 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4877 {
4878 std::stringstream tokenStream;
4879 tokenStream << offsetValue;
4880 std::string token = tokenStream.str();
4881 error(offset->getLine(), "Texture offset value out of valid range",
4882 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004883 }
4884 }
4885 }
4886 }
4887}
4888
Martin Radev2cc85b32016-08-05 16:22:53 +03004889// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4890void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4891{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004892 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004893 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4894
4895 if (name.compare(0, 5, "image") == 0)
4896 {
4897 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004898 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004899
Olli Etuaho485eefd2017-02-14 17:40:06 +00004900 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004901
4902 if (name.compare(5, 5, "Store") == 0)
4903 {
4904 if (memoryQualifier.readonly)
4905 {
4906 error(imageNode->getLine(),
4907 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004908 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004909 }
4910 }
4911 else if (name.compare(5, 4, "Load") == 0)
4912 {
4913 if (memoryQualifier.writeonly)
4914 {
4915 error(imageNode->getLine(),
4916 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004917 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004918 }
4919 }
4920 }
4921}
4922
4923// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4924void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4925 const TFunction *functionDefinition,
4926 const TIntermAggregate *functionCall)
4927{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004928 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004929
4930 const TIntermSequence &arguments = *functionCall->getSequence();
4931
4932 ASSERT(functionDefinition->getParamCount() == arguments.size());
4933
4934 for (size_t i = 0; i < arguments.size(); ++i)
4935 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004936 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4937 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004938 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4939 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4940
4941 if (IsImage(functionArgumentType.getBasicType()))
4942 {
4943 const TMemoryQualifier &functionArgumentMemoryQualifier =
4944 functionArgumentType.getMemoryQualifier();
4945 const TMemoryQualifier &functionParameterMemoryQualifier =
4946 functionParameterType.getMemoryQualifier();
4947 if (functionArgumentMemoryQualifier.readonly &&
4948 !functionParameterMemoryQualifier.readonly)
4949 {
4950 error(functionCall->getLine(),
4951 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004952 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004953 }
4954
4955 if (functionArgumentMemoryQualifier.writeonly &&
4956 !functionParameterMemoryQualifier.writeonly)
4957 {
4958 error(functionCall->getLine(),
4959 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004960 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004961 }
Martin Radev049edfa2016-11-11 14:35:37 +02004962
4963 if (functionArgumentMemoryQualifier.coherent &&
4964 !functionParameterMemoryQualifier.coherent)
4965 {
4966 error(functionCall->getLine(),
4967 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004968 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004969 }
4970
4971 if (functionArgumentMemoryQualifier.volatileQualifier &&
4972 !functionParameterMemoryQualifier.volatileQualifier)
4973 {
4974 error(functionCall->getLine(),
4975 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004976 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004977 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004978 }
4979 }
4980}
4981
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004982TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004983{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004984 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004985}
4986
4987TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004988 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004989 TIntermNode *thisNode,
4990 const TSourceLoc &loc)
4991{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004992 if (thisNode != nullptr)
4993 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004994 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004995 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004996
4997 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004998 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004999 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005000 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005001 }
5002 else
5003 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005004 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005005 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5006 }
5007}
5008
5009TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5010 TIntermSequence *arguments,
5011 TIntermNode *thisNode,
5012 const TSourceLoc &loc)
5013{
5014 TConstantUnion *unionArray = new TConstantUnion[1];
5015 int arraySize = 0;
5016 TIntermTyped *typedThis = thisNode->getAsTyped();
5017 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5018 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5019 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5020 // So accessing fnCall->getName() below is safe.
5021 if (fnCall->getName() != "length")
5022 {
5023 error(loc, "invalid method", fnCall->getName().c_str());
5024 }
5025 else if (!arguments->empty())
5026 {
5027 error(loc, "method takes no parameters", "length");
5028 }
5029 else if (typedThis == nullptr || !typedThis->isArray())
5030 {
5031 error(loc, "length can only be called on arrays", "length");
5032 }
5033 else
5034 {
5035 arraySize = typedThis->getArraySize();
5036 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005037 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005038 // This code path can be hit with expressions like these:
5039 // (a = b).length()
5040 // (func()).length()
5041 // (int[3](0, 1, 2)).length()
5042 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5043 // expression.
5044 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5045 // spec section 5.9 which allows "An array, vector or matrix expression with the
5046 // length method applied".
5047 error(loc, "length can only be called on array names, not on array expressions",
5048 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005049 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005050 }
5051 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005052 TIntermConstantUnion *node =
5053 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5054 node->setLine(loc);
5055 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005056}
5057
5058TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5059 TIntermSequence *arguments,
5060 const TSourceLoc &loc)
5061{
5062 // First find by unmangled name to check whether the function name has been
5063 // hidden by a variable name or struct typename.
5064 // If a function is found, check for one with a matching argument list.
5065 bool builtIn;
5066 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5067 if (symbol != nullptr && !symbol->isFunction())
5068 {
5069 error(loc, "function name expected", fnCall->getName().c_str());
5070 }
5071 else
5072 {
5073 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5074 mShaderVersion, &builtIn);
5075 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005076 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005077 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5078 }
5079 else
5080 {
5081 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005082 //
5083 // A declared function.
5084 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005085 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005086 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005087 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005088 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005089 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005090 if (builtIn && op != EOpNull)
5091 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005092 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005093 if (fnCandidate->getParamCount() == 1)
5094 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005095 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005096 TIntermNode *unaryParamNode = arguments->front();
5097 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005098 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005099 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005100 }
5101 else
5102 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005103 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005104 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005105 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005106
5107 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005108 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305109
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005110 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305111 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005112 // See if we can constant fold a built-in. Note that this may be possible
5113 // even if it is not const-qualified.
5114 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305115 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005116 else
5117 {
5118 return callNode;
5119 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005120 }
5121 }
5122 else
5123 {
5124 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005125 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005126
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005127 // If builtIn == false, the function is user defined - could be an overloaded
5128 // built-in as well.
5129 // if builtIn == true, it's a builtIn function with no op associated with it.
5130 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005131 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005132 {
Olli Etuahofe486322017-03-21 09:30:54 +00005133 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005134 checkTextureOffsetConst(callNode);
5135 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005136 }
5137 else
5138 {
Olli Etuahofe486322017-03-21 09:30:54 +00005139 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005140 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005141 }
5142
Jiajia Qinbc585152017-06-23 15:42:17 +08005143 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005144
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005145 callNode->setLine(loc);
5146
5147 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005148 }
5149 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005150 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005151
5152 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005153 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005154}
5155
Jamie Madillb98c3a82015-07-23 14:26:04 -04005156TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005157 TIntermTyped *trueExpression,
5158 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005159 const TSourceLoc &loc)
5160{
Olli Etuaho56229f12017-07-10 14:16:33 +03005161 if (!checkIsScalarBool(loc, cond))
5162 {
5163 return falseExpression;
5164 }
Olli Etuaho52901742015-04-15 13:42:45 +03005165
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005166 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005167 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005168 std::stringstream reasonStream;
5169 reasonStream << "mismatching ternary operator operand types '"
5170 << trueExpression->getCompleteString() << " and '"
5171 << falseExpression->getCompleteString() << "'";
5172 std::string reason = reasonStream.str();
5173 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005174 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005175 }
Olli Etuahode318b22016-10-25 16:18:25 +01005176 if (IsOpaqueType(trueExpression->getBasicType()))
5177 {
5178 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005179 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005180 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5181 // Note that structs containing opaque types don't need to be checked as structs are
5182 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005183 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005184 return falseExpression;
5185 }
5186
Jiajia Qinbc585152017-06-23 15:42:17 +08005187 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5188 falseExpression->getMemoryQualifier().writeonly)
5189 {
5190 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5191 return falseExpression;
5192 }
5193
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005194 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005195 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005196 // ESSL 3.00.6 section 5.7:
5197 // Ternary operator support is optional for arrays. No certainty that it works across all
5198 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5199 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005200 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005201 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005202 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005203 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005204 }
Olli Etuaho94050052017-05-08 14:17:44 +03005205 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5206 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005207 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005208 return falseExpression;
5209 }
5210
Corentin Wallez0d959252016-07-12 17:26:32 -04005211 // WebGL2 section 5.26, the following results in an error:
5212 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005213 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005214 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005215 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005216 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005217 }
5218
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005219 // Note that the node resulting from here can be a constant union without being qualified as
5220 // constant.
5221 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5222 node->setLine(loc);
5223
5224 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005225}
Olli Etuaho49300862015-02-20 14:54:49 +02005226
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005227//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005228// Parse an array of strings using yyparse.
5229//
5230// Returns 0 for success.
5231//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005232int PaParseStrings(size_t count,
5233 const char *const string[],
5234 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305235 TParseContext *context)
5236{
Yunchao He4f285442017-04-21 12:15:49 +08005237 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005238 return 1;
5239
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005240 if (glslang_initialize(context))
5241 return 1;
5242
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005243 int error = glslang_scan(count, string, length, context);
5244 if (!error)
5245 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005246
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005247 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005248
alokp@chromium.org6b495712012-06-29 00:06:58 +00005249 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005250}
Jamie Madill45bcc782016-11-07 13:58:48 -05005251
5252} // namespace sh