blob: ae445fd5566c30401b17bc89444f10fe256423fe [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
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Martin Radev2cc85b32016-08-05 16:22:53 +030098} // namespace
99
jchen104cdac9e2017-05-08 11:01:20 +0800100// This tracks each binding point's current default offset for inheritance of subsequent
101// variables using the same binding, and keeps offsets unique and non overlapping.
102// See GLSL ES 3.1, section 4.4.6.
103class TParseContext::AtomicCounterBindingState
104{
105 public:
106 AtomicCounterBindingState() : mDefaultOffset(0) {}
107 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
108 // newly inserted span.
109 int insertSpan(int start, size_t length)
110 {
111 gl::RangeI newSpan(start, start + static_cast<int>(length));
112 for (const auto &span : mSpans)
113 {
114 if (newSpan.intersects(span))
115 {
116 return -1;
117 }
118 }
119 mSpans.push_back(newSpan);
120 mDefaultOffset = newSpan.high();
121 return start;
122 }
123 // Inserts a new span starting from the default offset.
124 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
125 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
126
127 private:
128 int mDefaultOffset;
129 std::vector<gl::RangeI> mSpans;
130};
131
Jamie Madillacb4b812016-11-07 13:50:29 -0500132TParseContext::TParseContext(TSymbolTable &symt,
133 TExtensionBehavior &ext,
134 sh::GLenum type,
135 ShShaderSpec spec,
136 ShCompileOptions options,
137 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000138 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500139 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300140 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300141 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500142 mShaderType(type),
143 mShaderSpec(spec),
144 mCompileOptions(options),
145 mShaderVersion(100),
146 mTreeRoot(nullptr),
147 mLoopNestingLevel(0),
148 mStructNestingLevel(0),
149 mSwitchNestingLevel(0),
150 mCurrentFunctionType(nullptr),
151 mFunctionReturnsValue(false),
152 mChecksPrecisionErrors(checksPrecErrors),
153 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800154 mDefaultUniformMatrixPacking(EmpColumnMajor),
155 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
156 mDefaultBufferMatrixPacking(EmpColumnMajor),
157 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000158 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500159 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000160 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500161 mShaderVersion,
162 mShaderType,
163 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000164 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500165 mScanner(nullptr),
166 mUsesFragData(false),
167 mUsesFragColor(false),
168 mUsesSecondaryOutputs(false),
169 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
170 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000171 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500172 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000173 mNumViews(-1),
174 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000175 mMaxImageUnits(resources.MaxImageUnits),
176 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000177 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800178 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800179 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800180 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Jamie Madillacb4b812016-11-07 13:50:29 -0500181 mDeclaringFunction(false)
182{
183 mComputeShaderLocalSize.fill(-1);
184}
185
jchen104cdac9e2017-05-08 11:01:20 +0800186TParseContext::~TParseContext()
187{
188}
189
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300190bool TParseContext::parseVectorFields(const TSourceLoc &line,
191 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400192 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300193 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300195 ASSERT(fieldOffsets);
196 size_t fieldCount = compString.size();
197 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000199 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000200 return false;
201 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300202 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203
Jamie Madillb98c3a82015-07-23 14:26:04 -0400204 enum
205 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000206 exyzw,
207 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000208 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000209 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300211 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530212 {
213 switch (compString[i])
214 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400215 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300216 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400217 fieldSet[i] = exyzw;
218 break;
219 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300220 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 fieldSet[i] = ergba;
222 break;
223 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400225 fieldSet[i] = estpq;
226 break;
227 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300228 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 fieldSet[i] = exyzw;
230 break;
231 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300232 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 fieldSet[i] = ergba;
234 break;
235 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300236 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400237 fieldSet[i] = estpq;
238 break;
239 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300240 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400241 fieldSet[i] = exyzw;
242 break;
243 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300244 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400245 fieldSet[i] = ergba;
246 break;
247 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300248 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400249 fieldSet[i] = estpq;
250 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530251
Jamie Madillb98c3a82015-07-23 14:26:04 -0400252 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300253 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400254 fieldSet[i] = exyzw;
255 break;
256 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300257 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400258 fieldSet[i] = ergba;
259 break;
260 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 fieldSet[i] = estpq;
263 break;
264 default:
265 error(line, "illegal vector field selection", compString.c_str());
266 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000267 }
268 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300270 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530271 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530273 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000275 return false;
276 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
Arun Patole7e7e68d2015-05-22 12:02:25 +0530278 if (i > 0)
279 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530281 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400282 error(line, "illegal - vector component fields not from the same set",
283 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000284 return false;
285 }
286 }
287 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000289 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290}
291
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292///////////////////////////////////////////////////////////////////////
293//
294// Errors
295//
296////////////////////////////////////////////////////////////////////////
297
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298//
299// Used by flex/bison to output all syntax and parsing errors.
300//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000301void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000303 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304}
305
Olli Etuaho4de340a2016-12-16 09:32:03 +0000306void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000308 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000309}
310
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200311void TParseContext::outOfRangeError(bool isError,
312 const TSourceLoc &loc,
313 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000314 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200315{
316 if (isError)
317 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000318 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200319 }
320 else
321 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000322 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200323 }
324}
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326//
327// Same error message for all places assignments don't work.
328//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000331 std::stringstream reasonStream;
332 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
333 std::string reason = reasonStream.str();
334 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335}
336
337//
338// Same error message for all places unary operations don't work.
339//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530340void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000342 std::stringstream reasonStream;
343 reasonStream << "wrong operand type - no operation '" << op
344 << "' exists that takes an operand of type " << operand
345 << " (or there is no acceptable conversion)";
346 std::string reason = reasonStream.str();
347 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348}
349
350//
351// Same error message for all binary operations don't work.
352//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400353void TParseContext::binaryOpError(const TSourceLoc &line,
354 const char *op,
355 TString left,
356 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000358 std::stringstream reasonStream;
359 reasonStream << "wrong operand types - no operation '" << op
360 << "' exists that takes a left-hand operand of type '" << left
361 << "' and a right operand of type '" << right
362 << "' (or there is no acceptable conversion)";
363 std::string reason = reasonStream.str();
364 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365}
366
Olli Etuaho856c4972016-08-08 11:38:39 +0300367void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
368 TPrecision precision,
369 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530370{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400371 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300372 return;
Martin Radev70866b82016-07-22 15:27:42 +0300373
374 if (precision != EbpUndefined && !SupportsPrecision(type))
375 {
376 error(line, "illegal type for precision qualifier", getBasicString(type));
377 }
378
Olli Etuaho183d7e22015-11-20 15:59:09 +0200379 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530380 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200381 switch (type)
382 {
383 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400384 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300385 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200386 case EbtInt:
387 case EbtUInt:
388 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400389 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300390 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200391 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800392 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200393 {
jchen10cc2a10e2017-05-03 14:05:12 +0800394 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300395 return;
396 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200397 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000398 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000399}
400
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401// Both test and if necessary, spit out an error, to see if the node is really
402// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300403bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500405 TIntermSymbol *symNode = node->getAsSymbolNode();
406 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100407 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
408
409 if (swizzleNode)
410 {
411 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
412 if (ok && swizzleNode->hasDuplicateOffsets())
413 {
414 error(line, " l-value of swizzle cannot have duplicate components", op);
415 return false;
416 }
417 return ok;
418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
Arun Patole7e7e68d2015-05-22 12:02:25 +0530420 if (binaryNode)
421 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400422 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530423 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400424 case EOpIndexDirect:
425 case EOpIndexIndirect:
426 case EOpIndexDirectStruct:
427 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300428 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400429 default:
430 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000432 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300433 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
jchen10cc2a10e2017-05-03 14:05:12 +0800436 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530437 switch (node->getQualifier())
438 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400439 case EvqConst:
440 message = "can't modify a const";
441 break;
442 case EvqConstReadOnly:
443 message = "can't modify a const";
444 break;
445 case EvqAttribute:
446 message = "can't modify an attribute";
447 break;
448 case EvqFragmentIn:
449 message = "can't modify an input";
450 break;
451 case EvqVertexIn:
452 message = "can't modify an input";
453 break;
454 case EvqUniform:
455 message = "can't modify a uniform";
456 break;
457 case EvqVaryingIn:
458 message = "can't modify a varying";
459 break;
460 case EvqFragCoord:
461 message = "can't modify gl_FragCoord";
462 break;
463 case EvqFrontFacing:
464 message = "can't modify gl_FrontFacing";
465 break;
466 case EvqPointCoord:
467 message = "can't modify gl_PointCoord";
468 break;
Martin Radevb0883602016-08-04 17:48:58 +0300469 case EvqNumWorkGroups:
470 message = "can't modify gl_NumWorkGroups";
471 break;
472 case EvqWorkGroupSize:
473 message = "can't modify gl_WorkGroupSize";
474 break;
475 case EvqWorkGroupID:
476 message = "can't modify gl_WorkGroupID";
477 break;
478 case EvqLocalInvocationID:
479 message = "can't modify gl_LocalInvocationID";
480 break;
481 case EvqGlobalInvocationID:
482 message = "can't modify gl_GlobalInvocationID";
483 break;
484 case EvqLocalInvocationIndex:
485 message = "can't modify gl_LocalInvocationIndex";
486 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300487 case EvqViewIDOVR:
488 message = "can't modify gl_ViewID_OVR";
489 break;
Martin Radev802abe02016-08-04 17:48:32 +0300490 case EvqComputeIn:
491 message = "can't modify work group size variable";
492 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400493 default:
494 //
495 // Type that can't be written to?
496 //
497 if (node->getBasicType() == EbtVoid)
498 {
499 message = "can't modify void";
500 }
jchen10cc2a10e2017-05-03 14:05:12 +0800501 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400502 {
jchen10cc2a10e2017-05-03 14:05:12 +0800503 message = "can't modify a variable with type ";
504 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300505 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800506 else if (node->getMemoryQualifier().readonly)
507 {
508 message = "can't modify a readonly variable";
509 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
jchen10cc2a10e2017-05-03 14:05:12 +0800512 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530513 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000514 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515
Olli Etuaho8a176262016-08-16 14:23:01 +0300516 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 //
520 // Everything else is okay, no error.
521 //
jchen10cc2a10e2017-05-03 14:05:12 +0800522 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300523 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 //
526 // If we get here, we have an error and a message.
527 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530528 if (symNode)
529 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000530 const char *symbol = symNode->getSymbol().c_str();
531 std::stringstream reasonStream;
532 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
533 std::string reason = reasonStream.str();
534 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530536 else
537 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000538 std::stringstream reasonStream;
539 reasonStream << "l-value required (" << message << ")";
540 std::string reason = reasonStream.str();
541 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000542 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000543
Olli Etuaho8a176262016-08-16 14:23:01 +0300544 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545}
546
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547// Both test, and if necessary spit out an error, to see if the node is really
548// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300549void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550{
Olli Etuaho383b7912016-08-05 11:22:59 +0300551 if (node->getQualifier() != EvqConst)
552 {
553 error(node->getLine(), "constant expression required", "");
554 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555}
556
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557// Both test, and if necessary spit out an error, to see if the node is really
558// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300559void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560{
Olli Etuaho383b7912016-08-05 11:22:59 +0300561 if (!node->isScalarInt())
562 {
563 error(node->getLine(), "integer expression required", token);
564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565}
566
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567// Both test, and if necessary spit out an error, to see if we are currently
568// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800569bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570{
Olli Etuaho856c4972016-08-08 11:38:39 +0300571 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300572 {
573 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800574 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300575 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800576 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577}
578
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300579// ESSL 3.00.5 sections 3.8 and 3.9.
580// If it starts "gl_" or contains two consecutive underscores, it's reserved.
581// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300582bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530584 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300585 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300587 error(line, reservedErrMsg, "gl_");
588 return false;
589 }
590 if (sh::IsWebGLBasedSpec(mShaderSpec))
591 {
592 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530593 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300594 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300597 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300599 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300600 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 }
602 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300603 if (identifier.find("__") != TString::npos)
604 {
605 error(line,
606 "identifiers containing two consecutive underscores (__) are reserved as "
607 "possible future keywords",
608 identifier.c_str());
609 return false;
610 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300611 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612}
613
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300614// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300615bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800616 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300617 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800619 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530620 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200621 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300622 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200624
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300625 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530626 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300627 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200628 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300629 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200630 {
jchen10cc2a10e2017-05-03 14:05:12 +0800631 std::string reason("cannot convert a variable with type ");
632 reason += getBasicString(argTyped->getBasicType());
633 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300634 return false;
635 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800636 else if (argTyped->getMemoryQualifier().writeonly)
637 {
638 error(line, "cannot convert a variable with writeonly", "constructor");
639 return false;
640 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200641 if (argTyped->getBasicType() == EbtVoid)
642 {
643 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300644 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200645 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
Olli Etuaho856c4972016-08-08 11:38:39 +0300648 if (type.isArray())
649 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300650 // The size of an unsized constructor should already have been determined.
651 ASSERT(!type.isUnsizedArray());
652 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
653 {
654 error(line, "array constructor needs one argument per array element", "constructor");
655 return false;
656 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300657 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
658 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800659 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300660 {
661 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500662 if (argType.isArray())
663 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300664 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500665 return false;
666 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300667 if (!argType.sameElementType(type))
668 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000669 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300670 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300671 }
672 }
673 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300674 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300675 {
676 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300677 if (fields.size() != arguments->size())
678 {
679 error(line,
680 "Number of constructor parameters does not match the number of structure fields",
681 "constructor");
682 return false;
683 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300684
685 for (size_t i = 0; i < fields.size(); i++)
686 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800687 if (i >= arguments->size() ||
688 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300689 {
690 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000691 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300692 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300693 }
694 }
695 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300696 else
697 {
698 // We're constructing a scalar, vector, or matrix.
699
700 // Note: It's okay to have too many components available, but not okay to have unused
701 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
702 // there is an extra argument, so 'overFull' will become true.
703
704 size_t size = 0;
705 bool full = false;
706 bool overFull = false;
707 bool matrixArg = false;
708 for (TIntermNode *arg : *arguments)
709 {
710 const TIntermTyped *argTyped = arg->getAsTyped();
711 ASSERT(argTyped != nullptr);
712
Olli Etuaho487b63a2017-05-23 15:55:09 +0300713 if (argTyped->getBasicType() == EbtStruct)
714 {
715 error(line, "a struct cannot be used as a constructor argument for this type",
716 "constructor");
717 return false;
718 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300719 if (argTyped->getType().isArray())
720 {
721 error(line, "constructing from a non-dereferenced array", "constructor");
722 return false;
723 }
724 if (argTyped->getType().isMatrix())
725 {
726 matrixArg = true;
727 }
728
729 size += argTyped->getType().getObjectSize();
730 if (full)
731 {
732 overFull = true;
733 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300734 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300735 {
736 full = true;
737 }
738 }
739
740 if (type.isMatrix() && matrixArg)
741 {
742 if (arguments->size() != 1)
743 {
744 error(line, "constructing matrix from matrix can only take one argument",
745 "constructor");
746 return false;
747 }
748 }
749 else
750 {
751 if (size != 1 && size < type.getObjectSize())
752 {
753 error(line, "not enough data provided for construction", "constructor");
754 return false;
755 }
756 if (overFull)
757 {
758 error(line, "too many arguments", "constructor");
759 return false;
760 }
761 }
762 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300763
Olli Etuaho8a176262016-08-16 14:23:01 +0300764 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765}
766
Jamie Madillb98c3a82015-07-23 14:26:04 -0400767// This function checks to see if a void variable has been declared and raise an error message for
768// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769//
770// returns true in case of an error
771//
Olli Etuaho856c4972016-08-08 11:38:39 +0300772bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400773 const TString &identifier,
774 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300776 if (type == EbtVoid)
777 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000778 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300779 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781
Olli Etuaho8a176262016-08-16 14:23:01 +0300782 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
Jamie Madillb98c3a82015-07-23 14:26:04 -0400785// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300786// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300787bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300789 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530790 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000791 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300792 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530793 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300794 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795}
796
Jamie Madillb98c3a82015-07-23 14:26:04 -0400797// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300798// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300799void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800{
Martin Radev4a9cd802016-09-01 16:51:51 +0300801 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530802 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000803 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805}
806
jchen10cc2a10e2017-05-03 14:05:12 +0800807bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
808 const TTypeSpecifierNonArray &pType,
809 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530811 if (pType.type == EbtStruct)
812 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300813 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530814 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000815 std::stringstream reasonStream;
816 reasonStream << reason << " (structure contains a sampler)";
817 std::string reasonStr = reasonStream.str();
818 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300819 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 }
jchen10cc2a10e2017-05-03 14:05:12 +0800821 // only samplers need to be checked from structs, since other opaque types can't be struct
822 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300823 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530824 }
jchen10cc2a10e2017-05-03 14:05:12 +0800825 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530826 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000827 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300828 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
Olli Etuaho856c4972016-08-08 11:38:39 +0300834void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
835 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400836{
837 if (pType.layoutQualifier.location != -1)
838 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400839 error(line, "location must only be specified for a single input or output variable",
840 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400841 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400842}
843
Olli Etuaho856c4972016-08-08 11:38:39 +0300844void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
845 const TLayoutQualifier &layoutQualifier)
846{
847 if (layoutQualifier.location != -1)
848 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000849 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
850 if (mShaderVersion >= 310)
851 {
852 errorMsg =
853 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
854 }
855 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300856 }
857}
858
Martin Radev2cc85b32016-08-05 16:22:53 +0300859void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
860 TQualifier qualifier,
861 const TType &type)
862{
Martin Radev2cc85b32016-08-05 16:22:53 +0300863 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800864 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530865 {
jchen10cc2a10e2017-05-03 14:05:12 +0800866 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000867 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300871unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000874
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200875 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
876 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
877 // fold as array size.
878 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000879 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000880 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300881 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000882 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
Olli Etuaho856c4972016-08-08 11:38:39 +0300884 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400885
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 if (constant->getBasicType() == EbtUInt)
887 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000889 }
890 else
891 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000895 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400896 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000898 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400899
Olli Etuaho856c4972016-08-08 11:38:39 +0300900 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400901 }
902
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400904 {
905 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400907 }
908
909 // The size of arrays is restricted here to prevent issues further down the
910 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
911 // 4096 registers so this should be reasonable even for aggressively optimizable code.
912 const unsigned int sizeLimit = 65536;
913
Olli Etuaho856c4972016-08-08 11:38:39 +0300914 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400915 {
916 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300917 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300919
920 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921}
922
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300924bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
925 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926{
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 if ((elementQualifier.qualifier == EvqAttribute) ||
928 (elementQualifier.qualifier == EvqVertexIn) ||
929 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400931 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300932 TType(elementQualifier).getQualifierString());
933 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000934 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937}
938
Olli Etuaho8a176262016-08-16 14:23:01 +0300939// See if this element type can be formed into an array.
940bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000942 //
943 // Can the type be an array?
944 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300945 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400946 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300947 error(line, "cannot declare arrays of arrays",
948 TType(elementType).getCompleteString().c_str());
949 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000950 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300951 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
952 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
953 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300954 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300955 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300956 {
957 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300958 TType(elementType).getCompleteString().c_str());
959 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300960 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
Olli Etuaho8a176262016-08-16 14:23:01 +0300962 return true;
963}
964
965// Check if this qualified element type can be formed into an array.
966bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
967 const TPublicType &elementType)
968{
969 if (checkIsValidTypeForArray(indexLocation, elementType))
970 {
971 return checkIsValidQualifierForArray(indexLocation, elementType);
972 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300977void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
978 const TString &identifier,
979 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980{
Olli Etuaho3739d232015-04-08 12:23:44 +0300981 ASSERT(type != nullptr);
982 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000983 {
984 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300985 type->qualifier = EvqTemporary;
986
987 // Generate informative error messages for ESSL1.
988 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400989 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000990 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530991 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400992 "structures containing arrays may not be declared constant since they cannot be "
993 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530994 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000995 }
996 else
997 {
998 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
999 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001000 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001001 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001002 if (type->isUnsizedArray())
1003 {
1004 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001005 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001006}
1007
Olli Etuaho2935c582015-04-08 14:32:06 +03001008// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009// and update the symbol table.
1010//
Olli Etuaho2935c582015-04-08 14:32:06 +03001011// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013bool TParseContext::declareVariable(const TSourceLoc &line,
1014 const TString &identifier,
1015 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001016 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017{
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001019
Olli Etuaho43364892017-02-13 16:00:12 +00001020 checkBindingIsValid(line, type);
1021
Olli Etuaho856c4972016-08-08 11:38:39 +03001022 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Olli Etuaho2935c582015-04-08 14:32:06 +03001024 // gl_LastFragData may be redeclared with a new precision qualifier
1025 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1026 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001027 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1028 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001029 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001030 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001031 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001032 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 }
1035 }
1036 else
1037 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001038 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1039 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001040 return false;
1041 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001042 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043
Olli Etuaho8a176262016-08-16 14:23:01 +03001044 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001045 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046
Olli Etuaho0f684632017-07-13 12:42:15 +03001047 (*variable) = symbolTable.declareVariable(&identifier, type);
1048 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001049 {
1050 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001051 return false;
1052 }
1053
Olli Etuaho8a176262016-08-16 14:23:01 +03001054 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001055 return false;
1056
1057 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058}
1059
Martin Radev70866b82016-07-22 15:27:42 +03001060void TParseContext::checkIsParameterQualifierValid(
1061 const TSourceLoc &line,
1062 const TTypeQualifierBuilder &typeQualifierBuilder,
1063 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301064{
Olli Etuahocce89652017-06-19 16:04:09 +03001065 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001066 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001067
1068 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301069 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001070 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1071 }
1072
1073 if (!IsImage(type->getBasicType()))
1074 {
Olli Etuaho43364892017-02-13 16:00:12 +00001075 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001076 }
1077 else
1078 {
1079 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001080 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
Martin Radev70866b82016-07-22 15:27:42 +03001082 type->setQualifier(typeQualifier.qualifier);
1083
1084 if (typeQualifier.precision != EbpUndefined)
1085 {
1086 type->setPrecision(typeQualifier.precision);
1087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
Olli Etuaho856c4972016-08-08 11:38:39 +03001090bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001091{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001092 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001093 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301094 if (iter == extBehavior.end())
1095 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001096 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001098 }
zmo@google.comf5450912011-09-09 01:37:19 +00001099 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301100 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1101 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001102 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1103 // associated with more than one extension.
1104 if (extension == "GL_OVR_multiview")
1105 {
1106 return checkCanUseExtension(line, "GL_OVR_multiview2");
1107 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001108 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001109 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001110 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301111 if (iter->second == EBhWarn)
1112 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001113 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001114 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001115 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho8a176262016-08-16 14:23:01 +03001117 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118}
1119
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001120// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1121// compile-time or link-time errors are the same whether or not the declaration is empty".
1122// This function implements all the checks that are done on qualifiers regardless of if the
1123// declaration is empty.
1124void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1125 const sh::TLayoutQualifier &layoutQualifier,
1126 const TSourceLoc &location)
1127{
1128 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1129 {
1130 error(location, "Shared memory declarations cannot have layout specified", "layout");
1131 }
1132
1133 if (layoutQualifier.matrixPacking != EmpUnspecified)
1134 {
1135 error(location, "layout qualifier only valid for interface blocks",
1136 getMatrixPackingString(layoutQualifier.matrixPacking));
1137 return;
1138 }
1139
1140 if (layoutQualifier.blockStorage != EbsUnspecified)
1141 {
1142 error(location, "layout qualifier only valid for interface blocks",
1143 getBlockStorageString(layoutQualifier.blockStorage));
1144 return;
1145 }
1146
1147 if (qualifier == EvqFragmentOut)
1148 {
1149 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1150 {
1151 error(location, "invalid layout qualifier combination", "yuv");
1152 return;
1153 }
1154 }
1155 else
1156 {
1157 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1158 }
1159
Olli Etuaho95468d12017-05-04 11:14:34 +03001160 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1161 // parsing steps. So it needs to be checked here.
1162 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1163 {
1164 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1165 }
1166
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001167 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1168 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1169 {
1170 canHaveLocation = true;
1171 // We're not checking whether the uniform location is in range here since that depends on
1172 // the type of the variable.
1173 // The type can only be fully determined for non-empty declarations.
1174 }
1175 if (!canHaveLocation)
1176 {
1177 checkLocationIsNotSpecified(location, layoutQualifier);
1178 }
1179}
1180
jchen104cdac9e2017-05-08 11:01:20 +08001181void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1182 const TSourceLoc &location)
1183{
1184 if (publicType.precision != EbpHigh)
1185 {
1186 error(location, "Can only be highp", "atomic counter");
1187 }
1188 // dEQP enforces compile error if location is specified. See uniform_location.test.
1189 if (publicType.layoutQualifier.location != -1)
1190 {
1191 error(location, "location must not be set for atomic_uint", "layout");
1192 }
1193 if (publicType.layoutQualifier.binding == -1)
1194 {
1195 error(location, "no binding specified", "atomic counter");
1196 }
1197}
1198
Martin Radevb8b01222016-11-20 23:25:53 +02001199void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1200 const TSourceLoc &location)
1201{
1202 if (publicType.isUnsizedArray())
1203 {
1204 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1205 // error. It is assumed that this applies to empty declarations as well.
1206 error(location, "empty array declaration needs to specify a size", "");
1207 }
Martin Radevb8b01222016-11-20 23:25:53 +02001208}
1209
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001210// These checks are done for all declarations that are non-empty. They're done for non-empty
1211// declarations starting a declarator list, and declarators that follow an empty declaration.
1212void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1213 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001214{
Olli Etuahofa33d582015-04-09 14:33:12 +03001215 switch (publicType.qualifier)
1216 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001217 case EvqVaryingIn:
1218 case EvqVaryingOut:
1219 case EvqAttribute:
1220 case EvqVertexIn:
1221 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001222 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001223 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001224 {
1225 error(identifierLocation, "cannot be used with a structure",
1226 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001227 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001228 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001229 break;
1230 case EvqBuffer:
1231 if (publicType.getBasicType() != EbtInterfaceBlock)
1232 {
1233 error(identifierLocation,
1234 "cannot declare buffer variables at global scope(outside a block)",
1235 getQualifierString(publicType.qualifier));
1236 return;
1237 }
1238 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001239 default:
1240 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001241 }
jchen10cc2a10e2017-05-03 14:05:12 +08001242 std::string reason(getBasicString(publicType.getBasicType()));
1243 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001244 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001245 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001246 {
1247 return;
1248 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001249
Andrei Volykhina5527072017-03-22 16:46:30 +03001250 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1251 publicType.qualifier != EvqConst) &&
1252 publicType.getBasicType() == EbtYuvCscStandardEXT)
1253 {
1254 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1255 getQualifierString(publicType.qualifier));
1256 return;
1257 }
1258
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001259 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1260 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001261 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1262 // But invalid shaders may still reach here with an unsized array declaration.
1263 if (!publicType.isUnsizedArray())
1264 {
1265 TType type(publicType);
1266 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1267 publicType.layoutQualifier);
1268 }
1269 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001270
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001271 // check for layout qualifier issues
1272 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001273
Martin Radev2cc85b32016-08-05 16:22:53 +03001274 if (IsImage(publicType.getBasicType()))
1275 {
1276
1277 switch (layoutQualifier.imageInternalFormat)
1278 {
1279 case EiifRGBA32F:
1280 case EiifRGBA16F:
1281 case EiifR32F:
1282 case EiifRGBA8:
1283 case EiifRGBA8_SNORM:
1284 if (!IsFloatImage(publicType.getBasicType()))
1285 {
1286 error(identifierLocation,
1287 "internal image format requires a floating image type",
1288 getBasicString(publicType.getBasicType()));
1289 return;
1290 }
1291 break;
1292 case EiifRGBA32I:
1293 case EiifRGBA16I:
1294 case EiifRGBA8I:
1295 case EiifR32I:
1296 if (!IsIntegerImage(publicType.getBasicType()))
1297 {
1298 error(identifierLocation,
1299 "internal image format requires an integer image type",
1300 getBasicString(publicType.getBasicType()));
1301 return;
1302 }
1303 break;
1304 case EiifRGBA32UI:
1305 case EiifRGBA16UI:
1306 case EiifRGBA8UI:
1307 case EiifR32UI:
1308 if (!IsUnsignedImage(publicType.getBasicType()))
1309 {
1310 error(identifierLocation,
1311 "internal image format requires an unsigned image type",
1312 getBasicString(publicType.getBasicType()));
1313 return;
1314 }
1315 break;
1316 case EiifUnspecified:
1317 error(identifierLocation, "layout qualifier", "No image internal format specified");
1318 return;
1319 default:
1320 error(identifierLocation, "layout qualifier", "unrecognized token");
1321 return;
1322 }
1323
1324 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1325 switch (layoutQualifier.imageInternalFormat)
1326 {
1327 case EiifR32F:
1328 case EiifR32I:
1329 case EiifR32UI:
1330 break;
1331 default:
1332 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1333 {
1334 error(identifierLocation, "layout qualifier",
1335 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1336 "image variables must be qualified readonly and/or writeonly");
1337 return;
1338 }
1339 break;
1340 }
1341 }
1342 else
1343 {
Olli Etuaho43364892017-02-13 16:00:12 +00001344 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001345 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1346 }
jchen104cdac9e2017-05-08 11:01:20 +08001347
1348 if (IsAtomicCounter(publicType.getBasicType()))
1349 {
1350 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1351 }
1352 else
1353 {
1354 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1355 }
Olli Etuaho43364892017-02-13 16:00:12 +00001356}
Martin Radev2cc85b32016-08-05 16:22:53 +03001357
Olli Etuaho43364892017-02-13 16:00:12 +00001358void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1359{
1360 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1361 int arraySize = type.isArray() ? type.getArraySize() : 1;
1362 if (IsImage(type.getBasicType()))
1363 {
1364 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1365 }
1366 else if (IsSampler(type.getBasicType()))
1367 {
1368 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1369 }
jchen104cdac9e2017-05-08 11:01:20 +08001370 else if (IsAtomicCounter(type.getBasicType()))
1371 {
1372 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1373 }
Olli Etuaho43364892017-02-13 16:00:12 +00001374 else
1375 {
1376 ASSERT(!IsOpaqueType(type.getBasicType()));
1377 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001378 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001379}
1380
Olli Etuaho856c4972016-08-08 11:38:39 +03001381void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1382 const TString &layoutQualifierName,
1383 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001384{
1385
1386 if (mShaderVersion < versionRequired)
1387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001388 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001389 }
1390}
1391
Olli Etuaho856c4972016-08-08 11:38:39 +03001392bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1393 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001394{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001395 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001396 for (size_t i = 0u; i < localSize.size(); ++i)
1397 {
1398 if (localSize[i] != -1)
1399 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001400 error(location,
1401 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1402 "global layout declaration",
1403 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001404 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001405 }
1406 }
1407
Olli Etuaho8a176262016-08-16 14:23:01 +03001408 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001409}
1410
Olli Etuaho43364892017-02-13 16:00:12 +00001411void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001412 TLayoutImageInternalFormat internalFormat)
1413{
1414 if (internalFormat != EiifUnspecified)
1415 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001416 error(location, "invalid layout qualifier: only valid when used with images",
1417 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001418 }
Olli Etuaho43364892017-02-13 16:00:12 +00001419}
1420
1421void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1422{
1423 if (binding != -1)
1424 {
1425 error(location,
1426 "invalid layout qualifier: only valid when used with opaque types or blocks",
1427 "binding");
1428 }
1429}
1430
jchen104cdac9e2017-05-08 11:01:20 +08001431void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1432{
1433 if (offset != -1)
1434 {
1435 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1436 "offset");
1437 }
1438}
1439
Olli Etuaho43364892017-02-13 16:00:12 +00001440void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1441{
1442 // Expects arraySize to be 1 when setting binding for only a single variable.
1443 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1444 {
1445 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1446 }
1447}
1448
1449void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1450 int binding,
1451 int arraySize)
1452{
1453 // Expects arraySize to be 1 when setting binding for only a single variable.
1454 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1455 {
1456 error(location, "sampler binding greater than maximum texture units", "binding");
1457 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001458}
1459
Jiajia Qinbc585152017-06-23 15:42:17 +08001460void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1461 const TQualifier &qualifier,
1462 int binding,
1463 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001464{
1465 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001466 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001467 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001468 if (binding + size > mMaxUniformBufferBindings)
1469 {
1470 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1471 "binding");
1472 }
1473 }
1474 else if (qualifier == EvqBuffer)
1475 {
1476 if (binding + size > mMaxShaderStorageBufferBindings)
1477 {
1478 error(location,
1479 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1480 "binding");
1481 }
jchen10af713a22017-04-19 09:10:56 +08001482 }
1483}
jchen104cdac9e2017-05-08 11:01:20 +08001484void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1485{
1486 if (binding >= mMaxAtomicCounterBindings)
1487 {
1488 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1489 "binding");
1490 }
1491}
jchen10af713a22017-04-19 09:10:56 +08001492
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001493void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1494 int objectLocationCount,
1495 const TLayoutQualifier &layoutQualifier)
1496{
1497 int loc = layoutQualifier.location;
1498 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1499 {
1500 error(location, "Uniform location out of range", "location");
1501 }
1502}
1503
Andrei Volykhina5527072017-03-22 16:46:30 +03001504void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1505{
1506 if (yuv != false)
1507 {
1508 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1509 }
1510}
1511
Jiajia Qinbc585152017-06-23 15:42:17 +08001512void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1513 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001514{
1515 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1516 {
1517 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001518 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1519 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1520 qual == EvqInOut || qual == EvqConstReadOnly))
1521 {
1522 if (argument->getMemoryQualifier().writeonly)
1523 {
1524 error(argument->getLine(),
1525 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1526 fnCall->getFunctionSymbolInfo()->getName().c_str());
1527 return;
1528 }
1529 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001530 if (qual == EvqOut || qual == EvqInOut)
1531 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001532 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001533 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001534 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001535 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001536 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001537 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001538 }
1539 }
1540 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001541}
1542
Martin Radev70866b82016-07-22 15:27:42 +03001543void TParseContext::checkInvariantVariableQualifier(bool invariant,
1544 const TQualifier qualifier,
1545 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001546{
Martin Radev70866b82016-07-22 15:27:42 +03001547 if (!invariant)
1548 return;
1549
1550 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001551 {
Martin Radev70866b82016-07-22 15:27:42 +03001552 // input variables in the fragment shader can be also qualified as invariant
1553 if (!sh::CanBeInvariantESSL1(qualifier))
1554 {
1555 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1556 }
1557 }
1558 else
1559 {
1560 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1561 {
1562 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1563 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001564 }
1565}
1566
Arun Patole7e7e68d2015-05-22 12:02:25 +05301567bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001568{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001569 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001570 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1571 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001572}
1573
Arun Patole7e7e68d2015-05-22 12:02:25 +05301574bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001575{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001576 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001577}
1578
Jamie Madillb98c3a82015-07-23 14:26:04 -04001579void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1580 const char *extName,
1581 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001582{
1583 pp::SourceLocation srcLoc;
1584 srcLoc.file = loc.first_file;
1585 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001586 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001587}
1588
Jamie Madillb98c3a82015-07-23 14:26:04 -04001589void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1590 const char *name,
1591 const char *value,
1592 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001593{
1594 pp::SourceLocation srcLoc;
1595 srcLoc.file = loc.first_file;
1596 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001597 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001598}
1599
Martin Radev4c4c8e72016-08-04 12:25:34 +03001600sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001601{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001602 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001603 for (size_t i = 0u; i < result.size(); ++i)
1604 {
1605 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1606 {
1607 result[i] = 1;
1608 }
1609 else
1610 {
1611 result[i] = mComputeShaderLocalSize[i];
1612 }
1613 }
1614 return result;
1615}
1616
Olli Etuaho56229f12017-07-10 14:16:33 +03001617TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1618 const TSourceLoc &line)
1619{
1620 TIntermConstantUnion *node = new TIntermConstantUnion(
1621 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1622 node->setLine(line);
1623 return node;
1624}
1625
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001626/////////////////////////////////////////////////////////////////////////////////
1627//
1628// Non-Errors.
1629//
1630/////////////////////////////////////////////////////////////////////////////////
1631
Jamie Madill5c097022014-08-20 16:38:32 -04001632const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1633 const TString *name,
1634 const TSymbol *symbol)
1635{
Jamie Madill5c097022014-08-20 16:38:32 -04001636 if (!symbol)
1637 {
1638 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001639 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001640 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001641
1642 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001643 {
1644 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001645 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001646 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001647
1648 const TVariable *variable = static_cast<const TVariable *>(symbol);
1649
1650 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1651 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001652 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001653 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001654 }
1655
Olli Etuaho0f684632017-07-13 12:42:15 +03001656 // Reject shaders using both gl_FragData and gl_FragColor
1657 TQualifier qualifier = variable->getType().getQualifier();
1658 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001659 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001660 mUsesFragData = true;
1661 }
1662 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1663 {
1664 mUsesFragColor = true;
1665 }
1666 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1667 {
1668 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001669 }
1670
Olli Etuaho0f684632017-07-13 12:42:15 +03001671 // This validation is not quite correct - it's only an error to write to
1672 // both FragData and FragColor. For simplicity, and because users shouldn't
1673 // be rewarded for reading from undefined varaibles, return an error
1674 // if they are both referenced, rather than assigned.
1675 if (mUsesFragData && mUsesFragColor)
1676 {
1677 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1678 if (mUsesSecondaryOutputs)
1679 {
1680 errorMessage =
1681 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1682 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1683 }
1684 error(location, errorMessage, name->c_str());
1685 }
1686
1687 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1688 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1689 qualifier == EvqWorkGroupSize)
1690 {
1691 error(location,
1692 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1693 "gl_WorkGroupSize");
1694 }
Jamie Madill5c097022014-08-20 16:38:32 -04001695 return variable;
1696}
1697
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001698TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1699 const TString *name,
1700 const TSymbol *symbol)
1701{
1702 const TVariable *variable = getNamedVariable(location, name, symbol);
1703
Olli Etuaho0f684632017-07-13 12:42:15 +03001704 if (!variable)
1705 {
1706 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1707 node->setLine(location);
1708 return node;
1709 }
1710
Olli Etuaho09b04a22016-12-15 13:30:26 +00001711 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1712 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1713 {
1714 // WEBGL_multiview spec
1715 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1716 "gl_ViewID_OVR");
1717 }
1718
Olli Etuaho56229f12017-07-10 14:16:33 +03001719 TIntermTyped *node = nullptr;
1720
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001721 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001722 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001723 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001724 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001725 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001726 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1727 mComputeShaderLocalSizeDeclared)
1728 {
1729 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1730 // needs to be added to the AST as a constant and not as a symbol.
1731 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1732 TConstantUnion *constArray = new TConstantUnion[3];
1733 for (size_t i = 0; i < 3; ++i)
1734 {
1735 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1736 }
1737
1738 ASSERT(variable->getType().getBasicType() == EbtUInt);
1739 ASSERT(variable->getType().getObjectSize() == 3);
1740
1741 TType type(variable->getType());
1742 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001743 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001744 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001745 else
1746 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001747 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001748 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001749 ASSERT(node != nullptr);
1750 node->setLine(location);
1751 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001752}
1753
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754// Initializers show up in several places in the grammar. Have one set of
1755// code to handle them here.
1756//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001757// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758bool TParseContext::executeInitializer(const TSourceLoc &line,
1759 const TString &identifier,
1760 const TPublicType &pType,
1761 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001762 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763{
Olli Etuaho13389b62016-10-16 11:48:18 +01001764 ASSERT(initNode != nullptr);
1765 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001766 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
Olli Etuaho2935c582015-04-08 14:32:06 +03001768 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001769 if (type.isUnsizedArray())
1770 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001771 // We have not checked yet whether the initializer actually is an array or not.
1772 if (initializer->isArray())
1773 {
1774 type.setArraySize(initializer->getArraySize());
1775 }
1776 else
1777 {
1778 // Having a non-array initializer for an unsized array will result in an error later,
1779 // so we don't generate an error message here.
1780 type.setArraySize(1u);
1781 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001782 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001783 if (!declareVariable(line, identifier, type, &variable))
1784 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001785 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787
Olli Etuahob0c645e2015-05-12 14:25:36 +03001788 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789 if (symbolTable.atGlobalLevel() &&
1790 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001791 {
1792 // Error message does not completely match behavior with ESSL 1.00, but
1793 // we want to steer developers towards only using constant expressions.
1794 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001795 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001796 }
1797 if (globalInitWarning)
1798 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001799 warning(
1800 line,
1801 "global variable initializers should be constant expressions "
1802 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1803 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001804 }
1805
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 //
1807 // identifier must be of type constant, a global, or a temporary
1808 //
1809 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301810 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1811 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001812 error(line, " cannot initialize this type of qualifier ",
1813 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001814 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001815 }
1816 //
1817 // test for and propagate constant
1818 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819
Arun Patole7e7e68d2015-05-22 12:02:25 +05301820 if (qualifier == EvqConst)
1821 {
1822 if (qualifier != initializer->getType().getQualifier())
1823 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001824 std::stringstream reasonStream;
1825 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1826 << "'";
1827 std::string reason = reasonStream.str();
1828 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001829 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001830 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001831 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301832 if (type != initializer->getType())
1833 {
1834 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001836 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001837 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001838 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001839
1840 // Save the constant folded value to the variable if possible. For example array
1841 // initializers are not folded, since that way copying the array literal to multiple places
1842 // in the shader is avoided.
1843 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1844 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301845 if (initializer->getAsConstantUnion())
1846 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001847 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001848 ASSERT(*initNode == nullptr);
1849 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301850 }
1851 else if (initializer->getAsSymbolNode())
1852 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001853 const TSymbol *symbol =
1854 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1855 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001857 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001858 if (constArray)
1859 {
1860 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001861 ASSERT(*initNode == nullptr);
1862 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001863 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001864 }
1865 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001866
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001867 TIntermSymbol *intermSymbol =
1868 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1869 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001870 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1871 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001872 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001873 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001874 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001875 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001876
Olli Etuaho914b79a2017-06-19 16:03:19 +03001877 return true;
1878}
1879
1880TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1881 const TString &identifier,
1882 TIntermTyped *initializer,
1883 const TSourceLoc &loc)
1884{
1885 checkIsScalarBool(loc, pType);
1886 TIntermBinary *initNode = nullptr;
1887 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1888 {
1889 // The initializer is valid. The init condition needs to have a node - either the
1890 // initializer node, or a constant node in case the initialized variable is const and won't
1891 // be recorded in the AST.
1892 if (initNode == nullptr)
1893 {
1894 return initializer;
1895 }
1896 else
1897 {
1898 TIntermDeclaration *declaration = new TIntermDeclaration();
1899 declaration->appendDeclarator(initNode);
1900 return declaration;
1901 }
1902 }
1903 return nullptr;
1904}
1905
1906TIntermNode *TParseContext::addLoop(TLoopType type,
1907 TIntermNode *init,
1908 TIntermNode *cond,
1909 TIntermTyped *expr,
1910 TIntermNode *body,
1911 const TSourceLoc &line)
1912{
1913 TIntermNode *node = nullptr;
1914 TIntermTyped *typedCond = nullptr;
1915 if (cond)
1916 {
1917 typedCond = cond->getAsTyped();
1918 }
1919 if (cond == nullptr || typedCond)
1920 {
Olli Etuahocce89652017-06-19 16:04:09 +03001921 if (type == ELoopDoWhile)
1922 {
1923 checkIsScalarBool(line, typedCond);
1924 }
1925 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1926 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1927 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1928 !typedCond->isVector()));
1929
Olli Etuaho3ec75682017-07-05 17:02:55 +03001930 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001931 node->setLine(line);
1932 return node;
1933 }
1934
Olli Etuahocce89652017-06-19 16:04:09 +03001935 ASSERT(type != ELoopDoWhile);
1936
Olli Etuaho914b79a2017-06-19 16:03:19 +03001937 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1938 ASSERT(declaration);
1939 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1940 ASSERT(declarator->getLeft()->getAsSymbolNode());
1941
1942 // The condition is a declaration. In the AST representation we don't support declarations as
1943 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1944 // the loop.
1945 TIntermBlock *block = new TIntermBlock();
1946
1947 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1948 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1949 block->appendStatement(declareCondition);
1950
1951 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1952 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03001953 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001954 block->appendStatement(loop);
1955 loop->setLine(line);
1956 block->setLine(line);
1957 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958}
1959
Olli Etuahocce89652017-06-19 16:04:09 +03001960TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1961 TIntermNodePair code,
1962 const TSourceLoc &loc)
1963{
Olli Etuaho56229f12017-07-10 14:16:33 +03001964 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03001965
1966 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03001967 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03001968 {
1969 if (cond->getAsConstantUnion()->getBConst(0) == true)
1970 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001971 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03001972 }
1973 else
1974 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001975 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03001976 }
1977 }
1978
Olli Etuaho3ec75682017-07-05 17:02:55 +03001979 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03001980 node->setLine(loc);
1981
1982 return node;
1983}
1984
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001985void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1986{
1987 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1988 typeSpecifier->getBasicType());
1989
1990 if (mShaderVersion < 300 && typeSpecifier->array)
1991 {
1992 error(typeSpecifier->getLine(), "not supported", "first-class array");
1993 typeSpecifier->clearArrayness();
1994 }
1995}
1996
Martin Radev70866b82016-07-22 15:27:42 +03001997TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301998 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001999{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002000 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002001
Martin Radev70866b82016-07-22 15:27:42 +03002002 TPublicType returnType = typeSpecifier;
2003 returnType.qualifier = typeQualifier.qualifier;
2004 returnType.invariant = typeQualifier.invariant;
2005 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002006 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002007 returnType.precision = typeSpecifier.precision;
2008
2009 if (typeQualifier.precision != EbpUndefined)
2010 {
2011 returnType.precision = typeQualifier.precision;
2012 }
2013
Martin Radev4a9cd802016-09-01 16:51:51 +03002014 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2015 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002016
Martin Radev4a9cd802016-09-01 16:51:51 +03002017 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2018 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002019
Martin Radev4a9cd802016-09-01 16:51:51 +03002020 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002021
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002022 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002023 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002024 if (typeSpecifier.array)
2025 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002026 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002027 returnType.clearArrayness();
2028 }
2029
Martin Radev70866b82016-07-22 15:27:42 +03002030 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002031 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002032 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002033 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002034 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002035 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002036
Martin Radev70866b82016-07-22 15:27:42 +03002037 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002038 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002039 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002040 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002041 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002042 }
2043 }
2044 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002045 {
Martin Radev70866b82016-07-22 15:27:42 +03002046 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002047 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002048 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002049 }
Martin Radev70866b82016-07-22 15:27:42 +03002050 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2051 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002052 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002053 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2054 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002055 }
Martin Radev70866b82016-07-22 15:27:42 +03002056 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002057 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002058 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002059 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002060 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002061 }
2062
2063 return returnType;
2064}
2065
Olli Etuaho856c4972016-08-08 11:38:39 +03002066void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2067 const TPublicType &type,
2068 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002069{
2070 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002071 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002072 {
2073 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002074 }
2075
2076 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2077 switch (qualifier)
2078 {
2079 case EvqVertexIn:
2080 // ESSL 3.00 section 4.3.4
2081 if (type.array)
2082 {
2083 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002084 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002085 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002086 return;
2087 case EvqFragmentOut:
2088 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002089 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002090 {
2091 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002092 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002093 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002094 return;
2095 default:
2096 break;
2097 }
2098
2099 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2100 // restrictions.
2101 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002102 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2103 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002104 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2105 {
2106 error(qualifierLocation, "must use 'flat' interpolation here",
2107 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002108 }
2109
Martin Radev4a9cd802016-09-01 16:51:51 +03002110 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002111 {
2112 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2113 // These restrictions are only implied by the ESSL 3.00 spec, but
2114 // the ESSL 3.10 spec lists these restrictions explicitly.
2115 if (type.array)
2116 {
2117 error(qualifierLocation, "cannot be an array of structures",
2118 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002119 }
2120 if (type.isStructureContainingArrays())
2121 {
2122 error(qualifierLocation, "cannot be a structure containing an array",
2123 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002124 }
2125 if (type.isStructureContainingType(EbtStruct))
2126 {
2127 error(qualifierLocation, "cannot be a structure containing a structure",
2128 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002129 }
2130 if (type.isStructureContainingType(EbtBool))
2131 {
2132 error(qualifierLocation, "cannot be a structure containing a bool",
2133 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002134 }
2135 }
2136}
2137
Martin Radev2cc85b32016-08-05 16:22:53 +03002138void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2139{
2140 if (qualifier.getType() == QtStorage)
2141 {
2142 const TStorageQualifierWrapper &storageQualifier =
2143 static_cast<const TStorageQualifierWrapper &>(qualifier);
2144 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2145 !symbolTable.atGlobalLevel())
2146 {
2147 error(storageQualifier.getLine(),
2148 "Local variables can only use the const storage qualifier.",
2149 storageQualifier.getQualifierString().c_str());
2150 }
2151 }
2152}
2153
Olli Etuaho43364892017-02-13 16:00:12 +00002154void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002155 const TSourceLoc &location)
2156{
Jiajia Qinbc585152017-06-23 15:42:17 +08002157 const std::string reason(
2158 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2159 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002160 if (memoryQualifier.readonly)
2161 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002162 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002163 }
2164 if (memoryQualifier.writeonly)
2165 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002166 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002167 }
Martin Radev049edfa2016-11-11 14:35:37 +02002168 if (memoryQualifier.coherent)
2169 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002170 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002171 }
2172 if (memoryQualifier.restrictQualifier)
2173 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002174 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002175 }
2176 if (memoryQualifier.volatileQualifier)
2177 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002178 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002179 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002180}
2181
jchen104cdac9e2017-05-08 11:01:20 +08002182// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2183// intermediate tree.
2184void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2185 size_t size,
2186 bool forceAppend,
2187 const TSourceLoc &loc,
2188 TType &type)
2189{
2190 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2191 int offset;
2192 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2193 {
2194 offset = bindingState.appendSpan(size);
2195 }
2196 else
2197 {
2198 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2199 }
2200 if (offset == -1)
2201 {
2202 error(loc, "Offset overlapping", "atomic counter");
2203 return;
2204 }
2205 TLayoutQualifier qualifier = type.getLayoutQualifier();
2206 qualifier.offset = offset;
2207 type.setLayoutQualifier(qualifier);
2208}
2209
Olli Etuaho13389b62016-10-16 11:48:18 +01002210TIntermDeclaration *TParseContext::parseSingleDeclaration(
2211 TPublicType &publicType,
2212 const TSourceLoc &identifierOrTypeLocation,
2213 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002214{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002215 TType type(publicType);
2216 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2217 mDirectiveHandler.pragma().stdgl.invariantAll)
2218 {
2219 TQualifier qualifier = type.getQualifier();
2220
2221 // The directive handler has already taken care of rejecting invalid uses of this pragma
2222 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2223 // affected variable declarations:
2224 //
2225 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2226 // elsewhere, in TranslatorGLSL.)
2227 //
2228 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2229 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2230 // the way this is currently implemented we have to enable this compiler option before
2231 // parsing the shader and determining the shading language version it uses. If this were
2232 // implemented as a post-pass, the workaround could be more targeted.
2233 //
2234 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2235 // the specification, but there are desktop OpenGL drivers that expect that this is the
2236 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2237 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2238 {
2239 type.setInvariant(true);
2240 }
2241 }
2242
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002243 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2244 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002245
Olli Etuahobab4c082015-04-24 16:38:49 +03002246 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002247 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002248
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002249 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002250 if (emptyDeclaration)
2251 {
Martin Radevb8b01222016-11-20 23:25:53 +02002252 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002253 // In most cases we don't need to create a symbol node for an empty declaration.
2254 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2255 if (type.getBasicType() == EbtStruct)
2256 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002257 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002258 }
jchen104cdac9e2017-05-08 11:01:20 +08002259 else if (IsAtomicCounter(publicType.getBasicType()))
2260 {
2261 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2262 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002263 }
2264 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002265 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002266 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002267
Olli Etuaho856c4972016-08-08 11:38:39 +03002268 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002269
jchen104cdac9e2017-05-08 11:01:20 +08002270 if (IsAtomicCounter(publicType.getBasicType()))
2271 {
2272
2273 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2274 identifierOrTypeLocation, type);
2275 }
2276
Olli Etuaho2935c582015-04-08 14:32:06 +03002277 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002278 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002279
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002280 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002281 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002282 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002283 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002284 }
2285
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002286 TIntermDeclaration *declaration = new TIntermDeclaration();
2287 declaration->setLine(identifierOrTypeLocation);
2288 if (symbol)
2289 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002290 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002291 declaration->appendDeclarator(symbol);
2292 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002293 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002294}
2295
Olli Etuaho13389b62016-10-16 11:48:18 +01002296TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2297 const TSourceLoc &identifierLocation,
2298 const TString &identifier,
2299 const TSourceLoc &indexLocation,
2300 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002301{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002302 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002303
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002304 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2305 identifierLocation);
2306
2307 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002308
Olli Etuaho856c4972016-08-08 11:38:39 +03002309 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002310
Olli Etuaho8a176262016-08-16 14:23:01 +03002311 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002312
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002313 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002314
Olli Etuaho856c4972016-08-08 11:38:39 +03002315 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002316 // Make the type an array even if size check failed.
2317 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2318 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002319
jchen104cdac9e2017-05-08 11:01:20 +08002320 if (IsAtomicCounter(publicType.getBasicType()))
2321 {
2322 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2323 identifierLocation, arrayType);
2324 }
2325
Olli Etuaho2935c582015-04-08 14:32:06 +03002326 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002327 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002328
Olli Etuaho13389b62016-10-16 11:48:18 +01002329 TIntermDeclaration *declaration = new TIntermDeclaration();
2330 declaration->setLine(identifierLocation);
2331
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002332 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002333 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002334 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2335 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002336 declaration->appendDeclarator(symbol);
2337 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002338
Olli Etuaho13389b62016-10-16 11:48:18 +01002339 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002340}
2341
Olli Etuaho13389b62016-10-16 11:48:18 +01002342TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2343 const TSourceLoc &identifierLocation,
2344 const TString &identifier,
2345 const TSourceLoc &initLocation,
2346 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002347{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002348 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002349
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002350 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2351 identifierLocation);
2352
2353 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002354
Olli Etuaho13389b62016-10-16 11:48:18 +01002355 TIntermDeclaration *declaration = new TIntermDeclaration();
2356 declaration->setLine(identifierLocation);
2357
2358 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002359 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002360 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002361 if (initNode)
2362 {
2363 declaration->appendDeclarator(initNode);
2364 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002365 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002366 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002367}
2368
Olli Etuaho13389b62016-10-16 11:48:18 +01002369TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002370 TPublicType &publicType,
2371 const TSourceLoc &identifierLocation,
2372 const TString &identifier,
2373 const TSourceLoc &indexLocation,
2374 TIntermTyped *indexExpression,
2375 const TSourceLoc &initLocation,
2376 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002377{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002378 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002379
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002380 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2381 identifierLocation);
2382
2383 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002384
Olli Etuaho8a176262016-08-16 14:23:01 +03002385 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002386
2387 TPublicType arrayType(publicType);
2388
Olli Etuaho856c4972016-08-08 11:38:39 +03002389 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002390 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2391 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002392 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002393 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002394 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002395 }
2396 // Make the type an array even if size check failed.
2397 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2398 arrayType.setArraySize(size);
2399
Olli Etuaho13389b62016-10-16 11:48:18 +01002400 TIntermDeclaration *declaration = new TIntermDeclaration();
2401 declaration->setLine(identifierLocation);
2402
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002403 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002404 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002405 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002406 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002407 if (initNode)
2408 {
2409 declaration->appendDeclarator(initNode);
2410 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002411 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002412
2413 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002414}
2415
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002416TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002417 const TTypeQualifierBuilder &typeQualifierBuilder,
2418 const TSourceLoc &identifierLoc,
2419 const TString *identifier,
2420 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002421{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002422 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002423
Martin Radev70866b82016-07-22 15:27:42 +03002424 if (!typeQualifier.invariant)
2425 {
2426 error(identifierLoc, "Expected invariant", identifier->c_str());
2427 return nullptr;
2428 }
2429 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2430 {
2431 return nullptr;
2432 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002433 if (!symbol)
2434 {
2435 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002436 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002437 }
Martin Radev70866b82016-07-22 15:27:42 +03002438 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002439 {
Martin Radev70866b82016-07-22 15:27:42 +03002440 error(identifierLoc, "invariant declaration specifies qualifier",
2441 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002442 }
Martin Radev70866b82016-07-22 15:27:42 +03002443 if (typeQualifier.precision != EbpUndefined)
2444 {
2445 error(identifierLoc, "invariant declaration specifies precision",
2446 getPrecisionString(typeQualifier.precision));
2447 }
2448 if (!typeQualifier.layoutQualifier.isEmpty())
2449 {
2450 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2451 }
2452
2453 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002454 if (!variable)
2455 {
2456 return nullptr;
2457 }
Martin Radev70866b82016-07-22 15:27:42 +03002458 const TType &type = variable->getType();
2459
2460 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2461 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002462 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002463
2464 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2465
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002466 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2467 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002468
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002469 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002470}
2471
Olli Etuaho13389b62016-10-16 11:48:18 +01002472void TParseContext::parseDeclarator(TPublicType &publicType,
2473 const TSourceLoc &identifierLocation,
2474 const TString &identifier,
2475 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002476{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002477 // If the declaration starting this declarator list was empty (example: int,), some checks were
2478 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002479 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002480 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002481 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2482 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002483 }
2484
Olli Etuaho856c4972016-08-08 11:38:39 +03002485 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002486
Olli Etuaho856c4972016-08-08 11:38:39 +03002487 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002488
Olli Etuaho2935c582015-04-08 14:32:06 +03002489 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002490 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002491 if (IsAtomicCounter(publicType.getBasicType()))
2492 {
2493 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2494 identifierLocation, type);
2495 }
Olli Etuaho43364892017-02-13 16:00:12 +00002496 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002497
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002498 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002499 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002500 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2501 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002502 declarationOut->appendDeclarator(symbol);
2503 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002504}
2505
Olli Etuaho13389b62016-10-16 11:48:18 +01002506void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2507 const TSourceLoc &identifierLocation,
2508 const TString &identifier,
2509 const TSourceLoc &arrayLocation,
2510 TIntermTyped *indexExpression,
2511 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002513 // If the declaration starting this declarator list was empty (example: int,), some checks were
2514 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002515 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002516 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002517 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2518 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002519 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002520
Olli Etuaho856c4972016-08-08 11:38:39 +03002521 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002522
Olli Etuaho856c4972016-08-08 11:38:39 +03002523 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002524
Olli Etuaho8a176262016-08-16 14:23:01 +03002525 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002526 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002527 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002528 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002529 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002530
jchen104cdac9e2017-05-08 11:01:20 +08002531 if (IsAtomicCounter(publicType.getBasicType()))
2532 {
2533 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2534 true, identifierLocation, arrayType);
2535 }
2536
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002537 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002538 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002539
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002540 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002541 {
2542 TIntermSymbol *symbol =
2543 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2544 symbol->setLine(identifierLocation);
2545 declarationOut->appendDeclarator(symbol);
2546 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002547 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002548}
2549
Olli Etuaho13389b62016-10-16 11:48:18 +01002550void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2551 const TSourceLoc &identifierLocation,
2552 const TString &identifier,
2553 const TSourceLoc &initLocation,
2554 TIntermTyped *initializer,
2555 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002556{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 // If the declaration starting this declarator list was empty (example: int,), some checks were
2558 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002559 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002560 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002561 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2562 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002563 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002564
Olli Etuaho856c4972016-08-08 11:38:39 +03002565 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002566
Olli Etuaho13389b62016-10-16 11:48:18 +01002567 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002568 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002569 {
2570 //
2571 // build the intermediate representation
2572 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002573 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002574 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002575 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002576 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002577 }
2578}
2579
Olli Etuaho13389b62016-10-16 11:48:18 +01002580void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2581 const TSourceLoc &identifierLocation,
2582 const TString &identifier,
2583 const TSourceLoc &indexLocation,
2584 TIntermTyped *indexExpression,
2585 const TSourceLoc &initLocation,
2586 TIntermTyped *initializer,
2587 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002588{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002589 // If the declaration starting this declarator list was empty (example: int,), some checks were
2590 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002591 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002592 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002593 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2594 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002595 }
2596
Olli Etuaho856c4972016-08-08 11:38:39 +03002597 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002598
Olli Etuaho8a176262016-08-16 14:23:01 +03002599 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002600
2601 TPublicType arrayType(publicType);
2602
Olli Etuaho856c4972016-08-08 11:38:39 +03002603 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2605 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002606 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002607 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002608 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002609 }
2610 // Make the type an array even if size check failed.
2611 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2612 arrayType.setArraySize(size);
2613
2614 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002615 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002616 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002617 {
2618 if (initNode)
2619 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002620 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002621 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002622 }
2623}
2624
jchen104cdac9e2017-05-08 11:01:20 +08002625void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2626 const TSourceLoc &location)
2627{
2628 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2629 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2630 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2631 {
2632 error(location, "Requires both binding and offset", "layout");
2633 return;
2634 }
2635 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2636}
2637
Olli Etuahocce89652017-06-19 16:04:09 +03002638void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2639 const TPublicType &type,
2640 const TSourceLoc &loc)
2641{
2642 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2643 !getFragmentPrecisionHigh())
2644 {
2645 error(loc, "precision is not supported in fragment shader", "highp");
2646 }
2647
2648 if (!CanSetDefaultPrecisionOnType(type))
2649 {
2650 error(loc, "illegal type argument for default precision qualifier",
2651 getBasicString(type.getBasicType()));
2652 return;
2653 }
2654 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2655}
2656
Martin Radev70866b82016-07-22 15:27:42 +03002657void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002658{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002659 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002660 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002661
Martin Radev70866b82016-07-22 15:27:42 +03002662 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2663 typeQualifier.line);
2664
Jamie Madillc2128ff2016-07-04 10:26:17 -04002665 // It should never be the case, but some strange parser errors can send us here.
2666 if (layoutQualifier.isEmpty())
2667 {
2668 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002669 return;
2670 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002671
Martin Radev802abe02016-08-04 17:48:32 +03002672 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002673 {
Olli Etuaho43364892017-02-13 16:00:12 +00002674 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002675 return;
2676 }
2677
Olli Etuaho43364892017-02-13 16:00:12 +00002678 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2679
2680 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002681
2682 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2683
Andrei Volykhina5527072017-03-22 16:46:30 +03002684 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2685
jchen104cdac9e2017-05-08 11:01:20 +08002686 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2687
Martin Radev802abe02016-08-04 17:48:32 +03002688 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002689 {
Martin Radev802abe02016-08-04 17:48:32 +03002690 if (mComputeShaderLocalSizeDeclared &&
2691 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2692 {
2693 error(typeQualifier.line, "Work group size does not match the previous declaration",
2694 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002695 return;
2696 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002697
Martin Radev802abe02016-08-04 17:48:32 +03002698 if (mShaderVersion < 310)
2699 {
2700 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002701 return;
2702 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002703
Martin Radev4c4c8e72016-08-04 12:25:34 +03002704 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002705 {
2706 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002707 return;
2708 }
2709
2710 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2711 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2712
2713 const TConstantUnion *maxComputeWorkGroupSizeData =
2714 maxComputeWorkGroupSize->getConstPointer();
2715
2716 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2717 {
2718 if (layoutQualifier.localSize[i] != -1)
2719 {
2720 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2721 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2722 if (mComputeShaderLocalSize[i] < 1 ||
2723 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2724 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002725 std::stringstream reasonStream;
2726 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2727 << maxComputeWorkGroupSizeValue;
2728 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002729
Olli Etuaho4de340a2016-12-16 09:32:03 +00002730 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002731 return;
2732 }
2733 }
2734 }
2735
2736 mComputeShaderLocalSizeDeclared = true;
2737 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002738 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002739 {
2740 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2741 // specification.
2742 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2743 {
2744 error(typeQualifier.line, "Number of views does not match the previous declaration",
2745 "layout");
2746 return;
2747 }
2748
2749 if (layoutQualifier.numViews == -1)
2750 {
2751 error(typeQualifier.line, "No num_views specified", "layout");
2752 return;
2753 }
2754
2755 if (layoutQualifier.numViews > mMaxNumViews)
2756 {
2757 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2758 "layout");
2759 return;
2760 }
2761
2762 mNumViews = layoutQualifier.numViews;
2763 }
Martin Radev802abe02016-08-04 17:48:32 +03002764 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002765 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002766 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002767 {
Martin Radev802abe02016-08-04 17:48:32 +03002768 return;
2769 }
2770
Jiajia Qinbc585152017-06-23 15:42:17 +08002771 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002772 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002773 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002774 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002775 return;
2776 }
2777
2778 if (mShaderVersion < 300)
2779 {
2780 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2781 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002782 return;
2783 }
2784
Olli Etuaho09b04a22016-12-15 13:30:26 +00002785 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002786
2787 if (layoutQualifier.matrixPacking != EmpUnspecified)
2788 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002789 if (typeQualifier.qualifier == EvqUniform)
2790 {
2791 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
2792 }
2793 else if (typeQualifier.qualifier == EvqBuffer)
2794 {
2795 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
2796 }
Martin Radev802abe02016-08-04 17:48:32 +03002797 }
2798
2799 if (layoutQualifier.blockStorage != EbsUnspecified)
2800 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002801 if (typeQualifier.qualifier == EvqUniform)
2802 {
2803 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
2804 }
2805 else if (typeQualifier.qualifier == EvqBuffer)
2806 {
2807 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
2808 }
Martin Radev802abe02016-08-04 17:48:32 +03002809 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002810 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002811}
2812
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002813TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2814 const TFunction &function,
2815 const TSourceLoc &location,
2816 bool insertParametersToSymbolTable)
2817{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002818 checkIsNotReserved(location, function.getName());
2819
Olli Etuahofe486322017-03-21 09:30:54 +00002820 TIntermFunctionPrototype *prototype =
2821 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002822 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2823 // point to the data that already exists in the symbol table.
2824 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2825 prototype->setLine(location);
2826
2827 for (size_t i = 0; i < function.getParamCount(); i++)
2828 {
2829 const TConstParameter &param = function.getParam(i);
2830
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002831 TIntermSymbol *symbol = nullptr;
2832
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002833 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2834 // be used for unused args).
2835 if (param.name != nullptr)
2836 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002837 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002838 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002839 {
Olli Etuaho0f684632017-07-13 12:42:15 +03002840 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
2841 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002842 {
2843 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2844 variable->getType());
2845 }
2846 else
2847 {
2848 error(location, "redefinition", variable->getName().c_str());
2849 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002850 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002851 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002852 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002853 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002854 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2855 // symbol.
2856 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002857 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002858 symbol->setLine(location);
2859 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002860 }
2861 return prototype;
2862}
2863
Olli Etuaho16c745a2017-01-16 17:02:27 +00002864TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2865 const TFunction &parsedFunction,
2866 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002867{
Olli Etuaho476197f2016-10-11 13:59:08 +01002868 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2869 // first declaration. Either way the instance in the symbol table is used to track whether the
2870 // function is declared multiple times.
2871 TFunction *function = static_cast<TFunction *>(
2872 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2873 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002874 {
2875 // ESSL 1.00.17 section 4.2.7.
2876 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2877 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002878 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002879 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002880
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002881 TIntermFunctionPrototype *prototype =
2882 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002883
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002884 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002885
2886 if (!symbolTable.atGlobalLevel())
2887 {
2888 // ESSL 3.00.4 section 4.2.4.
2889 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002890 }
2891
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002892 return prototype;
2893}
2894
Olli Etuaho336b1472016-10-05 16:37:55 +01002895TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002896 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002897 TIntermBlock *functionBody,
2898 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002899{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002900 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002901 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2902 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002903 error(location, "function does not return a value:",
2904 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002905 }
2906
Olli Etuahof51fdd22016-10-03 10:03:40 +01002907 if (functionBody == nullptr)
2908 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002909 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002910 functionBody->setLine(location);
2911 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002912 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002913 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002914 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002915
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002916 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002917 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002918}
2919
Olli Etuaho476197f2016-10-11 13:59:08 +01002920void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2921 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002922 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002923{
Olli Etuaho476197f2016-10-11 13:59:08 +01002924 ASSERT(function);
2925 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002926 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002927 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002928
2929 if (builtIn)
2930 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002931 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002932 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002933 else
Jamie Madill185fb402015-06-12 15:48:48 -04002934 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002935 TFunction *prevDec = static_cast<TFunction *>(
2936 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2937
2938 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2939 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2940 // occurance.
2941 if (*function != prevDec)
2942 {
2943 // Swap the parameters of the previous declaration to the parameters of the function
2944 // definition (parameter names may differ).
2945 prevDec->swapParameters(**function);
2946
2947 // The function definition will share the same symbol as any previous declaration.
2948 *function = prevDec;
2949 }
2950
2951 if ((*function)->isDefined())
2952 {
2953 error(location, "function already has a body", (*function)->getName().c_str());
2954 }
2955
2956 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002957 }
Jamie Madill185fb402015-06-12 15:48:48 -04002958
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002959 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002960 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002961 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002962
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002963 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002964 setLoopNestingLevel(0);
2965}
2966
Jamie Madillb98c3a82015-07-23 14:26:04 -04002967TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002968{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002969 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002970 // We don't know at this point whether this is a function definition or a prototype.
2971 // The definition production code will check for redefinitions.
2972 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002973 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002974 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2975 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002976 //
2977 TFunction *prevDec =
2978 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302979
Martin Radevda6254b2016-12-14 17:00:36 +02002980 if (getShaderVersion() >= 300 &&
2981 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2982 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302983 {
Martin Radevda6254b2016-12-14 17:00:36 +02002984 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302985 // Therefore overloading or redefining builtin functions is an error.
2986 error(location, "Name of a built-in function cannot be redeclared as function",
2987 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302988 }
2989 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002990 {
2991 if (prevDec->getReturnType() != function->getReturnType())
2992 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002993 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002994 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002995 }
2996 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2997 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002998 if (prevDec->getParam(i).type->getQualifier() !=
2999 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003000 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003001 error(location,
3002 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003003 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003004 }
3005 }
3006 }
3007
3008 //
3009 // Check for previously declared variables using the same name.
3010 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003011 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003012 if (prevSym)
3013 {
3014 if (!prevSym->isFunction())
3015 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003016 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003017 }
3018 }
3019 else
3020 {
3021 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003022 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003023 }
3024
3025 // We're at the inner scope level of the function's arguments and body statement.
3026 // Add the function prototype to the surrounding scope instead.
3027 symbolTable.getOuterLevel()->insert(function);
3028
Olli Etuaho78d13742017-01-18 13:06:10 +00003029 // Raise error message if main function takes any parameters or return anything other than void
3030 if (function->getName() == "main")
3031 {
3032 if (function->getParamCount() > 0)
3033 {
3034 error(location, "function cannot take any parameter(s)", "main");
3035 }
3036 if (function->getReturnType().getBasicType() != EbtVoid)
3037 {
3038 error(location, "main function cannot return a value",
3039 function->getReturnType().getBasicString());
3040 }
3041 }
3042
Jamie Madill185fb402015-06-12 15:48:48 -04003043 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003044 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3045 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003046 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3047 //
3048 return function;
3049}
3050
Olli Etuaho9de84a52016-06-14 17:36:01 +03003051TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3052 const TString *name,
3053 const TSourceLoc &location)
3054{
3055 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3056 {
3057 error(location, "no qualifiers allowed for function return",
3058 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003059 }
3060 if (!type.layoutQualifier.isEmpty())
3061 {
3062 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003063 }
jchen10cc2a10e2017-05-03 14:05:12 +08003064 // make sure an opaque type is not involved as well...
3065 std::string reason(getBasicString(type.getBasicType()));
3066 reason += "s can't be function return values";
3067 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003068 if (mShaderVersion < 300)
3069 {
3070 // Array return values are forbidden, but there's also no valid syntax for declaring array
3071 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003072 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003073
3074 if (type.isStructureContainingArrays())
3075 {
3076 // ESSL 1.00.17 section 6.1 Function Definitions
3077 error(location, "structures containing arrays can't be function return values",
3078 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003079 }
3080 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003081
3082 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003083 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003084}
3085
Olli Etuahocce89652017-06-19 16:04:09 +03003086TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3087{
Olli Etuahocce89652017-06-19 16:04:09 +03003088 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003089 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003090}
3091
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003092TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003093{
Olli Etuahocce89652017-06-19 16:04:09 +03003094 if (mShaderVersion < 300 && publicType.array)
3095 {
3096 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3097 "[]");
3098 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003099 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003100 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003101 error(publicType.getLine(), "constructor can't be a structure definition",
3102 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003103 }
3104
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003105 TType *type = new TType(publicType);
3106 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003107 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003108 error(publicType.getLine(), "cannot construct this type",
3109 getBasicString(publicType.getBasicType()));
3110 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003111 }
3112
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003113 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003114}
3115
Olli Etuahocce89652017-06-19 16:04:09 +03003116TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3117 const TString *name,
3118 const TSourceLoc &nameLoc)
3119{
3120 if (publicType.getBasicType() == EbtVoid)
3121 {
3122 error(nameLoc, "illegal use of type 'void'", name->c_str());
3123 }
3124 checkIsNotReserved(nameLoc, *name);
3125 TType *type = new TType(publicType);
3126 TParameter param = {name, type};
3127 return param;
3128}
3129
3130TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3131 const TSourceLoc &identifierLoc,
3132 TIntermTyped *arraySize,
3133 const TSourceLoc &arrayLoc,
3134 TPublicType *type)
3135{
3136 checkIsValidTypeForArray(arrayLoc, *type);
3137 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3138 type->setArraySize(size);
3139 return parseParameterDeclarator(*type, identifier, identifierLoc);
3140}
3141
Jamie Madillb98c3a82015-07-23 14:26:04 -04003142// This function is used to test for the correctness of the parameters passed to various constructor
3143// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003144//
Olli Etuaho856c4972016-08-08 11:38:39 +03003145// 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 +00003146//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003147TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003148 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303149 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150{
Olli Etuaho856c4972016-08-08 11:38:39 +03003151 if (type.isUnsizedArray())
3152 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003153 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003154 {
3155 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3156 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003157 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003158 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003159 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003160 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003161
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003162 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003163 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003164 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003165 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003166
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003167 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003168 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003170 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3171 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003172 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003173 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003174 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003175 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003176}
3177
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003178//
3179// Interface/uniform blocks
3180//
Olli Etuaho13389b62016-10-16 11:48:18 +01003181TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003182 const TTypeQualifierBuilder &typeQualifierBuilder,
3183 const TSourceLoc &nameLine,
3184 const TString &blockName,
3185 TFieldList *fieldList,
3186 const TString *instanceName,
3187 const TSourceLoc &instanceLine,
3188 TIntermTyped *arrayIndex,
3189 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003190{
Olli Etuaho856c4972016-08-08 11:38:39 +03003191 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003192
Olli Etuaho77ba4082016-12-16 12:01:18 +00003193 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003194
Jiajia Qinbc585152017-06-23 15:42:17 +08003195 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003196 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003197 error(typeQualifier.line,
3198 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3199 "3.10",
3200 getQualifierString(typeQualifier.qualifier));
3201 }
3202 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3203 {
3204 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003205 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003206 }
3207
Martin Radev70866b82016-07-22 15:27:42 +03003208 if (typeQualifier.invariant)
3209 {
3210 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3211 }
3212
Jiajia Qinbc585152017-06-23 15:42:17 +08003213 if (typeQualifier.qualifier != EvqBuffer)
3214 {
3215 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3216 }
Olli Etuaho43364892017-02-13 16:00:12 +00003217
jchen10af713a22017-04-19 09:10:56 +08003218 // add array index
3219 unsigned int arraySize = 0;
3220 if (arrayIndex != nullptr)
3221 {
3222 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3223 }
3224
3225 if (mShaderVersion < 310)
3226 {
3227 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3228 }
3229 else
3230 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003231 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3232 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003233 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003234
Andrei Volykhina5527072017-03-22 16:46:30 +03003235 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3236
Jamie Madill099c0f32013-06-20 11:55:52 -04003237 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003238 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003239
Jamie Madill099c0f32013-06-20 11:55:52 -04003240 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3241 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003242 if (typeQualifier.qualifier == EvqUniform)
3243 {
3244 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3245 }
3246 else if (typeQualifier.qualifier == EvqBuffer)
3247 {
3248 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3249 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003250 }
3251
Jamie Madill1566ef72013-06-20 11:55:54 -04003252 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3253 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003254 if (typeQualifier.qualifier == EvqUniform)
3255 {
3256 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3257 }
3258 else if (typeQualifier.qualifier == EvqBuffer)
3259 {
3260 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3261 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003262 }
3263
Olli Etuaho856c4972016-08-08 11:38:39 +03003264 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003265
Martin Radev2cc85b32016-08-05 16:22:53 +03003266 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3267
Olli Etuaho0f684632017-07-13 12:42:15 +03003268 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303269 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003270 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003271 }
3272
Jamie Madill98493dd2013-07-08 14:39:03 -04003273 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303274 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3275 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003276 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303277 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003278 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303279 {
jchen10cc2a10e2017-05-03 14:05:12 +08003280 std::string reason("unsupported type - ");
3281 reason += fieldType->getBasicString();
3282 reason += " types are not allowed in interface blocks";
3283 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003284 }
3285
Jamie Madill98493dd2013-07-08 14:39:03 -04003286 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003287 switch (qualifier)
3288 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003289 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003290 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003291 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003292 if (typeQualifier.qualifier == EvqBuffer)
3293 {
3294 error(field->line(), "invalid qualifier on shader storage block member",
3295 getQualifierString(qualifier));
3296 }
3297 break;
3298 case EvqBuffer:
3299 if (typeQualifier.qualifier == EvqUniform)
3300 {
3301 error(field->line(), "invalid qualifier on uniform block member",
3302 getQualifierString(qualifier));
3303 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003304 break;
3305 default:
3306 error(field->line(), "invalid qualifier on interface block member",
3307 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003308 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003309 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003310
Martin Radev70866b82016-07-22 15:27:42 +03003311 if (fieldType->isInvariant())
3312 {
3313 error(field->line(), "invalid qualifier on interface block member", "invariant");
3314 }
3315
Jamie Madilla5efff92013-06-06 11:56:47 -04003316 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003317 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003318 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003319 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003320
Jamie Madill98493dd2013-07-08 14:39:03 -04003321 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003322 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003323 error(field->line(), "invalid layout qualifier: cannot be used here",
3324 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003325 }
3326
Jamie Madill98493dd2013-07-08 14:39:03 -04003327 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003328 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003329 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003330 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003331 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003332 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003333 warning(field->line(),
3334 "extraneous layout qualifier: only has an effect on matrix types",
3335 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003336 }
3337
Jamie Madill98493dd2013-07-08 14:39:03 -04003338 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003339
3340 if (typeQualifier.qualifier == EvqBuffer)
3341 {
3342 // set memory qualifiers
3343 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3344 // qualified with a memory qualifier, it is as if all of its members were declared with
3345 // the same memory qualifier.
3346 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3347 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3348 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3349 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3350 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3351 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3352 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3353 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3354 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3355 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3356 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003357 }
3358
Jamie Madillb98c3a82015-07-23 14:26:04 -04003359 TInterfaceBlock *interfaceBlock =
3360 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3361 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3362 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003363
3364 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003365 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003366
Jamie Madill98493dd2013-07-08 14:39:03 -04003367 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003368 {
3369 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003370 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3371 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003372 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303373 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003374
3375 // set parent pointer of the field variable
3376 fieldType->setInterfaceBlock(interfaceBlock);
3377
Olli Etuaho0f684632017-07-13 12:42:15 +03003378 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003379
Olli Etuaho0f684632017-07-13 12:42:15 +03003380 if (fieldVariable)
3381 {
3382 fieldVariable->setQualifier(typeQualifier.qualifier);
3383 }
3384 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303385 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003386 error(field->line(), "redefinition of an interface block member name",
3387 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003388 }
3389 }
3390 }
3391 else
3392 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003393 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003394
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003395 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003396 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3397 if (instanceTypeDef)
3398 {
3399 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3400 symbolId = instanceTypeDef->getUniqueId();
3401 }
3402 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303403 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003404 error(instanceLine, "redefinition of an interface block instance name",
3405 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003406 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003407 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003408 }
3409
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003410 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3411 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003412 TIntermDeclaration *declaration = new TIntermDeclaration();
3413 declaration->appendDeclarator(blockSymbol);
3414 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003415
3416 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003417 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003418}
3419
Olli Etuaho383b7912016-08-05 11:22:59 +03003420void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003421{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003422 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003423
3424 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003425 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303426 if (mStructNestingLevel > 1)
3427 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003428 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003429 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003430}
3431
3432void TParseContext::exitStructDeclaration()
3433{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003434 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003435}
3436
Olli Etuaho8a176262016-08-16 14:23:01 +03003437void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003438{
Jamie Madillacb4b812016-11-07 13:50:29 -05003439 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303440 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003441 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003442 }
3443
Arun Patole7e7e68d2015-05-22 12:02:25 +05303444 if (field.type()->getBasicType() != EbtStruct)
3445 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003446 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003447 }
3448
3449 // We're already inside a structure definition at this point, so add
3450 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303451 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3452 {
Jamie Madill41a49272014-03-18 16:10:13 -04003453 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003454 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3455 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003456 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003457 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003458 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003459 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003460}
3461
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003462//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003463// Parse an array index expression
3464//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003465TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3466 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303467 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003468{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003469 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3470 {
3471 if (baseExpression->getAsSymbolNode())
3472 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303473 error(location, " left of '[' is not of type array, matrix, or vector ",
3474 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003475 }
3476 else
3477 {
3478 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3479 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003480
Olli Etuaho3ec75682017-07-05 17:02:55 +03003481 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003482 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003483
Jamie Madill21c1e452014-12-29 11:33:41 -05003484 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3485
Olli Etuaho36b05142015-11-12 13:10:42 +02003486 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3487 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3488 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3489 // index is a constant expression.
3490 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3491 {
3492 if (baseExpression->isInterfaceBlock())
3493 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003494 error(location,
3495 "array indexes for interface blocks arrays must be constant integral expressions",
3496 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003497 }
3498 else if (baseExpression->getQualifier() == EvqFragmentOut)
3499 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003500 error(location,
3501 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003502 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003503 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3504 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003505 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003506 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003507 }
3508
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003509 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003510 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003511 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3512 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3513 // constant fold expressions that are not constant expressions). The most compatible way to
3514 // handle this case is to report a warning instead of an error and force the index to be in
3515 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003516 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003517 int index = 0;
3518 if (indexConstantUnion->getBasicType() == EbtInt)
3519 {
3520 index = indexConstantUnion->getIConst(0);
3521 }
3522 else if (indexConstantUnion->getBasicType() == EbtUInt)
3523 {
3524 index = static_cast<int>(indexConstantUnion->getUConst(0));
3525 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003526
3527 int safeIndex = -1;
3528
3529 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003530 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003531 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003532 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003533 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003534 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003535 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003536 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003537 "GL_EXT_draw_buffers is disabled",
3538 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003539 safeIndex = 0;
3540 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003541 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003542 // Only do generic out-of-range check if similar error hasn't already been reported.
3543 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003544 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003545 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3546 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003547 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003548 }
3549 }
3550 else if (baseExpression->isMatrix())
3551 {
3552 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003553 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003554 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003555 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003556 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003557 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003558 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3559 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003560 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003561 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003562
3563 ASSERT(safeIndex >= 0);
3564 // Data of constant unions can't be changed, because it may be shared with other
3565 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3566 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003567 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003568 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003569 TConstantUnion *safeConstantUnion = new TConstantUnion();
3570 safeConstantUnion->setIConst(safeIndex);
3571 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003572 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003573 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003574
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003575 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3576 node->setLine(location);
3577 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003578 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003579 else
3580 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003581 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3582 node->setLine(location);
3583 // Indirect indexing can never be constant folded.
3584 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003585 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003586}
3587
Olli Etuaho90892fb2016-07-14 14:44:51 +03003588int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3589 const TSourceLoc &location,
3590 int index,
3591 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003592 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003593{
3594 if (index >= arraySize || index < 0)
3595 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003596 std::stringstream reasonStream;
3597 reasonStream << reason << " '" << index << "'";
3598 std::string token = reasonStream.str();
3599 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003600 if (index < 0)
3601 {
3602 return 0;
3603 }
3604 else
3605 {
3606 return arraySize - 1;
3607 }
3608 }
3609 return index;
3610}
3611
Jamie Madillb98c3a82015-07-23 14:26:04 -04003612TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3613 const TSourceLoc &dotLocation,
3614 const TString &fieldString,
3615 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003616{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003617 if (baseExpression->isArray())
3618 {
3619 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003620 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003621 }
3622
3623 if (baseExpression->isVector())
3624 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003625 TVector<int> fieldOffsets;
3626 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3627 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003628 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003629 fieldOffsets.resize(1);
3630 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003631 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003632 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3633 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003634
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003635 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003636 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003637 else if (baseExpression->getBasicType() == EbtStruct)
3638 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303639 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003640 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003641 {
3642 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003643 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003644 }
3645 else
3646 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003647 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003648 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003649 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003650 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003651 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003652 {
3653 fieldFound = true;
3654 break;
3655 }
3656 }
3657 if (fieldFound)
3658 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003659 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003660 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003661 TIntermBinary *node =
3662 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3663 node->setLine(dotLocation);
3664 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003665 }
3666 else
3667 {
3668 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003669 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003670 }
3671 }
3672 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003673 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003674 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303675 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003676 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003677 {
3678 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003679 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003680 }
3681 else
3682 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003683 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003684 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003685 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003686 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003687 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003688 {
3689 fieldFound = true;
3690 break;
3691 }
3692 }
3693 if (fieldFound)
3694 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003695 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003696 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003697 TIntermBinary *node =
3698 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3699 node->setLine(dotLocation);
3700 // Indexing interface blocks can never be constant folded.
3701 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003702 }
3703 else
3704 {
3705 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003706 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003707 }
3708 }
3709 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003710 else
3711 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003712 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003713 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003714 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303715 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003716 }
3717 else
3718 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303719 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003720 " field selection requires structure, vector, or interface block on left hand "
3721 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303722 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003723 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003724 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003725 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003726}
3727
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3729 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003730{
Martin Radev802abe02016-08-04 17:48:32 +03003731 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003732
3733 if (qualifierType == "shared")
3734 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003735 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003736 {
3737 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3738 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003739 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003740 }
3741 else if (qualifierType == "packed")
3742 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003743 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003744 {
3745 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3746 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003747 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003748 }
3749 else if (qualifierType == "std140")
3750 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003751 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003752 }
3753 else if (qualifierType == "row_major")
3754 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003755 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003756 }
3757 else if (qualifierType == "column_major")
3758 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003759 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003760 }
3761 else if (qualifierType == "location")
3762 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003763 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3764 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003765 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003766 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3767 mShaderType == GL_FRAGMENT_SHADER)
3768 {
3769 qualifier.yuv = true;
3770 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003771 else if (qualifierType == "rgba32f")
3772 {
3773 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3774 qualifier.imageInternalFormat = EiifRGBA32F;
3775 }
3776 else if (qualifierType == "rgba16f")
3777 {
3778 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3779 qualifier.imageInternalFormat = EiifRGBA16F;
3780 }
3781 else if (qualifierType == "r32f")
3782 {
3783 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3784 qualifier.imageInternalFormat = EiifR32F;
3785 }
3786 else if (qualifierType == "rgba8")
3787 {
3788 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3789 qualifier.imageInternalFormat = EiifRGBA8;
3790 }
3791 else if (qualifierType == "rgba8_snorm")
3792 {
3793 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3794 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3795 }
3796 else if (qualifierType == "rgba32i")
3797 {
3798 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3799 qualifier.imageInternalFormat = EiifRGBA32I;
3800 }
3801 else if (qualifierType == "rgba16i")
3802 {
3803 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3804 qualifier.imageInternalFormat = EiifRGBA16I;
3805 }
3806 else if (qualifierType == "rgba8i")
3807 {
3808 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3809 qualifier.imageInternalFormat = EiifRGBA8I;
3810 }
3811 else if (qualifierType == "r32i")
3812 {
3813 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3814 qualifier.imageInternalFormat = EiifR32I;
3815 }
3816 else if (qualifierType == "rgba32ui")
3817 {
3818 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3819 qualifier.imageInternalFormat = EiifRGBA32UI;
3820 }
3821 else if (qualifierType == "rgba16ui")
3822 {
3823 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3824 qualifier.imageInternalFormat = EiifRGBA16UI;
3825 }
3826 else if (qualifierType == "rgba8ui")
3827 {
3828 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3829 qualifier.imageInternalFormat = EiifRGBA8UI;
3830 }
3831 else if (qualifierType == "r32ui")
3832 {
3833 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3834 qualifier.imageInternalFormat = EiifR32UI;
3835 }
3836
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003837 else
3838 {
3839 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003840 }
3841
Jamie Madilla5efff92013-06-06 11:56:47 -04003842 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003843}
3844
Martin Radev802abe02016-08-04 17:48:32 +03003845void TParseContext::parseLocalSize(const TString &qualifierType,
3846 const TSourceLoc &qualifierTypeLine,
3847 int intValue,
3848 const TSourceLoc &intValueLine,
3849 const std::string &intValueString,
3850 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003851 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003852{
Olli Etuaho856c4972016-08-08 11:38:39 +03003853 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003854 if (intValue < 1)
3855 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003856 std::stringstream reasonStream;
3857 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3858 std::string reason = reasonStream.str();
3859 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003860 }
3861 (*localSize)[index] = intValue;
3862}
3863
Olli Etuaho09b04a22016-12-15 13:30:26 +00003864void TParseContext::parseNumViews(int intValue,
3865 const TSourceLoc &intValueLine,
3866 const std::string &intValueString,
3867 int *numViews)
3868{
3869 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3870 // specification.
3871 if (intValue < 1)
3872 {
3873 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3874 }
3875 *numViews = intValue;
3876}
3877
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3879 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003880 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303881 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003882{
Martin Radev802abe02016-08-04 17:48:32 +03003883 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003884
Martin Radev802abe02016-08-04 17:48:32 +03003885 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003886
Martin Radev802abe02016-08-04 17:48:32 +03003887 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003888 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003889 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003890 if (intValue < 0)
3891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 error(intValueLine, "out of range: location must be non-negative",
3893 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003894 }
3895 else
3896 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003897 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003898 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003899 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003900 }
Olli Etuaho43364892017-02-13 16:00:12 +00003901 else if (qualifierType == "binding")
3902 {
3903 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3904 if (intValue < 0)
3905 {
3906 error(intValueLine, "out of range: binding must be non-negative",
3907 intValueString.c_str());
3908 }
3909 else
3910 {
3911 qualifier.binding = intValue;
3912 }
3913 }
jchen104cdac9e2017-05-08 11:01:20 +08003914 else if (qualifierType == "offset")
3915 {
3916 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3917 if (intValue < 0)
3918 {
3919 error(intValueLine, "out of range: offset must be non-negative",
3920 intValueString.c_str());
3921 }
3922 else
3923 {
3924 qualifier.offset = intValue;
3925 }
3926 }
Martin Radev802abe02016-08-04 17:48:32 +03003927 else if (qualifierType == "local_size_x")
3928 {
3929 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3930 &qualifier.localSize);
3931 }
3932 else if (qualifierType == "local_size_y")
3933 {
3934 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3935 &qualifier.localSize);
3936 }
3937 else if (qualifierType == "local_size_z")
3938 {
3939 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3940 &qualifier.localSize);
3941 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003942 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003943 mShaderType == GL_VERTEX_SHADER)
3944 {
3945 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3946 }
Martin Radev802abe02016-08-04 17:48:32 +03003947 else
3948 {
3949 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003950 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003951
Jamie Madilla5efff92013-06-06 11:56:47 -04003952 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003953}
3954
Olli Etuaho613b9592016-09-05 12:05:53 +03003955TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3956{
3957 return new TTypeQualifierBuilder(
3958 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3959 mShaderVersion);
3960}
3961
Olli Etuahocce89652017-06-19 16:04:09 +03003962TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3963 const TSourceLoc &loc)
3964{
3965 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3966 return new TStorageQualifierWrapper(qualifier, loc);
3967}
3968
3969TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3970{
3971 if (getShaderType() == GL_VERTEX_SHADER)
3972 {
3973 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3974 }
3975 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3976}
3977
3978TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3979{
3980 if (declaringFunction())
3981 {
3982 return new TStorageQualifierWrapper(EvqIn, loc);
3983 }
3984 if (getShaderType() == GL_FRAGMENT_SHADER)
3985 {
3986 if (mShaderVersion < 300)
3987 {
3988 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3989 }
3990 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3991 }
3992 if (getShaderType() == GL_VERTEX_SHADER)
3993 {
3994 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3995 {
3996 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3997 }
3998 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3999 }
4000 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4001}
4002
4003TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4004{
4005 if (declaringFunction())
4006 {
4007 return new TStorageQualifierWrapper(EvqOut, loc);
4008 }
4009 if (mShaderVersion < 300)
4010 {
4011 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4012 }
4013 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
4014 {
4015 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
4016 }
4017 if (getShaderType() == GL_VERTEX_SHADER)
4018 {
4019 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4020 }
4021 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4022}
4023
4024TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4025{
4026 if (!declaringFunction())
4027 {
4028 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4029 }
4030 return new TStorageQualifierWrapper(EvqInOut, loc);
4031}
4032
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004034 TLayoutQualifier rightQualifier,
4035 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004036{
Martin Radevc28888b2016-07-22 15:27:42 +03004037 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004038 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004039}
4040
Olli Etuahocce89652017-06-19 16:04:09 +03004041TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4042{
4043 checkIsNotReserved(loc, *identifier);
4044 TType *type = new TType(EbtVoid, EbpUndefined);
4045 return new TField(type, identifier, loc);
4046}
4047
4048TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4049 const TSourceLoc &loc,
4050 TIntermTyped *arraySize,
4051 const TSourceLoc &arraySizeLoc)
4052{
4053 checkIsNotReserved(loc, *identifier);
4054
4055 TType *type = new TType(EbtVoid, EbpUndefined);
4056 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
4057 type->setArraySize(size);
4058
4059 return new TField(type, identifier, loc);
4060}
4061
Olli Etuaho4de340a2016-12-16 09:32:03 +00004062TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4063 const TFieldList *newlyAddedFields,
4064 const TSourceLoc &location)
4065{
4066 for (TField *field : *newlyAddedFields)
4067 {
4068 for (TField *oldField : *processedFields)
4069 {
4070 if (oldField->name() == field->name())
4071 {
4072 error(location, "duplicate field name in structure", field->name().c_str());
4073 }
4074 }
4075 processedFields->push_back(field);
4076 }
4077 return processedFields;
4078}
4079
Martin Radev70866b82016-07-22 15:27:42 +03004080TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4081 const TTypeQualifierBuilder &typeQualifierBuilder,
4082 TPublicType *typeSpecifier,
4083 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004084{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004085 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004086
Martin Radev70866b82016-07-22 15:27:42 +03004087 typeSpecifier->qualifier = typeQualifier.qualifier;
4088 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004089 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004090 typeSpecifier->invariant = typeQualifier.invariant;
4091 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304092 {
Martin Radev70866b82016-07-22 15:27:42 +03004093 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004094 }
Martin Radev70866b82016-07-22 15:27:42 +03004095 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004096}
4097
Jamie Madillb98c3a82015-07-23 14:26:04 -04004098TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
4099 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100{
Martin Radev4a9cd802016-09-01 16:51:51 +03004101 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4102 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004103
Martin Radev4a9cd802016-09-01 16:51:51 +03004104 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105
Martin Radev4a9cd802016-09-01 16:51:51 +03004106 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004107
Arun Patole7e7e68d2015-05-22 12:02:25 +05304108 for (unsigned int i = 0; i < fieldList->size(); ++i)
4109 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004110 //
4111 // Careful not to replace already known aspects of type, like array-ness
4112 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304113 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03004114 type->setBasicType(typeSpecifier.getBasicType());
4115 type->setPrimarySize(typeSpecifier.getPrimarySize());
4116 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004117 type->setPrecision(typeSpecifier.precision);
4118 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004119 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004120 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004121 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004122
4123 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304124 if (type->isArray())
4125 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004126 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004127 }
4128 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004129 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004130 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304131 {
Olli Etuaho0f684632017-07-13 12:42:15 +03004132 type->setStruct(typeSpecifier.getUserDef());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004133 }
4134
Martin Radev4a9cd802016-09-01 16:51:51 +03004135 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004136 }
4137
Jamie Madill98493dd2013-07-08 14:39:03 -04004138 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004139}
4140
Martin Radev4a9cd802016-09-01 16:51:51 +03004141TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4142 const TSourceLoc &nameLine,
4143 const TString *structName,
4144 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004145{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004146 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004147
Jamie Madill9b820842015-02-12 10:40:10 -05004148 // Store a bool in the struct if we're at global scope, to allow us to
4149 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004150 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004151
Jamie Madill98493dd2013-07-08 14:39:03 -04004152 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004154 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004155 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304156 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004157 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158 }
4159 }
4160
4161 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004162 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004163 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004164 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004165 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004166 switch (qualifier)
4167 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004168 case EvqGlobal:
4169 case EvqTemporary:
4170 break;
4171 default:
4172 error(field.line(), "invalid qualifier on struct member",
4173 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004174 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004175 }
Martin Radev70866b82016-07-22 15:27:42 +03004176 if (field.type()->isInvariant())
4177 {
4178 error(field.line(), "invalid qualifier on struct member", "invariant");
4179 }
jchen104cdac9e2017-05-08 11:01:20 +08004180 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4181 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004182 {
4183 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4184 }
4185
Olli Etuaho43364892017-02-13 16:00:12 +00004186 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4187
4188 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004189
4190 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004191 }
4192
Martin Radev4a9cd802016-09-01 16:51:51 +03004193 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004194 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004195 exitStructDeclaration();
4196
Martin Radev4a9cd802016-09-01 16:51:51 +03004197 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004198}
4199
Jamie Madillb98c3a82015-07-23 14:26:04 -04004200TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004201 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004202 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004203{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004204 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004205 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004206 init->isVector())
4207 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004208 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4209 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004210 return nullptr;
4211 }
4212
Olli Etuahoac5274d2015-02-20 10:19:08 +02004213 if (statementList)
4214 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004215 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004216 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004217 return nullptr;
4218 }
4219 }
4220
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004221 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4222 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004223 return node;
4224}
4225
4226TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4227{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004228 if (mSwitchNestingLevel == 0)
4229 {
4230 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004231 return nullptr;
4232 }
4233 if (condition == nullptr)
4234 {
4235 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004236 return nullptr;
4237 }
4238 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004239 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004240 {
4241 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004242 }
4243 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004244 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4245 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4246 // fold in case labels.
4247 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004248 {
4249 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004250 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004251 TIntermCase *node = new TIntermCase(condition);
4252 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004253 return node;
4254}
4255
4256TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4257{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004258 if (mSwitchNestingLevel == 0)
4259 {
4260 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004261 return nullptr;
4262 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004263 TIntermCase *node = new TIntermCase(nullptr);
4264 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004265 return node;
4266}
4267
Jamie Madillb98c3a82015-07-23 14:26:04 -04004268TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4269 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004270 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004271{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004272 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004273
4274 switch (op)
4275 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004276 case EOpLogicalNot:
4277 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4278 child->isVector())
4279 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004280 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004281 return nullptr;
4282 }
4283 break;
4284 case EOpBitwiseNot:
4285 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4286 child->isMatrix() || child->isArray())
4287 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004288 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004289 return nullptr;
4290 }
4291 break;
4292 case EOpPostIncrement:
4293 case EOpPreIncrement:
4294 case EOpPostDecrement:
4295 case EOpPreDecrement:
4296 case EOpNegative:
4297 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004298 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4299 child->getBasicType() == EbtBool || child->isArray() ||
4300 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004301 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004302 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004303 return nullptr;
4304 }
4305 // Operators for built-ins are already type checked against their prototype.
4306 default:
4307 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004308 }
4309
Jiajia Qinbc585152017-06-23 15:42:17 +08004310 if (child->getMemoryQualifier().writeonly)
4311 {
4312 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4313 return nullptr;
4314 }
4315
Olli Etuahof119a262016-08-19 15:54:22 +03004316 TIntermUnary *node = new TIntermUnary(op, child);
4317 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004318
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004319 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004320}
4321
Olli Etuaho09b22472015-02-11 11:47:26 +02004322TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4323{
Olli Etuahocce89652017-06-19 16:04:09 +03004324 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004325 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004326 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004327 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004328 return child;
4329 }
4330 return node;
4331}
4332
Jamie Madillb98c3a82015-07-23 14:26:04 -04004333TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4334 TIntermTyped *child,
4335 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004336{
Olli Etuaho856c4972016-08-08 11:38:39 +03004337 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004338 return addUnaryMath(op, child, loc);
4339}
4340
Jamie Madillb98c3a82015-07-23 14:26:04 -04004341bool TParseContext::binaryOpCommonCheck(TOperator op,
4342 TIntermTyped *left,
4343 TIntermTyped *right,
4344 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004345{
jchen10b4cf5652017-05-05 18:51:17 +08004346 // Check opaque types are not allowed to be operands in expressions other than array indexing
4347 // and structure member selection.
4348 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4349 {
4350 switch (op)
4351 {
4352 case EOpIndexDirect:
4353 case EOpIndexIndirect:
4354 break;
4355 case EOpIndexDirectStruct:
4356 UNREACHABLE();
4357
4358 default:
4359 error(loc, "Invalid operation for variables with an opaque type",
4360 GetOperatorString(op));
4361 return false;
4362 }
4363 }
jchen10cc2a10e2017-05-03 14:05:12 +08004364
Jiajia Qinbc585152017-06-23 15:42:17 +08004365 if (right->getMemoryQualifier().writeonly)
4366 {
4367 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4368 return false;
4369 }
4370
4371 if (left->getMemoryQualifier().writeonly)
4372 {
4373 switch (op)
4374 {
4375 case EOpAssign:
4376 case EOpInitialize:
4377 case EOpIndexDirect:
4378 case EOpIndexIndirect:
4379 case EOpIndexDirectStruct:
4380 case EOpIndexDirectInterfaceBlock:
4381 break;
4382 default:
4383 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4384 return false;
4385 }
4386 }
4387
Olli Etuaho244be012016-08-18 15:26:02 +03004388 if (left->getType().getStruct() || right->getType().getStruct())
4389 {
4390 switch (op)
4391 {
4392 case EOpIndexDirectStruct:
4393 ASSERT(left->getType().getStruct());
4394 break;
4395 case EOpEqual:
4396 case EOpNotEqual:
4397 case EOpAssign:
4398 case EOpInitialize:
4399 if (left->getType() != right->getType())
4400 {
4401 return false;
4402 }
4403 break;
4404 default:
4405 error(loc, "Invalid operation for structs", GetOperatorString(op));
4406 return false;
4407 }
4408 }
4409
Olli Etuaho94050052017-05-08 14:17:44 +03004410 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4411 {
4412 switch (op)
4413 {
4414 case EOpIndexDirectInterfaceBlock:
4415 ASSERT(left->getType().getInterfaceBlock());
4416 break;
4417 default:
4418 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4419 return false;
4420 }
4421 }
4422
Olli Etuahod6b14282015-03-17 14:31:35 +02004423 if (left->isArray() || right->isArray())
4424 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004425 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004426 {
4427 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4428 return false;
4429 }
4430
4431 if (left->isArray() != right->isArray())
4432 {
4433 error(loc, "array / non-array mismatch", GetOperatorString(op));
4434 return false;
4435 }
4436
4437 switch (op)
4438 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004439 case EOpEqual:
4440 case EOpNotEqual:
4441 case EOpAssign:
4442 case EOpInitialize:
4443 break;
4444 default:
4445 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4446 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004447 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004448 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004449 if (left->getArraySize() != right->getArraySize())
4450 {
4451 error(loc, "array size mismatch", GetOperatorString(op));
4452 return false;
4453 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004454 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004455
4456 // Check ops which require integer / ivec parameters
4457 bool isBitShift = false;
4458 switch (op)
4459 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004460 case EOpBitShiftLeft:
4461 case EOpBitShiftRight:
4462 case EOpBitShiftLeftAssign:
4463 case EOpBitShiftRightAssign:
4464 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4465 // check that the basic type is an integer type.
4466 isBitShift = true;
4467 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4468 {
4469 return false;
4470 }
4471 break;
4472 case EOpBitwiseAnd:
4473 case EOpBitwiseXor:
4474 case EOpBitwiseOr:
4475 case EOpBitwiseAndAssign:
4476 case EOpBitwiseXorAssign:
4477 case EOpBitwiseOrAssign:
4478 // It is enough to check the type of only one operand, since later it
4479 // is checked that the operand types match.
4480 if (!IsInteger(left->getBasicType()))
4481 {
4482 return false;
4483 }
4484 break;
4485 default:
4486 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004487 }
4488
4489 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4490 // So the basic type should usually match.
4491 if (!isBitShift && left->getBasicType() != right->getBasicType())
4492 {
4493 return false;
4494 }
4495
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004496 // Check that:
4497 // 1. Type sizes match exactly on ops that require that.
4498 // 2. Restrictions for structs that contain arrays or samplers are respected.
4499 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004500 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004501 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004502 case EOpAssign:
4503 case EOpInitialize:
4504 case EOpEqual:
4505 case EOpNotEqual:
4506 // ESSL 1.00 sections 5.7, 5.8, 5.9
4507 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4508 {
4509 error(loc, "undefined operation for structs containing arrays",
4510 GetOperatorString(op));
4511 return false;
4512 }
4513 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4514 // we interpret the spec so that this extends to structs containing samplers,
4515 // similarly to ESSL 1.00 spec.
4516 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4517 left->getType().isStructureContainingSamplers())
4518 {
4519 error(loc, "undefined operation for structs containing samplers",
4520 GetOperatorString(op));
4521 return false;
4522 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004523
Olli Etuahoe1805592017-01-02 16:41:20 +00004524 if ((left->getNominalSize() != right->getNominalSize()) ||
4525 (left->getSecondarySize() != right->getSecondarySize()))
4526 {
4527 error(loc, "dimension mismatch", GetOperatorString(op));
4528 return false;
4529 }
4530 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004531 case EOpLessThan:
4532 case EOpGreaterThan:
4533 case EOpLessThanEqual:
4534 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004535 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004536 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004537 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004538 return false;
4539 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004540 break;
4541 case EOpAdd:
4542 case EOpSub:
4543 case EOpDiv:
4544 case EOpIMod:
4545 case EOpBitShiftLeft:
4546 case EOpBitShiftRight:
4547 case EOpBitwiseAnd:
4548 case EOpBitwiseXor:
4549 case EOpBitwiseOr:
4550 case EOpAddAssign:
4551 case EOpSubAssign:
4552 case EOpDivAssign:
4553 case EOpIModAssign:
4554 case EOpBitShiftLeftAssign:
4555 case EOpBitShiftRightAssign:
4556 case EOpBitwiseAndAssign:
4557 case EOpBitwiseXorAssign:
4558 case EOpBitwiseOrAssign:
4559 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4560 {
4561 return false;
4562 }
4563
4564 // Are the sizes compatible?
4565 if (left->getNominalSize() != right->getNominalSize() ||
4566 left->getSecondarySize() != right->getSecondarySize())
4567 {
4568 // If the nominal sizes of operands do not match:
4569 // One of them must be a scalar.
4570 if (!left->isScalar() && !right->isScalar())
4571 return false;
4572
4573 // In the case of compound assignment other than multiply-assign,
4574 // the right side needs to be a scalar. Otherwise a vector/matrix
4575 // would be assigned to a scalar. A scalar can't be shifted by a
4576 // vector either.
4577 if (!right->isScalar() &&
4578 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4579 return false;
4580 }
4581 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004582 default:
4583 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004584 }
4585
Olli Etuahod6b14282015-03-17 14:31:35 +02004586 return true;
4587}
4588
Olli Etuaho1dded802016-08-18 18:13:13 +03004589bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4590 const TType &left,
4591 const TType &right)
4592{
4593 switch (op)
4594 {
4595 case EOpMul:
4596 case EOpMulAssign:
4597 return left.getNominalSize() == right.getNominalSize() &&
4598 left.getSecondarySize() == right.getSecondarySize();
4599 case EOpVectorTimesScalar:
4600 return true;
4601 case EOpVectorTimesScalarAssign:
4602 ASSERT(!left.isMatrix() && !right.isMatrix());
4603 return left.isVector() && !right.isVector();
4604 case EOpVectorTimesMatrix:
4605 return left.getNominalSize() == right.getRows();
4606 case EOpVectorTimesMatrixAssign:
4607 ASSERT(!left.isMatrix() && right.isMatrix());
4608 return left.isVector() && left.getNominalSize() == right.getRows() &&
4609 left.getNominalSize() == right.getCols();
4610 case EOpMatrixTimesVector:
4611 return left.getCols() == right.getNominalSize();
4612 case EOpMatrixTimesScalar:
4613 return true;
4614 case EOpMatrixTimesScalarAssign:
4615 ASSERT(left.isMatrix() && !right.isMatrix());
4616 return !right.isVector();
4617 case EOpMatrixTimesMatrix:
4618 return left.getCols() == right.getRows();
4619 case EOpMatrixTimesMatrixAssign:
4620 ASSERT(left.isMatrix() && right.isMatrix());
4621 // We need to check two things:
4622 // 1. The matrix multiplication step is valid.
4623 // 2. The result will have the same number of columns as the lvalue.
4624 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4625
4626 default:
4627 UNREACHABLE();
4628 return false;
4629 }
4630}
4631
Jamie Madillb98c3a82015-07-23 14:26:04 -04004632TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4633 TIntermTyped *left,
4634 TIntermTyped *right,
4635 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004636{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004637 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004638 return nullptr;
4639
Olli Etuahofc1806e2015-03-17 13:03:11 +02004640 switch (op)
4641 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004642 case EOpEqual:
4643 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004644 case EOpLessThan:
4645 case EOpGreaterThan:
4646 case EOpLessThanEqual:
4647 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004648 break;
4649 case EOpLogicalOr:
4650 case EOpLogicalXor:
4651 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004652 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4653 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004654 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004655 {
4656 return nullptr;
4657 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004658 // Basic types matching should have been already checked.
4659 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660 break;
4661 case EOpAdd:
4662 case EOpSub:
4663 case EOpDiv:
4664 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004665 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4666 !right->getType().getStruct());
4667 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004668 {
4669 return nullptr;
4670 }
4671 break;
4672 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004673 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4674 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004675 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004676 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004677 {
4678 return nullptr;
4679 }
4680 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004681 default:
4682 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004683 }
4684
Olli Etuaho1dded802016-08-18 18:13:13 +03004685 if (op == EOpMul)
4686 {
4687 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4688 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4689 {
4690 return nullptr;
4691 }
4692 }
4693
Olli Etuaho3fdec912016-08-18 15:08:06 +03004694 TIntermBinary *node = new TIntermBinary(op, left, right);
4695 node->setLine(loc);
4696
Olli Etuaho3fdec912016-08-18 15:08:06 +03004697 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004698 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004699}
4700
Jamie Madillb98c3a82015-07-23 14:26:04 -04004701TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4702 TIntermTyped *left,
4703 TIntermTyped *right,
4704 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004705{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004706 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004707 if (node == 0)
4708 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004709 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4710 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004711 return left;
4712 }
4713 return node;
4714}
4715
Jamie Madillb98c3a82015-07-23 14:26:04 -04004716TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4717 TIntermTyped *left,
4718 TIntermTyped *right,
4719 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004720{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004721 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004722 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004724 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4725 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03004726 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03004727 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004728 }
4729 return node;
4730}
4731
Olli Etuaho13389b62016-10-16 11:48:18 +01004732TIntermBinary *TParseContext::createAssign(TOperator op,
4733 TIntermTyped *left,
4734 TIntermTyped *right,
4735 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004736{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004737 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004738 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004739 if (op == EOpMulAssign)
4740 {
4741 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4742 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4743 {
4744 return nullptr;
4745 }
4746 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004747 TIntermBinary *node = new TIntermBinary(op, left, right);
4748 node->setLine(loc);
4749
Olli Etuaho3fdec912016-08-18 15:08:06 +03004750 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004751 }
4752 return nullptr;
4753}
4754
Jamie Madillb98c3a82015-07-23 14:26:04 -04004755TIntermTyped *TParseContext::addAssign(TOperator op,
4756 TIntermTyped *left,
4757 TIntermTyped *right,
4758 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004759{
Olli Etuahocce89652017-06-19 16:04:09 +03004760 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004761 TIntermTyped *node = createAssign(op, left, right, loc);
4762 if (node == nullptr)
4763 {
4764 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004765 return left;
4766 }
4767 return node;
4768}
4769
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004770TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4771 TIntermTyped *right,
4772 const TSourceLoc &loc)
4773{
Corentin Wallez0d959252016-07-12 17:26:32 -04004774 // WebGL2 section 5.26, the following results in an error:
4775 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004776 if (mShaderSpec == SH_WEBGL2_SPEC &&
4777 (left->isArray() || left->getBasicType() == EbtVoid ||
4778 left->getType().isStructureContainingArrays() || right->isArray() ||
4779 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004780 {
4781 error(loc,
4782 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4783 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004784 }
4785
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004786 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4787 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4788 commaNode->getTypePointer()->setQualifier(resultQualifier);
4789 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004790}
4791
Olli Etuaho49300862015-02-20 14:54:49 +02004792TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4793{
4794 switch (op)
4795 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004796 case EOpContinue:
4797 if (mLoopNestingLevel <= 0)
4798 {
4799 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004800 }
4801 break;
4802 case EOpBreak:
4803 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4804 {
4805 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004806 }
4807 break;
4808 case EOpReturn:
4809 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4810 {
4811 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004812 }
4813 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004814 case EOpKill:
4815 if (mShaderType != GL_FRAGMENT_SHADER)
4816 {
4817 error(loc, "discard supported in fragment shaders only", "discard");
4818 }
4819 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004820 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004821 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004822 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004823 }
Olli Etuahocce89652017-06-19 16:04:09 +03004824 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004825}
4826
Jamie Madillb98c3a82015-07-23 14:26:04 -04004827TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004828 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004829 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004830{
Olli Etuahocce89652017-06-19 16:04:09 +03004831 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004832 {
Olli Etuahocce89652017-06-19 16:04:09 +03004833 ASSERT(op == EOpReturn);
4834 mFunctionReturnsValue = true;
4835 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4836 {
4837 error(loc, "void function cannot return a value", "return");
4838 }
4839 else if (*mCurrentFunctionType != expression->getType())
4840 {
4841 error(loc, "function return is not matching type:", "return");
4842 }
Olli Etuaho49300862015-02-20 14:54:49 +02004843 }
Olli Etuahocce89652017-06-19 16:04:09 +03004844 TIntermBranch *node = new TIntermBranch(op, expression);
4845 node->setLine(loc);
4846 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004847}
4848
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004849void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4850{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004851 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004852 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004853 TIntermNode *offset = nullptr;
4854 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004855 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4856 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4857 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004858 {
4859 offset = arguments->back();
4860 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004861 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004862 {
4863 // A bias parameter might follow the offset parameter.
4864 ASSERT(arguments->size() >= 3);
4865 offset = (*arguments)[2];
4866 }
4867 if (offset != nullptr)
4868 {
4869 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4870 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4871 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004872 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004873 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004874 }
4875 else
4876 {
4877 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4878 size_t size = offsetConstantUnion->getType().getObjectSize();
4879 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4880 for (size_t i = 0u; i < size; ++i)
4881 {
4882 int offsetValue = values[i].getIConst();
4883 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4884 {
4885 std::stringstream tokenStream;
4886 tokenStream << offsetValue;
4887 std::string token = tokenStream.str();
4888 error(offset->getLine(), "Texture offset value out of valid range",
4889 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004890 }
4891 }
4892 }
4893 }
4894}
4895
Martin Radev2cc85b32016-08-05 16:22:53 +03004896// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4897void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4898{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004899 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004900 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4901
4902 if (name.compare(0, 5, "image") == 0)
4903 {
4904 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004905 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004906
Olli Etuaho485eefd2017-02-14 17:40:06 +00004907 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004908
4909 if (name.compare(5, 5, "Store") == 0)
4910 {
4911 if (memoryQualifier.readonly)
4912 {
4913 error(imageNode->getLine(),
4914 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004915 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004916 }
4917 }
4918 else if (name.compare(5, 4, "Load") == 0)
4919 {
4920 if (memoryQualifier.writeonly)
4921 {
4922 error(imageNode->getLine(),
4923 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004924 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004925 }
4926 }
4927 }
4928}
4929
4930// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4931void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4932 const TFunction *functionDefinition,
4933 const TIntermAggregate *functionCall)
4934{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004935 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004936
4937 const TIntermSequence &arguments = *functionCall->getSequence();
4938
4939 ASSERT(functionDefinition->getParamCount() == arguments.size());
4940
4941 for (size_t i = 0; i < arguments.size(); ++i)
4942 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004943 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4944 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004945 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4946 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4947
4948 if (IsImage(functionArgumentType.getBasicType()))
4949 {
4950 const TMemoryQualifier &functionArgumentMemoryQualifier =
4951 functionArgumentType.getMemoryQualifier();
4952 const TMemoryQualifier &functionParameterMemoryQualifier =
4953 functionParameterType.getMemoryQualifier();
4954 if (functionArgumentMemoryQualifier.readonly &&
4955 !functionParameterMemoryQualifier.readonly)
4956 {
4957 error(functionCall->getLine(),
4958 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004959 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004960 }
4961
4962 if (functionArgumentMemoryQualifier.writeonly &&
4963 !functionParameterMemoryQualifier.writeonly)
4964 {
4965 error(functionCall->getLine(),
4966 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004967 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004968 }
Martin Radev049edfa2016-11-11 14:35:37 +02004969
4970 if (functionArgumentMemoryQualifier.coherent &&
4971 !functionParameterMemoryQualifier.coherent)
4972 {
4973 error(functionCall->getLine(),
4974 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004975 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004976 }
4977
4978 if (functionArgumentMemoryQualifier.volatileQualifier &&
4979 !functionParameterMemoryQualifier.volatileQualifier)
4980 {
4981 error(functionCall->getLine(),
4982 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004983 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004984 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004985 }
4986 }
4987}
4988
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004989TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004990{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004991 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004992}
4993
4994TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004995 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004996 TIntermNode *thisNode,
4997 const TSourceLoc &loc)
4998{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004999 if (thisNode != nullptr)
5000 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005001 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005002 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005003
5004 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005005 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005006 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005007 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005008 }
5009 else
5010 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005011 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005012 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5013 }
5014}
5015
5016TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5017 TIntermSequence *arguments,
5018 TIntermNode *thisNode,
5019 const TSourceLoc &loc)
5020{
5021 TConstantUnion *unionArray = new TConstantUnion[1];
5022 int arraySize = 0;
5023 TIntermTyped *typedThis = thisNode->getAsTyped();
5024 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5025 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5026 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5027 // So accessing fnCall->getName() below is safe.
5028 if (fnCall->getName() != "length")
5029 {
5030 error(loc, "invalid method", fnCall->getName().c_str());
5031 }
5032 else if (!arguments->empty())
5033 {
5034 error(loc, "method takes no parameters", "length");
5035 }
5036 else if (typedThis == nullptr || !typedThis->isArray())
5037 {
5038 error(loc, "length can only be called on arrays", "length");
5039 }
5040 else
5041 {
5042 arraySize = typedThis->getArraySize();
5043 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005044 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005045 // This code path can be hit with expressions like these:
5046 // (a = b).length()
5047 // (func()).length()
5048 // (int[3](0, 1, 2)).length()
5049 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5050 // expression.
5051 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5052 // spec section 5.9 which allows "An array, vector or matrix expression with the
5053 // length method applied".
5054 error(loc, "length can only be called on array names, not on array expressions",
5055 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005056 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005057 }
5058 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005059 TIntermConstantUnion *node =
5060 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5061 node->setLine(loc);
5062 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005063}
5064
5065TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5066 TIntermSequence *arguments,
5067 const TSourceLoc &loc)
5068{
5069 // First find by unmangled name to check whether the function name has been
5070 // hidden by a variable name or struct typename.
5071 // If a function is found, check for one with a matching argument list.
5072 bool builtIn;
5073 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5074 if (symbol != nullptr && !symbol->isFunction())
5075 {
5076 error(loc, "function name expected", fnCall->getName().c_str());
5077 }
5078 else
5079 {
5080 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5081 mShaderVersion, &builtIn);
5082 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005083 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005084 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5085 }
5086 else
5087 {
5088 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005089 //
5090 // A declared function.
5091 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005092 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005093 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005094 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005095 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005096 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005097 if (builtIn && op != EOpNull)
5098 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005099 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005100 if (fnCandidate->getParamCount() == 1)
5101 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005102 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005103 TIntermNode *unaryParamNode = arguments->front();
5104 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005105 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005106 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005107 }
5108 else
5109 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005110 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005111 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005112 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005113
5114 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005115 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305116
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005117 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305118 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005119 // See if we can constant fold a built-in. Note that this may be possible
5120 // even if it is not const-qualified.
5121 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305122 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005123 else
5124 {
5125 return callNode;
5126 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005127 }
5128 }
5129 else
5130 {
5131 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005132 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005133
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005134 // If builtIn == false, the function is user defined - could be an overloaded
5135 // built-in as well.
5136 // if builtIn == true, it's a builtIn function with no op associated with it.
5137 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005138 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005139 {
Olli Etuahofe486322017-03-21 09:30:54 +00005140 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005141 checkTextureOffsetConst(callNode);
5142 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005143 }
5144 else
5145 {
Olli Etuahofe486322017-03-21 09:30:54 +00005146 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005147 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005148 }
5149
Jiajia Qinbc585152017-06-23 15:42:17 +08005150 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005151
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005152 callNode->setLine(loc);
5153
5154 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005155 }
5156 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005157 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005158
5159 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005160 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005161}
5162
Jamie Madillb98c3a82015-07-23 14:26:04 -04005163TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005164 TIntermTyped *trueExpression,
5165 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005166 const TSourceLoc &loc)
5167{
Olli Etuaho56229f12017-07-10 14:16:33 +03005168 if (!checkIsScalarBool(loc, cond))
5169 {
5170 return falseExpression;
5171 }
Olli Etuaho52901742015-04-15 13:42:45 +03005172
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005173 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005174 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005175 std::stringstream reasonStream;
5176 reasonStream << "mismatching ternary operator operand types '"
5177 << trueExpression->getCompleteString() << " and '"
5178 << falseExpression->getCompleteString() << "'";
5179 std::string reason = reasonStream.str();
5180 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005181 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005182 }
Olli Etuahode318b22016-10-25 16:18:25 +01005183 if (IsOpaqueType(trueExpression->getBasicType()))
5184 {
5185 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005186 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005187 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5188 // Note that structs containing opaque types don't need to be checked as structs are
5189 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005190 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005191 return falseExpression;
5192 }
5193
Jiajia Qinbc585152017-06-23 15:42:17 +08005194 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5195 falseExpression->getMemoryQualifier().writeonly)
5196 {
5197 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5198 return falseExpression;
5199 }
5200
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005201 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005202 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005203 // ESSL 3.00.6 section 5.7:
5204 // Ternary operator support is optional for arrays. No certainty that it works across all
5205 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5206 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005207 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005208 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005209 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005210 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005211 }
Olli Etuaho94050052017-05-08 14:17:44 +03005212 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5213 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005214 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005215 return falseExpression;
5216 }
5217
Corentin Wallez0d959252016-07-12 17:26:32 -04005218 // WebGL2 section 5.26, the following results in an error:
5219 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005220 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005221 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005222 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005223 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005224 }
5225
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005226 // Note that the node resulting from here can be a constant union without being qualified as
5227 // constant.
5228 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5229 node->setLine(loc);
5230
5231 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005232}
Olli Etuaho49300862015-02-20 14:54:49 +02005233
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005234//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005235// Parse an array of strings using yyparse.
5236//
5237// Returns 0 for success.
5238//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005239int PaParseStrings(size_t count,
5240 const char *const string[],
5241 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305242 TParseContext *context)
5243{
Yunchao He4f285442017-04-21 12:15:49 +08005244 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005245 return 1;
5246
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005247 if (glslang_initialize(context))
5248 return 1;
5249
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005250 int error = glslang_scan(count, string, length, context);
5251 if (!error)
5252 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005253
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005254 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005255
alokp@chromium.org6b495712012-06-29 00:06:58 +00005256 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005257}
Jamie Madill45bcc782016-11-07 13:58:48 -05005258
5259} // namespace sh