blob: 7c7e3601ba91b0b06f285203b6d0e92c1b9dfb73 [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 Etuahob0c645e2015-05-12 14:25:36 +030015#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080016#include "compiler/translator/ValidateSwitch.h"
17#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030018#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000019
Jamie Madill45bcc782016-11-07 13:58:48 -050020namespace sh
21{
22
alokp@chromium.org8b851c62012-06-15 16:25:11 +000023///////////////////////////////////////////////////////////////////////
24//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025// Sub- vector and matrix fields
26//
27////////////////////////////////////////////////////////////////////////
28
Martin Radev2cc85b32016-08-05 16:22:53 +030029namespace
30{
31
32const int kWebGLMaxStructNesting = 4;
33
34bool ContainsSampler(const TType &type)
35{
36 if (IsSampler(type.getBasicType()))
37 return true;
38
jchen10cc2a10e2017-05-03 14:05:12 +080039 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030040 {
41 const TFieldList &fields = type.getStruct()->fields();
42 for (unsigned int i = 0; i < fields.size(); ++i)
43 {
44 if (ContainsSampler(*fields[i]->type()))
45 return true;
46 }
47 }
48
49 return false;
50}
51
Olli Etuaho485eefd2017-02-14 17:40:06 +000052// Get a token from an image argument to use as an error message token.
53const char *GetImageArgumentToken(TIntermTyped *imageNode)
54{
55 ASSERT(IsImage(imageNode->getBasicType()));
56 while (imageNode->getAsBinaryNode() &&
57 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
58 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
59 {
60 imageNode = imageNode->getAsBinaryNode()->getLeft();
61 }
62 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
63 if (imageSymbol)
64 {
65 return imageSymbol->getSymbol().c_str();
66 }
67 return "image";
68}
69
Olli Etuahocce89652017-06-19 16:04:09 +030070bool CanSetDefaultPrecisionOnType(const TPublicType &type)
71{
72 if (!SupportsPrecision(type.getBasicType()))
73 {
74 return false;
75 }
76 if (type.getBasicType() == EbtUInt)
77 {
78 // ESSL 3.00.4 section 4.5.4
79 return false;
80 }
81 if (type.isAggregate())
82 {
83 // Not allowed to set for aggregate types
84 return false;
85 }
86 return true;
87}
88
Martin Radev2cc85b32016-08-05 16:22:53 +030089} // namespace
90
jchen104cdac9e2017-05-08 11:01:20 +080091// This tracks each binding point's current default offset for inheritance of subsequent
92// variables using the same binding, and keeps offsets unique and non overlapping.
93// See GLSL ES 3.1, section 4.4.6.
94class TParseContext::AtomicCounterBindingState
95{
96 public:
97 AtomicCounterBindingState() : mDefaultOffset(0) {}
98 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
99 // newly inserted span.
100 int insertSpan(int start, size_t length)
101 {
102 gl::RangeI newSpan(start, start + static_cast<int>(length));
103 for (const auto &span : mSpans)
104 {
105 if (newSpan.intersects(span))
106 {
107 return -1;
108 }
109 }
110 mSpans.push_back(newSpan);
111 mDefaultOffset = newSpan.high();
112 return start;
113 }
114 // Inserts a new span starting from the default offset.
115 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
116 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
117
118 private:
119 int mDefaultOffset;
120 std::vector<gl::RangeI> mSpans;
121};
122
Jamie Madillacb4b812016-11-07 13:50:29 -0500123TParseContext::TParseContext(TSymbolTable &symt,
124 TExtensionBehavior &ext,
125 sh::GLenum type,
126 ShShaderSpec spec,
127 ShCompileOptions options,
128 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000129 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500130 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300131 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300132 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500133 mShaderType(type),
134 mShaderSpec(spec),
135 mCompileOptions(options),
136 mShaderVersion(100),
137 mTreeRoot(nullptr),
138 mLoopNestingLevel(0),
139 mStructNestingLevel(0),
140 mSwitchNestingLevel(0),
141 mCurrentFunctionType(nullptr),
142 mFunctionReturnsValue(false),
143 mChecksPrecisionErrors(checksPrecErrors),
144 mFragmentPrecisionHighOnESSL1(false),
145 mDefaultMatrixPacking(EmpColumnMajor),
146 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000147 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500148 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000149 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500150 mShaderVersion,
151 mShaderType,
152 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000153 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500154 mScanner(nullptr),
155 mUsesFragData(false),
156 mUsesFragColor(false),
157 mUsesSecondaryOutputs(false),
158 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
159 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000160 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500161 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000162 mNumViews(-1),
163 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000164 mMaxImageUnits(resources.MaxImageUnits),
165 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000166 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800167 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800168 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jamie Madillacb4b812016-11-07 13:50:29 -0500169 mDeclaringFunction(false)
170{
171 mComputeShaderLocalSize.fill(-1);
172}
173
jchen104cdac9e2017-05-08 11:01:20 +0800174TParseContext::~TParseContext()
175{
176}
177
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300178bool TParseContext::parseVectorFields(const TSourceLoc &line,
179 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400180 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300181 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300183 ASSERT(fieldOffsets);
184 size_t fieldCount = compString.size();
185 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530186 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000187 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000188 return false;
189 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300190 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191
Jamie Madillb98c3a82015-07-23 14:26:04 -0400192 enum
193 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000194 exyzw,
195 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000196 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000197 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300199 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530200 {
201 switch (compString[i])
202 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400203 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300204 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400205 fieldSet[i] = exyzw;
206 break;
207 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300208 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400209 fieldSet[i] = ergba;
210 break;
211 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300212 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400213 fieldSet[i] = estpq;
214 break;
215 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300216 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400217 fieldSet[i] = exyzw;
218 break;
219 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300220 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400221 fieldSet[i] = ergba;
222 break;
223 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300224 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400225 fieldSet[i] = estpq;
226 break;
227 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300228 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 fieldSet[i] = exyzw;
230 break;
231 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300232 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 fieldSet[i] = ergba;
234 break;
235 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300236 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400237 fieldSet[i] = estpq;
238 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530239
Jamie Madillb98c3a82015-07-23 14:26:04 -0400240 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300241 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400242 fieldSet[i] = exyzw;
243 break;
244 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300245 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 fieldSet[i] = ergba;
247 break;
248 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400250 fieldSet[i] = estpq;
251 break;
252 default:
253 error(line, "illegal vector field selection", compString.c_str());
254 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000255 }
256 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530259 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300260 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400262 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000263 return false;
264 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Arun Patole7e7e68d2015-05-22 12:02:25 +0530266 if (i > 0)
267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 error(line, "illegal - vector component fields not from the same set",
271 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000272 return false;
273 }
274 }
275 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278}
279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280///////////////////////////////////////////////////////////////////////
281//
282// Errors
283//
284////////////////////////////////////////////////////////////////////////
285
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286//
287// Used by flex/bison to output all syntax and parsing errors.
288//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000289void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000291 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292}
293
Olli Etuaho4de340a2016-12-16 09:32:03 +0000294void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530295{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000296 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000297}
298
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200299void TParseContext::outOfRangeError(bool isError,
300 const TSourceLoc &loc,
301 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000302 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200303{
304 if (isError)
305 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000306 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200307 }
308 else
309 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000310 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200311 }
312}
313
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000314//
315// Same error message for all places assignments don't work.
316//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530317void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000319 std::stringstream reasonStream;
320 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
321 std::string reason = reasonStream.str();
322 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323}
324
325//
326// Same error message for all places unary operations don't work.
327//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530328void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000330 std::stringstream reasonStream;
331 reasonStream << "wrong operand type - no operation '" << op
332 << "' exists that takes an operand of type " << operand
333 << " (or there is no acceptable conversion)";
334 std::string reason = reasonStream.str();
335 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336}
337
338//
339// Same error message for all binary operations don't work.
340//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400341void TParseContext::binaryOpError(const TSourceLoc &line,
342 const char *op,
343 TString left,
344 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000346 std::stringstream reasonStream;
347 reasonStream << "wrong operand types - no operation '" << op
348 << "' exists that takes a left-hand operand of type '" << left
349 << "' and a right operand of type '" << right
350 << "' (or there is no acceptable conversion)";
351 std::string reason = reasonStream.str();
352 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
Olli Etuaho856c4972016-08-08 11:38:39 +0300355void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
356 TPrecision precision,
357 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400359 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300360 return;
Martin Radev70866b82016-07-22 15:27:42 +0300361
362 if (precision != EbpUndefined && !SupportsPrecision(type))
363 {
364 error(line, "illegal type for precision qualifier", getBasicString(type));
365 }
366
Olli Etuaho183d7e22015-11-20 15:59:09 +0200367 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530368 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200369 switch (type)
370 {
371 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400372 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300373 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200374 case EbtInt:
375 case EbtUInt:
376 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400377 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300378 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200379 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800380 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200381 {
jchen10cc2a10e2017-05-03 14:05:12 +0800382 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300383 return;
384 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200385 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000386 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000387}
388
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389// Both test and if necessary, spit out an error, to see if the node is really
390// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300391bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500393 TIntermSymbol *symNode = node->getAsSymbolNode();
394 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100395 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
396
397 if (swizzleNode)
398 {
399 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
400 if (ok && swizzleNode->hasDuplicateOffsets())
401 {
402 error(line, " l-value of swizzle cannot have duplicate components", op);
403 return false;
404 }
405 return ok;
406 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407
Arun Patole7e7e68d2015-05-22 12:02:25 +0530408 if (binaryNode)
409 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400410 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530411 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400412 case EOpIndexDirect:
413 case EOpIndexIndirect:
414 case EOpIndexDirectStruct:
415 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300416 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400417 default:
418 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000420 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300421 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000422 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423
jchen10cc2a10e2017-05-03 14:05:12 +0800424 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530425 switch (node->getQualifier())
426 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400427 case EvqConst:
428 message = "can't modify a const";
429 break;
430 case EvqConstReadOnly:
431 message = "can't modify a const";
432 break;
433 case EvqAttribute:
434 message = "can't modify an attribute";
435 break;
436 case EvqFragmentIn:
437 message = "can't modify an input";
438 break;
439 case EvqVertexIn:
440 message = "can't modify an input";
441 break;
442 case EvqUniform:
443 message = "can't modify a uniform";
444 break;
445 case EvqVaryingIn:
446 message = "can't modify a varying";
447 break;
448 case EvqFragCoord:
449 message = "can't modify gl_FragCoord";
450 break;
451 case EvqFrontFacing:
452 message = "can't modify gl_FrontFacing";
453 break;
454 case EvqPointCoord:
455 message = "can't modify gl_PointCoord";
456 break;
Martin Radevb0883602016-08-04 17:48:58 +0300457 case EvqNumWorkGroups:
458 message = "can't modify gl_NumWorkGroups";
459 break;
460 case EvqWorkGroupSize:
461 message = "can't modify gl_WorkGroupSize";
462 break;
463 case EvqWorkGroupID:
464 message = "can't modify gl_WorkGroupID";
465 break;
466 case EvqLocalInvocationID:
467 message = "can't modify gl_LocalInvocationID";
468 break;
469 case EvqGlobalInvocationID:
470 message = "can't modify gl_GlobalInvocationID";
471 break;
472 case EvqLocalInvocationIndex:
473 message = "can't modify gl_LocalInvocationIndex";
474 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300475 case EvqViewIDOVR:
476 message = "can't modify gl_ViewID_OVR";
477 break;
Martin Radev802abe02016-08-04 17:48:32 +0300478 case EvqComputeIn:
479 message = "can't modify work group size variable";
480 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400481 default:
482 //
483 // Type that can't be written to?
484 //
485 if (node->getBasicType() == EbtVoid)
486 {
487 message = "can't modify void";
488 }
jchen10cc2a10e2017-05-03 14:05:12 +0800489 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400490 {
jchen10cc2a10e2017-05-03 14:05:12 +0800491 message = "can't modify a variable with type ";
492 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300493 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495
jchen10cc2a10e2017-05-03 14:05:12 +0800496 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530497 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000498 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499
Olli Etuaho8a176262016-08-16 14:23:01 +0300500 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 //
504 // Everything else is okay, no error.
505 //
jchen10cc2a10e2017-05-03 14:05:12 +0800506 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300507 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000509 //
510 // If we get here, we have an error and a message.
511 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530512 if (symNode)
513 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000514 const char *symbol = symNode->getSymbol().c_str();
515 std::stringstream reasonStream;
516 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
517 std::string reason = reasonStream.str();
518 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000519 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530520 else
521 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000522 std::stringstream reasonStream;
523 reasonStream << "l-value required (" << message << ")";
524 std::string reason = reasonStream.str();
525 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
Olli Etuaho8a176262016-08-16 14:23:01 +0300528 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529}
530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531// Both test, and if necessary spit out an error, to see if the node is really
532// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300533void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534{
Olli Etuaho383b7912016-08-05 11:22:59 +0300535 if (node->getQualifier() != EvqConst)
536 {
537 error(node->getLine(), "constant expression required", "");
538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539}
540
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000541// Both test, and if necessary spit out an error, to see if the node is really
542// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300543void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544{
Olli Etuaho383b7912016-08-05 11:22:59 +0300545 if (!node->isScalarInt())
546 {
547 error(node->getLine(), "integer expression required", token);
548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549}
550
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551// Both test, and if necessary spit out an error, to see if we are currently
552// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800553bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554{
Olli Etuaho856c4972016-08-08 11:38:39 +0300555 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300556 {
557 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800558 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300559 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800560 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561}
562
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300563// ESSL 3.00.5 sections 3.8 and 3.9.
564// If it starts "gl_" or contains two consecutive underscores, it's reserved.
565// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300566bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530568 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300569 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530570 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300571 error(line, reservedErrMsg, "gl_");
572 return false;
573 }
574 if (sh::IsWebGLBasedSpec(mShaderSpec))
575 {
576 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530577 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300578 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300579 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300581 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300583 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300584 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 }
586 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300587 if (identifier.find("__") != TString::npos)
588 {
589 error(line,
590 "identifiers containing two consecutive underscores (__) are reserved as "
591 "possible future keywords",
592 identifier.c_str());
593 return false;
594 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596}
597
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300598// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300599bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800600 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300601 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800603 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530604 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200605 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300606 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200608
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300609 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530610 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300611 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300613 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200614 {
jchen10cc2a10e2017-05-03 14:05:12 +0800615 std::string reason("cannot convert a variable with type ");
616 reason += getBasicString(argTyped->getBasicType());
617 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300618 return false;
619 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200620 if (argTyped->getBasicType() == EbtVoid)
621 {
622 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200624 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
Olli Etuaho856c4972016-08-08 11:38:39 +0300627 if (type.isArray())
628 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300629 // The size of an unsized constructor should already have been determined.
630 ASSERT(!type.isUnsizedArray());
631 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
632 {
633 error(line, "array constructor needs one argument per array element", "constructor");
634 return false;
635 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300636 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
637 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800638 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300639 {
640 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500641 if (argType.isArray())
642 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300643 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500644 return false;
645 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300646 if (!argType.sameElementType(type))
647 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000648 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300650 }
651 }
652 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300653 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300654 {
655 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300656 if (fields.size() != arguments->size())
657 {
658 error(line,
659 "Number of constructor parameters does not match the number of structure fields",
660 "constructor");
661 return false;
662 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300663
664 for (size_t i = 0; i < fields.size(); i++)
665 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800666 if (i >= arguments->size() ||
667 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300668 {
669 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000670 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300671 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300672 }
673 }
674 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300675 else
676 {
677 // We're constructing a scalar, vector, or matrix.
678
679 // Note: It's okay to have too many components available, but not okay to have unused
680 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
681 // there is an extra argument, so 'overFull' will become true.
682
683 size_t size = 0;
684 bool full = false;
685 bool overFull = false;
686 bool matrixArg = false;
687 for (TIntermNode *arg : *arguments)
688 {
689 const TIntermTyped *argTyped = arg->getAsTyped();
690 ASSERT(argTyped != nullptr);
691
Olli Etuaho487b63a2017-05-23 15:55:09 +0300692 if (argTyped->getBasicType() == EbtStruct)
693 {
694 error(line, "a struct cannot be used as a constructor argument for this type",
695 "constructor");
696 return false;
697 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300698 if (argTyped->getType().isArray())
699 {
700 error(line, "constructing from a non-dereferenced array", "constructor");
701 return false;
702 }
703 if (argTyped->getType().isMatrix())
704 {
705 matrixArg = true;
706 }
707
708 size += argTyped->getType().getObjectSize();
709 if (full)
710 {
711 overFull = true;
712 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300713 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300714 {
715 full = true;
716 }
717 }
718
719 if (type.isMatrix() && matrixArg)
720 {
721 if (arguments->size() != 1)
722 {
723 error(line, "constructing matrix from matrix can only take one argument",
724 "constructor");
725 return false;
726 }
727 }
728 else
729 {
730 if (size != 1 && size < type.getObjectSize())
731 {
732 error(line, "not enough data provided for construction", "constructor");
733 return false;
734 }
735 if (overFull)
736 {
737 error(line, "too many arguments", "constructor");
738 return false;
739 }
740 }
741 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300742
Olli Etuaho8a176262016-08-16 14:23:01 +0300743 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
Jamie Madillb98c3a82015-07-23 14:26:04 -0400746// This function checks to see if a void variable has been declared and raise an error message for
747// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748//
749// returns true in case of an error
750//
Olli Etuaho856c4972016-08-08 11:38:39 +0300751bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400752 const TString &identifier,
753 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300755 if (type == EbtVoid)
756 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000757 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300758 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
Olli Etuaho8a176262016-08-16 14:23:01 +0300761 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
Jamie Madillb98c3a82015-07-23 14:26:04 -0400764// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300765// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300766bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530768 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
769 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000770 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300771 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530772 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300773 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774}
775
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300777// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300778void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779{
Martin Radev4a9cd802016-09-01 16:51:51 +0300780 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530781 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000782 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530783 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
jchen10cc2a10e2017-05-03 14:05:12 +0800786bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
787 const TTypeSpecifierNonArray &pType,
788 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530790 if (pType.type == EbtStruct)
791 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300792 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530793 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000794 std::stringstream reasonStream;
795 reasonStream << reason << " (structure contains a sampler)";
796 std::string reasonStr = reasonStream.str();
797 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300798 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 }
jchen10cc2a10e2017-05-03 14:05:12 +0800800 // only samplers need to be checked from structs, since other opaque types can't be struct
801 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300802 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530803 }
jchen10cc2a10e2017-05-03 14:05:12 +0800804 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530805 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000806 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300807 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000808 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809
Olli Etuaho8a176262016-08-16 14:23:01 +0300810 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811}
812
Olli Etuaho856c4972016-08-08 11:38:39 +0300813void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
814 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400815{
816 if (pType.layoutQualifier.location != -1)
817 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400818 error(line, "location must only be specified for a single input or output variable",
819 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400820 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400821}
822
Olli Etuaho856c4972016-08-08 11:38:39 +0300823void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
824 const TLayoutQualifier &layoutQualifier)
825{
826 if (layoutQualifier.location != -1)
827 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000828 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
829 if (mShaderVersion >= 310)
830 {
831 errorMsg =
832 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
833 }
834 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300835 }
836}
837
Martin Radev2cc85b32016-08-05 16:22:53 +0300838void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
839 TQualifier qualifier,
840 const TType &type)
841{
Martin Radev2cc85b32016-08-05 16:22:53 +0300842 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800843 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530844 {
jchen10cc2a10e2017-05-03 14:05:12 +0800845 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300850unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530852 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000853
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200854 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
855 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
856 // fold as array size.
857 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000858 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000859 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300860 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862
Olli Etuaho856c4972016-08-08 11:38:39 +0300863 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400864
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000865 if (constant->getBasicType() == EbtUInt)
866 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300867 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000868 }
869 else
870 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300871 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000872
Olli Etuaho856c4972016-08-08 11:38:39 +0300873 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000874 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400875 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300876 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400878
Olli Etuaho856c4972016-08-08 11:38:39 +0300879 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400880 }
881
Olli Etuaho856c4972016-08-08 11:38:39 +0300882 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400883 {
884 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400886 }
887
888 // The size of arrays is restricted here to prevent issues further down the
889 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
890 // 4096 registers so this should be reasonable even for aggressively optimizable code.
891 const unsigned int sizeLimit = 65536;
892
Olli Etuaho856c4972016-08-08 11:38:39 +0300893 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400894 {
895 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300896 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300898
899 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900}
901
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300903bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
904 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905{
Olli Etuaho8a176262016-08-16 14:23:01 +0300906 if ((elementQualifier.qualifier == EvqAttribute) ||
907 (elementQualifier.qualifier == EvqVertexIn) ||
908 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400910 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300911 TType(elementQualifier).getQualifierString());
912 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916}
917
Olli Etuaho8a176262016-08-16 14:23:01 +0300918// See if this element type can be formed into an array.
919bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 //
922 // Can the type be an array?
923 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300924 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400925 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300926 error(line, "cannot declare arrays of arrays",
927 TType(elementType).getCompleteString().c_str());
928 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300930 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
931 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
932 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300933 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300934 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300935 {
936 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300937 TType(elementType).getCompleteString().c_str());
938 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300939 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940
Olli Etuaho8a176262016-08-16 14:23:01 +0300941 return true;
942}
943
944// Check if this qualified element type can be formed into an array.
945bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
946 const TPublicType &elementType)
947{
948 if (checkIsValidTypeForArray(indexLocation, elementType))
949 {
950 return checkIsValidQualifierForArray(indexLocation, elementType);
951 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953}
954
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300956void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
957 const TString &identifier,
958 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959{
Olli Etuaho3739d232015-04-08 12:23:44 +0300960 ASSERT(type != nullptr);
961 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000962 {
963 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300964 type->qualifier = EvqTemporary;
965
966 // Generate informative error messages for ESSL1.
967 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400968 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000969 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530970 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400971 "structures containing arrays may not be declared constant since they cannot be "
972 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530973 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000974 }
975 else
976 {
977 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
978 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300979 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300981 if (type->isUnsizedArray())
982 {
983 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985}
986
Olli Etuaho2935c582015-04-08 14:32:06 +0300987// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988// and update the symbol table.
989//
Olli Etuaho2935c582015-04-08 14:32:06 +0300990// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400992bool TParseContext::declareVariable(const TSourceLoc &line,
993 const TString &identifier,
994 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300995 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996{
Olli Etuaho2935c582015-04-08 14:32:06 +0300997 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998
Olli Etuaho43364892017-02-13 16:00:12 +0000999 checkBindingIsValid(line, type);
1000
Olli Etuaho856c4972016-08-08 11:38:39 +03001001 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002
Olli Etuaho2935c582015-04-08 14:32:06 +03001003 // gl_LastFragData may be redeclared with a new precision qualifier
1004 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1005 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001006 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1007 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001008 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001009 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001010 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001011 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001012 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001013 }
1014 }
1015 else
1016 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001017 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1018 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001019 return false;
1020 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001021 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001022
Olli Etuaho8a176262016-08-16 14:23:01 +03001023 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001024 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025
Olli Etuaho2935c582015-04-08 14:32:06 +03001026 (*variable) = new TVariable(&identifier, type);
1027 if (!symbolTable.declare(*variable))
1028 {
1029 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001030 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001031 return false;
1032 }
1033
Olli Etuaho8a176262016-08-16 14:23:01 +03001034 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001035 return false;
1036
1037 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038}
1039
Martin Radev70866b82016-07-22 15:27:42 +03001040void TParseContext::checkIsParameterQualifierValid(
1041 const TSourceLoc &line,
1042 const TTypeQualifierBuilder &typeQualifierBuilder,
1043 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301044{
Olli Etuahocce89652017-06-19 16:04:09 +03001045 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001046 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001047
1048 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301049 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001050 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1051 }
1052
1053 if (!IsImage(type->getBasicType()))
1054 {
Olli Etuaho43364892017-02-13 16:00:12 +00001055 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001056 }
1057 else
1058 {
1059 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061
Martin Radev70866b82016-07-22 15:27:42 +03001062 type->setQualifier(typeQualifier.qualifier);
1063
1064 if (typeQualifier.precision != EbpUndefined)
1065 {
1066 type->setPrecision(typeQualifier.precision);
1067 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068}
1069
Olli Etuaho856c4972016-08-08 11:38:39 +03001070bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001071{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001072 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001073 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301074 if (iter == extBehavior.end())
1075 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001076 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001077 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001078 }
zmo@google.comf5450912011-09-09 01:37:19 +00001079 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301080 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1081 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001082 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1083 // associated with more than one extension.
1084 if (extension == "GL_OVR_multiview")
1085 {
1086 return checkCanUseExtension(line, "GL_OVR_multiview2");
1087 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001088 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001089 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001090 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091 if (iter->second == EBhWarn)
1092 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001093 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001094 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001095 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001100// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1101// compile-time or link-time errors are the same whether or not the declaration is empty".
1102// This function implements all the checks that are done on qualifiers regardless of if the
1103// declaration is empty.
1104void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1105 const sh::TLayoutQualifier &layoutQualifier,
1106 const TSourceLoc &location)
1107{
1108 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1109 {
1110 error(location, "Shared memory declarations cannot have layout specified", "layout");
1111 }
1112
1113 if (layoutQualifier.matrixPacking != EmpUnspecified)
1114 {
1115 error(location, "layout qualifier only valid for interface blocks",
1116 getMatrixPackingString(layoutQualifier.matrixPacking));
1117 return;
1118 }
1119
1120 if (layoutQualifier.blockStorage != EbsUnspecified)
1121 {
1122 error(location, "layout qualifier only valid for interface blocks",
1123 getBlockStorageString(layoutQualifier.blockStorage));
1124 return;
1125 }
1126
1127 if (qualifier == EvqFragmentOut)
1128 {
1129 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1130 {
1131 error(location, "invalid layout qualifier combination", "yuv");
1132 return;
1133 }
1134 }
1135 else
1136 {
1137 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1138 }
1139
Olli Etuaho95468d12017-05-04 11:14:34 +03001140 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1141 // parsing steps. So it needs to be checked here.
1142 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1143 {
1144 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1145 }
1146
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001147 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1148 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1149 {
1150 canHaveLocation = true;
1151 // We're not checking whether the uniform location is in range here since that depends on
1152 // the type of the variable.
1153 // The type can only be fully determined for non-empty declarations.
1154 }
1155 if (!canHaveLocation)
1156 {
1157 checkLocationIsNotSpecified(location, layoutQualifier);
1158 }
1159}
1160
jchen104cdac9e2017-05-08 11:01:20 +08001161void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1162 const TSourceLoc &location)
1163{
1164 if (publicType.precision != EbpHigh)
1165 {
1166 error(location, "Can only be highp", "atomic counter");
1167 }
1168 // dEQP enforces compile error if location is specified. See uniform_location.test.
1169 if (publicType.layoutQualifier.location != -1)
1170 {
1171 error(location, "location must not be set for atomic_uint", "layout");
1172 }
1173 if (publicType.layoutQualifier.binding == -1)
1174 {
1175 error(location, "no binding specified", "atomic counter");
1176 }
1177}
1178
Martin Radevb8b01222016-11-20 23:25:53 +02001179void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1180 const TSourceLoc &location)
1181{
1182 if (publicType.isUnsizedArray())
1183 {
1184 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1185 // error. It is assumed that this applies to empty declarations as well.
1186 error(location, "empty array declaration needs to specify a size", "");
1187 }
Martin Radevb8b01222016-11-20 23:25:53 +02001188}
1189
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001190// These checks are done for all declarations that are non-empty. They're done for non-empty
1191// declarations starting a declarator list, and declarators that follow an empty declaration.
1192void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1193 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001194{
Olli Etuahofa33d582015-04-09 14:33:12 +03001195 switch (publicType.qualifier)
1196 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001197 case EvqVaryingIn:
1198 case EvqVaryingOut:
1199 case EvqAttribute:
1200 case EvqVertexIn:
1201 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001202 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001203 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001204 {
1205 error(identifierLocation, "cannot be used with a structure",
1206 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001207 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001208 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001209
Jamie Madillb98c3a82015-07-23 14:26:04 -04001210 default:
1211 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001212 }
jchen10cc2a10e2017-05-03 14:05:12 +08001213 std::string reason(getBasicString(publicType.getBasicType()));
1214 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001215 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001216 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001217 {
1218 return;
1219 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001220
Andrei Volykhina5527072017-03-22 16:46:30 +03001221 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1222 publicType.qualifier != EvqConst) &&
1223 publicType.getBasicType() == EbtYuvCscStandardEXT)
1224 {
1225 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1226 getQualifierString(publicType.qualifier));
1227 return;
1228 }
1229
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001230 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1231 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001232 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1233 // But invalid shaders may still reach here with an unsized array declaration.
1234 if (!publicType.isUnsizedArray())
1235 {
1236 TType type(publicType);
1237 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1238 publicType.layoutQualifier);
1239 }
1240 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001241
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001242 // check for layout qualifier issues
1243 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001244
Martin Radev2cc85b32016-08-05 16:22:53 +03001245 if (IsImage(publicType.getBasicType()))
1246 {
1247
1248 switch (layoutQualifier.imageInternalFormat)
1249 {
1250 case EiifRGBA32F:
1251 case EiifRGBA16F:
1252 case EiifR32F:
1253 case EiifRGBA8:
1254 case EiifRGBA8_SNORM:
1255 if (!IsFloatImage(publicType.getBasicType()))
1256 {
1257 error(identifierLocation,
1258 "internal image format requires a floating image type",
1259 getBasicString(publicType.getBasicType()));
1260 return;
1261 }
1262 break;
1263 case EiifRGBA32I:
1264 case EiifRGBA16I:
1265 case EiifRGBA8I:
1266 case EiifR32I:
1267 if (!IsIntegerImage(publicType.getBasicType()))
1268 {
1269 error(identifierLocation,
1270 "internal image format requires an integer image type",
1271 getBasicString(publicType.getBasicType()));
1272 return;
1273 }
1274 break;
1275 case EiifRGBA32UI:
1276 case EiifRGBA16UI:
1277 case EiifRGBA8UI:
1278 case EiifR32UI:
1279 if (!IsUnsignedImage(publicType.getBasicType()))
1280 {
1281 error(identifierLocation,
1282 "internal image format requires an unsigned image type",
1283 getBasicString(publicType.getBasicType()));
1284 return;
1285 }
1286 break;
1287 case EiifUnspecified:
1288 error(identifierLocation, "layout qualifier", "No image internal format specified");
1289 return;
1290 default:
1291 error(identifierLocation, "layout qualifier", "unrecognized token");
1292 return;
1293 }
1294
1295 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1296 switch (layoutQualifier.imageInternalFormat)
1297 {
1298 case EiifR32F:
1299 case EiifR32I:
1300 case EiifR32UI:
1301 break;
1302 default:
1303 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1304 {
1305 error(identifierLocation, "layout qualifier",
1306 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1307 "image variables must be qualified readonly and/or writeonly");
1308 return;
1309 }
1310 break;
1311 }
1312 }
1313 else
1314 {
Olli Etuaho43364892017-02-13 16:00:12 +00001315 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001316 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1317 }
jchen104cdac9e2017-05-08 11:01:20 +08001318
1319 if (IsAtomicCounter(publicType.getBasicType()))
1320 {
1321 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1322 }
1323 else
1324 {
1325 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1326 }
Olli Etuaho43364892017-02-13 16:00:12 +00001327}
Martin Radev2cc85b32016-08-05 16:22:53 +03001328
Olli Etuaho43364892017-02-13 16:00:12 +00001329void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1330{
1331 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1332 int arraySize = type.isArray() ? type.getArraySize() : 1;
1333 if (IsImage(type.getBasicType()))
1334 {
1335 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1336 }
1337 else if (IsSampler(type.getBasicType()))
1338 {
1339 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1340 }
jchen104cdac9e2017-05-08 11:01:20 +08001341 else if (IsAtomicCounter(type.getBasicType()))
1342 {
1343 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1344 }
Olli Etuaho43364892017-02-13 16:00:12 +00001345 else
1346 {
1347 ASSERT(!IsOpaqueType(type.getBasicType()));
1348 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001349 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001350}
1351
Olli Etuaho856c4972016-08-08 11:38:39 +03001352void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1353 const TString &layoutQualifierName,
1354 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001355{
1356
1357 if (mShaderVersion < versionRequired)
1358 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001359 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001360 }
1361}
1362
Olli Etuaho856c4972016-08-08 11:38:39 +03001363bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1364 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001365{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001366 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001367 for (size_t i = 0u; i < localSize.size(); ++i)
1368 {
1369 if (localSize[i] != -1)
1370 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001371 error(location,
1372 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1373 "global layout declaration",
1374 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001375 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001376 }
1377 }
1378
Olli Etuaho8a176262016-08-16 14:23:01 +03001379 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001380}
1381
Olli Etuaho43364892017-02-13 16:00:12 +00001382void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001383 TLayoutImageInternalFormat internalFormat)
1384{
1385 if (internalFormat != EiifUnspecified)
1386 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001387 error(location, "invalid layout qualifier: only valid when used with images",
1388 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001389 }
Olli Etuaho43364892017-02-13 16:00:12 +00001390}
1391
1392void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1393{
1394 if (binding != -1)
1395 {
1396 error(location,
1397 "invalid layout qualifier: only valid when used with opaque types or blocks",
1398 "binding");
1399 }
1400}
1401
jchen104cdac9e2017-05-08 11:01:20 +08001402void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1403{
1404 if (offset != -1)
1405 {
1406 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1407 "offset");
1408 }
1409}
1410
Olli Etuaho43364892017-02-13 16:00:12 +00001411void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1412{
1413 // Expects arraySize to be 1 when setting binding for only a single variable.
1414 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1415 {
1416 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1417 }
1418}
1419
1420void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1421 int binding,
1422 int arraySize)
1423{
1424 // Expects arraySize to be 1 when setting binding for only a single variable.
1425 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1426 {
1427 error(location, "sampler binding greater than maximum texture units", "binding");
1428 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001429}
1430
jchen10af713a22017-04-19 09:10:56 +08001431void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1432{
1433 int size = (arraySize == 0 ? 1 : arraySize);
1434 if (binding + size > mMaxUniformBufferBindings)
1435 {
1436 error(location, "interface block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1437 "binding");
1438 }
1439}
jchen104cdac9e2017-05-08 11:01:20 +08001440void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1441{
1442 if (binding >= mMaxAtomicCounterBindings)
1443 {
1444 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1445 "binding");
1446 }
1447}
jchen10af713a22017-04-19 09:10:56 +08001448
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001449void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1450 int objectLocationCount,
1451 const TLayoutQualifier &layoutQualifier)
1452{
1453 int loc = layoutQualifier.location;
1454 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1455 {
1456 error(location, "Uniform location out of range", "location");
1457 }
1458}
1459
Andrei Volykhina5527072017-03-22 16:46:30 +03001460void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1461{
1462 if (yuv != false)
1463 {
1464 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1465 }
1466}
1467
Olli Etuaho383b7912016-08-05 11:22:59 +03001468void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001469 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001470{
1471 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1472 {
1473 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1474 if (qual == EvqOut || qual == EvqInOut)
1475 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001476 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001477 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001478 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001479 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001480 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001481 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001482 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001483 }
1484 }
1485 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001486}
1487
Martin Radev70866b82016-07-22 15:27:42 +03001488void TParseContext::checkInvariantVariableQualifier(bool invariant,
1489 const TQualifier qualifier,
1490 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001491{
Martin Radev70866b82016-07-22 15:27:42 +03001492 if (!invariant)
1493 return;
1494
1495 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001496 {
Martin Radev70866b82016-07-22 15:27:42 +03001497 // input variables in the fragment shader can be also qualified as invariant
1498 if (!sh::CanBeInvariantESSL1(qualifier))
1499 {
1500 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1501 }
1502 }
1503 else
1504 {
1505 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1506 {
1507 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1508 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001509 }
1510}
1511
Arun Patole7e7e68d2015-05-22 12:02:25 +05301512bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001513{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001514 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001515 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1516 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001517}
1518
Arun Patole7e7e68d2015-05-22 12:02:25 +05301519bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001520{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001521 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001522}
1523
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1525 const char *extName,
1526 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001527{
1528 pp::SourceLocation srcLoc;
1529 srcLoc.file = loc.first_file;
1530 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001531 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001532}
1533
Jamie Madillb98c3a82015-07-23 14:26:04 -04001534void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1535 const char *name,
1536 const char *value,
1537 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001538{
1539 pp::SourceLocation srcLoc;
1540 srcLoc.file = loc.first_file;
1541 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001542 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001543}
1544
Martin Radev4c4c8e72016-08-04 12:25:34 +03001545sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001546{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001547 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001548 for (size_t i = 0u; i < result.size(); ++i)
1549 {
1550 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1551 {
1552 result[i] = 1;
1553 }
1554 else
1555 {
1556 result[i] = mComputeShaderLocalSize[i];
1557 }
1558 }
1559 return result;
1560}
1561
Olli Etuaho56229f12017-07-10 14:16:33 +03001562TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1563 const TSourceLoc &line)
1564{
1565 TIntermConstantUnion *node = new TIntermConstantUnion(
1566 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1567 node->setLine(line);
1568 return node;
1569}
1570
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001571/////////////////////////////////////////////////////////////////////////////////
1572//
1573// Non-Errors.
1574//
1575/////////////////////////////////////////////////////////////////////////////////
1576
Jamie Madill5c097022014-08-20 16:38:32 -04001577const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1578 const TString *name,
1579 const TSymbol *symbol)
1580{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001581 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001582
1583 if (!symbol)
1584 {
1585 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001586 }
1587 else if (!symbol->isVariable())
1588 {
1589 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001590 }
1591 else
1592 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001593 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001594
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001595 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001596 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001597 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001598 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001599 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001600
1601 // Reject shaders using both gl_FragData and gl_FragColor
1602 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001603 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001604 {
1605 mUsesFragData = true;
1606 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001607 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001608 {
1609 mUsesFragColor = true;
1610 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001611 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1612 {
1613 mUsesSecondaryOutputs = true;
1614 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001615
1616 // This validation is not quite correct - it's only an error to write to
1617 // both FragData and FragColor. For simplicity, and because users shouldn't
1618 // be rewarded for reading from undefined varaibles, return an error
1619 // if they are both referenced, rather than assigned.
1620 if (mUsesFragData && mUsesFragColor)
1621 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001622 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1623 if (mUsesSecondaryOutputs)
1624 {
1625 errorMessage =
1626 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1627 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1628 }
1629 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001630 }
Martin Radevb0883602016-08-04 17:48:58 +03001631
1632 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1633 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1634 qualifier == EvqWorkGroupSize)
1635 {
1636 error(location,
1637 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1638 "gl_WorkGroupSize");
1639 }
Jamie Madill5c097022014-08-20 16:38:32 -04001640 }
1641
1642 if (!variable)
1643 {
1644 TType type(EbtFloat, EbpUndefined);
1645 TVariable *fakeVariable = new TVariable(name, type);
1646 symbolTable.declare(fakeVariable);
1647 variable = fakeVariable;
1648 }
1649
1650 return variable;
1651}
1652
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001653TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1654 const TString *name,
1655 const TSymbol *symbol)
1656{
1657 const TVariable *variable = getNamedVariable(location, name, symbol);
1658
Olli Etuaho09b04a22016-12-15 13:30:26 +00001659 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1660 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1661 {
1662 // WEBGL_multiview spec
1663 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1664 "gl_ViewID_OVR");
1665 }
1666
Olli Etuaho56229f12017-07-10 14:16:33 +03001667 TIntermTyped *node = nullptr;
1668
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001669 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001670 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001671 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001672 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001673 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001674 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1675 mComputeShaderLocalSizeDeclared)
1676 {
1677 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1678 // needs to be added to the AST as a constant and not as a symbol.
1679 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1680 TConstantUnion *constArray = new TConstantUnion[3];
1681 for (size_t i = 0; i < 3; ++i)
1682 {
1683 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1684 }
1685
1686 ASSERT(variable->getType().getBasicType() == EbtUInt);
1687 ASSERT(variable->getType().getObjectSize() == 3);
1688
1689 TType type(variable->getType());
1690 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001691 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001692 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001693 else
1694 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001695 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001696 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001697 ASSERT(node != nullptr);
1698 node->setLine(location);
1699 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001700}
1701
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001702// Initializers show up in several places in the grammar. Have one set of
1703// code to handle them here.
1704//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001705// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001706bool TParseContext::executeInitializer(const TSourceLoc &line,
1707 const TString &identifier,
1708 const TPublicType &pType,
1709 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001710 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711{
Olli Etuaho13389b62016-10-16 11:48:18 +01001712 ASSERT(initNode != nullptr);
1713 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001714 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715
Olli Etuaho2935c582015-04-08 14:32:06 +03001716 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001717 if (type.isUnsizedArray())
1718 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001719 // We have not checked yet whether the initializer actually is an array or not.
1720 if (initializer->isArray())
1721 {
1722 type.setArraySize(initializer->getArraySize());
1723 }
1724 else
1725 {
1726 // Having a non-array initializer for an unsized array will result in an error later,
1727 // so we don't generate an error message here.
1728 type.setArraySize(1u);
1729 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001730 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001731 if (!declareVariable(line, identifier, type, &variable))
1732 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001733 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735
Olli Etuahob0c645e2015-05-12 14:25:36 +03001736 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737 if (symbolTable.atGlobalLevel() &&
1738 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001739 {
1740 // Error message does not completely match behavior with ESSL 1.00, but
1741 // we want to steer developers towards only using constant expressions.
1742 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001743 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001744 }
1745 if (globalInitWarning)
1746 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001747 warning(
1748 line,
1749 "global variable initializers should be constant expressions "
1750 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1751 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001752 }
1753
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001754 //
1755 // identifier must be of type constant, a global, or a temporary
1756 //
1757 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301758 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1759 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001760 error(line, " cannot initialize this type of qualifier ",
1761 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001762 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001763 }
1764 //
1765 // test for and propagate constant
1766 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001767
Arun Patole7e7e68d2015-05-22 12:02:25 +05301768 if (qualifier == EvqConst)
1769 {
1770 if (qualifier != initializer->getType().getQualifier())
1771 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001772 std::stringstream reasonStream;
1773 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1774 << "'";
1775 std::string reason = reasonStream.str();
1776 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001777 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001778 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001779 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301780 if (type != initializer->getType())
1781 {
1782 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001783 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001784 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001785 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001786 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001787
1788 // Save the constant folded value to the variable if possible. For example array
1789 // initializers are not folded, since that way copying the array literal to multiple places
1790 // in the shader is avoided.
1791 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1792 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301793 if (initializer->getAsConstantUnion())
1794 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001795 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001796 ASSERT(*initNode == nullptr);
1797 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301798 }
1799 else if (initializer->getAsSymbolNode())
1800 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001801 const TSymbol *symbol =
1802 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1803 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001805 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001806 if (constArray)
1807 {
1808 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001809 ASSERT(*initNode == nullptr);
1810 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001811 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001812 }
1813 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001814
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001815 TIntermSymbol *intermSymbol =
1816 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1817 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001818 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1819 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001820 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001821 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001822 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001823 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
Olli Etuaho914b79a2017-06-19 16:03:19 +03001825 return true;
1826}
1827
1828TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1829 const TString &identifier,
1830 TIntermTyped *initializer,
1831 const TSourceLoc &loc)
1832{
1833 checkIsScalarBool(loc, pType);
1834 TIntermBinary *initNode = nullptr;
1835 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1836 {
1837 // The initializer is valid. The init condition needs to have a node - either the
1838 // initializer node, or a constant node in case the initialized variable is const and won't
1839 // be recorded in the AST.
1840 if (initNode == nullptr)
1841 {
1842 return initializer;
1843 }
1844 else
1845 {
1846 TIntermDeclaration *declaration = new TIntermDeclaration();
1847 declaration->appendDeclarator(initNode);
1848 return declaration;
1849 }
1850 }
1851 return nullptr;
1852}
1853
1854TIntermNode *TParseContext::addLoop(TLoopType type,
1855 TIntermNode *init,
1856 TIntermNode *cond,
1857 TIntermTyped *expr,
1858 TIntermNode *body,
1859 const TSourceLoc &line)
1860{
1861 TIntermNode *node = nullptr;
1862 TIntermTyped *typedCond = nullptr;
1863 if (cond)
1864 {
1865 typedCond = cond->getAsTyped();
1866 }
1867 if (cond == nullptr || typedCond)
1868 {
Olli Etuahocce89652017-06-19 16:04:09 +03001869 if (type == ELoopDoWhile)
1870 {
1871 checkIsScalarBool(line, typedCond);
1872 }
1873 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1874 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1875 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1876 !typedCond->isVector()));
1877
Olli Etuaho914b79a2017-06-19 16:03:19 +03001878 node = new TIntermLoop(type, init, typedCond, expr, TIntermediate::EnsureBlock(body));
1879 node->setLine(line);
1880 return node;
1881 }
1882
Olli Etuahocce89652017-06-19 16:04:09 +03001883 ASSERT(type != ELoopDoWhile);
1884
Olli Etuaho914b79a2017-06-19 16:03:19 +03001885 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1886 ASSERT(declaration);
1887 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1888 ASSERT(declarator->getLeft()->getAsSymbolNode());
1889
1890 // The condition is a declaration. In the AST representation we don't support declarations as
1891 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1892 // the loop.
1893 TIntermBlock *block = new TIntermBlock();
1894
1895 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1896 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1897 block->appendStatement(declareCondition);
1898
1899 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1900 declarator->getRight()->deepCopy());
1901 TIntermLoop *loop =
1902 new TIntermLoop(type, init, conditionInit, expr, TIntermediate::EnsureBlock(body));
1903 block->appendStatement(loop);
1904 loop->setLine(line);
1905 block->setLine(line);
1906 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907}
1908
Olli Etuahocce89652017-06-19 16:04:09 +03001909TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1910 TIntermNodePair code,
1911 const TSourceLoc &loc)
1912{
Olli Etuaho56229f12017-07-10 14:16:33 +03001913 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03001914
1915 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03001916 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03001917 {
1918 if (cond->getAsConstantUnion()->getBConst(0) == true)
1919 {
1920 return TIntermediate::EnsureBlock(code.node1);
1921 }
1922 else
1923 {
1924 return TIntermediate::EnsureBlock(code.node2);
1925 }
1926 }
1927
1928 TIntermIfElse *node = new TIntermIfElse(cond, TIntermediate::EnsureBlock(code.node1),
1929 TIntermediate::EnsureBlock(code.node2));
1930 node->setLine(loc);
1931
1932 return node;
1933}
1934
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001935void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1936{
1937 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1938 typeSpecifier->getBasicType());
1939
1940 if (mShaderVersion < 300 && typeSpecifier->array)
1941 {
1942 error(typeSpecifier->getLine(), "not supported", "first-class array");
1943 typeSpecifier->clearArrayness();
1944 }
1945}
1946
Martin Radev70866b82016-07-22 15:27:42 +03001947TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301948 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001949{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001950 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001951
Martin Radev70866b82016-07-22 15:27:42 +03001952 TPublicType returnType = typeSpecifier;
1953 returnType.qualifier = typeQualifier.qualifier;
1954 returnType.invariant = typeQualifier.invariant;
1955 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001956 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001957 returnType.precision = typeSpecifier.precision;
1958
1959 if (typeQualifier.precision != EbpUndefined)
1960 {
1961 returnType.precision = typeQualifier.precision;
1962 }
1963
Martin Radev4a9cd802016-09-01 16:51:51 +03001964 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1965 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001966
Martin Radev4a9cd802016-09-01 16:51:51 +03001967 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1968 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001969
Martin Radev4a9cd802016-09-01 16:51:51 +03001970 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001971
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001972 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001973 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001974 if (typeSpecifier.array)
1975 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001976 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001977 returnType.clearArrayness();
1978 }
1979
Martin Radev70866b82016-07-22 15:27:42 +03001980 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001981 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001982 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001983 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001984 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001985 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001986
Martin Radev70866b82016-07-22 15:27:42 +03001987 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001988 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001989 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001990 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001991 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001992 }
1993 }
1994 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001995 {
Martin Radev70866b82016-07-22 15:27:42 +03001996 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001997 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001998 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001999 }
Martin Radev70866b82016-07-22 15:27:42 +03002000 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2001 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002002 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002003 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2004 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002005 }
Martin Radev70866b82016-07-22 15:27:42 +03002006 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002007 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002008 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002009 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002010 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002011 }
2012
2013 return returnType;
2014}
2015
Olli Etuaho856c4972016-08-08 11:38:39 +03002016void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2017 const TPublicType &type,
2018 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002019{
2020 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002021 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002022 {
2023 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002024 }
2025
2026 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2027 switch (qualifier)
2028 {
2029 case EvqVertexIn:
2030 // ESSL 3.00 section 4.3.4
2031 if (type.array)
2032 {
2033 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002034 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002035 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002036 return;
2037 case EvqFragmentOut:
2038 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002039 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002040 {
2041 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002042 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002043 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002044 return;
2045 default:
2046 break;
2047 }
2048
2049 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2050 // restrictions.
2051 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002052 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2053 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002054 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2055 {
2056 error(qualifierLocation, "must use 'flat' interpolation here",
2057 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002058 }
2059
Martin Radev4a9cd802016-09-01 16:51:51 +03002060 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002061 {
2062 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2063 // These restrictions are only implied by the ESSL 3.00 spec, but
2064 // the ESSL 3.10 spec lists these restrictions explicitly.
2065 if (type.array)
2066 {
2067 error(qualifierLocation, "cannot be an array of structures",
2068 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002069 }
2070 if (type.isStructureContainingArrays())
2071 {
2072 error(qualifierLocation, "cannot be a structure containing an array",
2073 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002074 }
2075 if (type.isStructureContainingType(EbtStruct))
2076 {
2077 error(qualifierLocation, "cannot be a structure containing a structure",
2078 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002079 }
2080 if (type.isStructureContainingType(EbtBool))
2081 {
2082 error(qualifierLocation, "cannot be a structure containing a bool",
2083 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002084 }
2085 }
2086}
2087
Martin Radev2cc85b32016-08-05 16:22:53 +03002088void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2089{
2090 if (qualifier.getType() == QtStorage)
2091 {
2092 const TStorageQualifierWrapper &storageQualifier =
2093 static_cast<const TStorageQualifierWrapper &>(qualifier);
2094 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2095 !symbolTable.atGlobalLevel())
2096 {
2097 error(storageQualifier.getLine(),
2098 "Local variables can only use the const storage qualifier.",
2099 storageQualifier.getQualifierString().c_str());
2100 }
2101 }
2102}
2103
Olli Etuaho43364892017-02-13 16:00:12 +00002104void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002105 const TSourceLoc &location)
2106{
2107 if (memoryQualifier.readonly)
2108 {
2109 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002110 }
2111 if (memoryQualifier.writeonly)
2112 {
2113 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002114 }
Martin Radev049edfa2016-11-11 14:35:37 +02002115 if (memoryQualifier.coherent)
2116 {
2117 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002118 }
2119 if (memoryQualifier.restrictQualifier)
2120 {
2121 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002122 }
2123 if (memoryQualifier.volatileQualifier)
2124 {
2125 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002126 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002127}
2128
jchen104cdac9e2017-05-08 11:01:20 +08002129// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2130// intermediate tree.
2131void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2132 size_t size,
2133 bool forceAppend,
2134 const TSourceLoc &loc,
2135 TType &type)
2136{
2137 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2138 int offset;
2139 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2140 {
2141 offset = bindingState.appendSpan(size);
2142 }
2143 else
2144 {
2145 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2146 }
2147 if (offset == -1)
2148 {
2149 error(loc, "Offset overlapping", "atomic counter");
2150 return;
2151 }
2152 TLayoutQualifier qualifier = type.getLayoutQualifier();
2153 qualifier.offset = offset;
2154 type.setLayoutQualifier(qualifier);
2155}
2156
Olli Etuaho13389b62016-10-16 11:48:18 +01002157TIntermDeclaration *TParseContext::parseSingleDeclaration(
2158 TPublicType &publicType,
2159 const TSourceLoc &identifierOrTypeLocation,
2160 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002161{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002162 TType type(publicType);
2163 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2164 mDirectiveHandler.pragma().stdgl.invariantAll)
2165 {
2166 TQualifier qualifier = type.getQualifier();
2167
2168 // The directive handler has already taken care of rejecting invalid uses of this pragma
2169 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2170 // affected variable declarations:
2171 //
2172 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2173 // elsewhere, in TranslatorGLSL.)
2174 //
2175 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2176 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2177 // the way this is currently implemented we have to enable this compiler option before
2178 // parsing the shader and determining the shading language version it uses. If this were
2179 // implemented as a post-pass, the workaround could be more targeted.
2180 //
2181 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2182 // the specification, but there are desktop OpenGL drivers that expect that this is the
2183 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2184 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2185 {
2186 type.setInvariant(true);
2187 }
2188 }
2189
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002190 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2191 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002192
Olli Etuahobab4c082015-04-24 16:38:49 +03002193 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002194 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002195
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002196 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002197 if (emptyDeclaration)
2198 {
Martin Radevb8b01222016-11-20 23:25:53 +02002199 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002200 // In most cases we don't need to create a symbol node for an empty declaration.
2201 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2202 if (type.getBasicType() == EbtStruct)
2203 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002204 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002205 }
jchen104cdac9e2017-05-08 11:01:20 +08002206 else if (IsAtomicCounter(publicType.getBasicType()))
2207 {
2208 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2209 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002210 }
2211 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002212 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002213 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002214
Olli Etuaho856c4972016-08-08 11:38:39 +03002215 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002216
jchen104cdac9e2017-05-08 11:01:20 +08002217 if (IsAtomicCounter(publicType.getBasicType()))
2218 {
2219
2220 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2221 identifierOrTypeLocation, type);
2222 }
2223
Olli Etuaho2935c582015-04-08 14:32:06 +03002224 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002225 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002226
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002227 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002228 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002229 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002230 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002231 }
2232
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002233 TIntermDeclaration *declaration = new TIntermDeclaration();
2234 declaration->setLine(identifierOrTypeLocation);
2235 if (symbol)
2236 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002237 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002238 declaration->appendDeclarator(symbol);
2239 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002240 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002241}
2242
Olli Etuaho13389b62016-10-16 11:48:18 +01002243TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2244 const TSourceLoc &identifierLocation,
2245 const TString &identifier,
2246 const TSourceLoc &indexLocation,
2247 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002248{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002249 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002250
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002251 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2252 identifierLocation);
2253
2254 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002255
Olli Etuaho856c4972016-08-08 11:38:39 +03002256 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002257
Olli Etuaho8a176262016-08-16 14:23:01 +03002258 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002259
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002260 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002261
Olli Etuaho856c4972016-08-08 11:38:39 +03002262 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002263 // Make the type an array even if size check failed.
2264 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2265 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002266
jchen104cdac9e2017-05-08 11:01:20 +08002267 if (IsAtomicCounter(publicType.getBasicType()))
2268 {
2269 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2270 identifierLocation, arrayType);
2271 }
2272
Olli Etuaho2935c582015-04-08 14:32:06 +03002273 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002274 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002275
Olli Etuaho13389b62016-10-16 11:48:18 +01002276 TIntermDeclaration *declaration = new TIntermDeclaration();
2277 declaration->setLine(identifierLocation);
2278
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002279 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002280 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002281 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2282 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002283 declaration->appendDeclarator(symbol);
2284 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002285
Olli Etuaho13389b62016-10-16 11:48:18 +01002286 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002287}
2288
Olli Etuaho13389b62016-10-16 11:48:18 +01002289TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2290 const TSourceLoc &identifierLocation,
2291 const TString &identifier,
2292 const TSourceLoc &initLocation,
2293 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002294{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002295 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002296
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002297 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2298 identifierLocation);
2299
2300 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002301
Olli Etuaho13389b62016-10-16 11:48:18 +01002302 TIntermDeclaration *declaration = new TIntermDeclaration();
2303 declaration->setLine(identifierLocation);
2304
2305 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002306 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002307 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002308 if (initNode)
2309 {
2310 declaration->appendDeclarator(initNode);
2311 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002312 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002313 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002314}
2315
Olli Etuaho13389b62016-10-16 11:48:18 +01002316TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002317 TPublicType &publicType,
2318 const TSourceLoc &identifierLocation,
2319 const TString &identifier,
2320 const TSourceLoc &indexLocation,
2321 TIntermTyped *indexExpression,
2322 const TSourceLoc &initLocation,
2323 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002324{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002325 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002326
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002327 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2328 identifierLocation);
2329
2330 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002331
Olli Etuaho8a176262016-08-16 14:23:01 +03002332 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002333
2334 TPublicType arrayType(publicType);
2335
Olli Etuaho856c4972016-08-08 11:38:39 +03002336 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002337 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2338 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002339 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002340 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002341 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002342 }
2343 // Make the type an array even if size check failed.
2344 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2345 arrayType.setArraySize(size);
2346
Olli Etuaho13389b62016-10-16 11:48:18 +01002347 TIntermDeclaration *declaration = new TIntermDeclaration();
2348 declaration->setLine(identifierLocation);
2349
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002350 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002351 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002352 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002353 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002354 if (initNode)
2355 {
2356 declaration->appendDeclarator(initNode);
2357 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002358 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002359
2360 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002361}
2362
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002363TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002364 const TTypeQualifierBuilder &typeQualifierBuilder,
2365 const TSourceLoc &identifierLoc,
2366 const TString *identifier,
2367 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002368{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002369 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002370
Martin Radev70866b82016-07-22 15:27:42 +03002371 if (!typeQualifier.invariant)
2372 {
2373 error(identifierLoc, "Expected invariant", identifier->c_str());
2374 return nullptr;
2375 }
2376 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2377 {
2378 return nullptr;
2379 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002380 if (!symbol)
2381 {
2382 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002383 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002384 }
Martin Radev70866b82016-07-22 15:27:42 +03002385 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002386 {
Martin Radev70866b82016-07-22 15:27:42 +03002387 error(identifierLoc, "invariant declaration specifies qualifier",
2388 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002389 }
Martin Radev70866b82016-07-22 15:27:42 +03002390 if (typeQualifier.precision != EbpUndefined)
2391 {
2392 error(identifierLoc, "invariant declaration specifies precision",
2393 getPrecisionString(typeQualifier.precision));
2394 }
2395 if (!typeQualifier.layoutQualifier.isEmpty())
2396 {
2397 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2398 }
2399
2400 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2401 ASSERT(variable);
2402 const TType &type = variable->getType();
2403
2404 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2405 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002406 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002407
2408 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2409
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002410 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2411 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002412
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002413 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002414}
2415
Olli Etuaho13389b62016-10-16 11:48:18 +01002416void TParseContext::parseDeclarator(TPublicType &publicType,
2417 const TSourceLoc &identifierLocation,
2418 const TString &identifier,
2419 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002420{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002421 // If the declaration starting this declarator list was empty (example: int,), some checks were
2422 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002423 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002424 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002425 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2426 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002427 }
2428
Olli Etuaho856c4972016-08-08 11:38:39 +03002429 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002430
Olli Etuaho856c4972016-08-08 11:38:39 +03002431 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002432
Olli Etuaho2935c582015-04-08 14:32:06 +03002433 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002434 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002435 if (IsAtomicCounter(publicType.getBasicType()))
2436 {
2437 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2438 identifierLocation, type);
2439 }
Olli Etuaho43364892017-02-13 16:00:12 +00002440 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002441
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002442 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002443 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002444 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2445 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002446 declarationOut->appendDeclarator(symbol);
2447 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002448}
2449
Olli Etuaho13389b62016-10-16 11:48:18 +01002450void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2451 const TSourceLoc &identifierLocation,
2452 const TString &identifier,
2453 const TSourceLoc &arrayLocation,
2454 TIntermTyped *indexExpression,
2455 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002456{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002457 // If the declaration starting this declarator list was empty (example: int,), some checks were
2458 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002459 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002460 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002461 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2462 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002463 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002464
Olli Etuaho856c4972016-08-08 11:38:39 +03002465 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002466
Olli Etuaho856c4972016-08-08 11:38:39 +03002467 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002468
Olli Etuaho8a176262016-08-16 14:23:01 +03002469 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002470 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002471 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002472 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002473 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002474
jchen104cdac9e2017-05-08 11:01:20 +08002475 if (IsAtomicCounter(publicType.getBasicType()))
2476 {
2477 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2478 true, identifierLocation, arrayType);
2479 }
2480
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002481 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002482 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002483
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002484 TIntermSymbol *symbol = new TIntermSymbol(0, identifier, arrayType);
2485 symbol->setLine(identifierLocation);
2486 if (variable)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002487 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002488
Olli Etuaho13389b62016-10-16 11:48:18 +01002489 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002490 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002491}
2492
Olli Etuaho13389b62016-10-16 11:48:18 +01002493void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2494 const TSourceLoc &identifierLocation,
2495 const TString &identifier,
2496 const TSourceLoc &initLocation,
2497 TIntermTyped *initializer,
2498 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002499{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002500 // If the declaration starting this declarator list was empty (example: int,), some checks were
2501 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002502 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002503 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002504 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2505 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002506 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002507
Olli Etuaho856c4972016-08-08 11:38:39 +03002508 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002511 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002512 {
2513 //
2514 // build the intermediate representation
2515 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002517 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002519 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002520 }
2521}
2522
Olli Etuaho13389b62016-10-16 11:48:18 +01002523void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2524 const TSourceLoc &identifierLocation,
2525 const TString &identifier,
2526 const TSourceLoc &indexLocation,
2527 TIntermTyped *indexExpression,
2528 const TSourceLoc &initLocation,
2529 TIntermTyped *initializer,
2530 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002531{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 // If the declaration starting this declarator list was empty (example: int,), some checks were
2533 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002535 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2537 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002538 }
2539
Olli Etuaho856c4972016-08-08 11:38:39 +03002540 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002541
Olli Etuaho8a176262016-08-16 14:23:01 +03002542 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002543
2544 TPublicType arrayType(publicType);
2545
Olli Etuaho856c4972016-08-08 11:38:39 +03002546 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2548 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002549 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002550 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002551 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 }
2553 // Make the type an array even if size check failed.
2554 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2555 arrayType.setArraySize(size);
2556
2557 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002558 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002559 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560 {
2561 if (initNode)
2562 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002563 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565 }
2566}
2567
jchen104cdac9e2017-05-08 11:01:20 +08002568void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2569 const TSourceLoc &location)
2570{
2571 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2572 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2573 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2574 {
2575 error(location, "Requires both binding and offset", "layout");
2576 return;
2577 }
2578 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2579}
2580
Olli Etuahocce89652017-06-19 16:04:09 +03002581void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2582 const TPublicType &type,
2583 const TSourceLoc &loc)
2584{
2585 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2586 !getFragmentPrecisionHigh())
2587 {
2588 error(loc, "precision is not supported in fragment shader", "highp");
2589 }
2590
2591 if (!CanSetDefaultPrecisionOnType(type))
2592 {
2593 error(loc, "illegal type argument for default precision qualifier",
2594 getBasicString(type.getBasicType()));
2595 return;
2596 }
2597 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2598}
2599
Martin Radev70866b82016-07-22 15:27:42 +03002600void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002601{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002602 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002603 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002604
Martin Radev70866b82016-07-22 15:27:42 +03002605 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2606 typeQualifier.line);
2607
Jamie Madillc2128ff2016-07-04 10:26:17 -04002608 // It should never be the case, but some strange parser errors can send us here.
2609 if (layoutQualifier.isEmpty())
2610 {
2611 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002612 return;
2613 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002614
Martin Radev802abe02016-08-04 17:48:32 +03002615 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002616 {
Olli Etuaho43364892017-02-13 16:00:12 +00002617 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002618 return;
2619 }
2620
Olli Etuaho43364892017-02-13 16:00:12 +00002621 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2622
2623 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002624
2625 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2626
Andrei Volykhina5527072017-03-22 16:46:30 +03002627 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2628
jchen104cdac9e2017-05-08 11:01:20 +08002629 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2630
Martin Radev802abe02016-08-04 17:48:32 +03002631 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002632 {
Martin Radev802abe02016-08-04 17:48:32 +03002633 if (mComputeShaderLocalSizeDeclared &&
2634 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2635 {
2636 error(typeQualifier.line, "Work group size does not match the previous declaration",
2637 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002638 return;
2639 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002640
Martin Radev802abe02016-08-04 17:48:32 +03002641 if (mShaderVersion < 310)
2642 {
2643 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002644 return;
2645 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002646
Martin Radev4c4c8e72016-08-04 12:25:34 +03002647 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002648 {
2649 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002650 return;
2651 }
2652
2653 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2654 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2655
2656 const TConstantUnion *maxComputeWorkGroupSizeData =
2657 maxComputeWorkGroupSize->getConstPointer();
2658
2659 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2660 {
2661 if (layoutQualifier.localSize[i] != -1)
2662 {
2663 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2664 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2665 if (mComputeShaderLocalSize[i] < 1 ||
2666 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2667 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002668 std::stringstream reasonStream;
2669 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2670 << maxComputeWorkGroupSizeValue;
2671 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002672
Olli Etuaho4de340a2016-12-16 09:32:03 +00002673 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002674 return;
2675 }
2676 }
2677 }
2678
2679 mComputeShaderLocalSizeDeclared = true;
2680 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002681 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002682 {
2683 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2684 // specification.
2685 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2686 {
2687 error(typeQualifier.line, "Number of views does not match the previous declaration",
2688 "layout");
2689 return;
2690 }
2691
2692 if (layoutQualifier.numViews == -1)
2693 {
2694 error(typeQualifier.line, "No num_views specified", "layout");
2695 return;
2696 }
2697
2698 if (layoutQualifier.numViews > mMaxNumViews)
2699 {
2700 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2701 "layout");
2702 return;
2703 }
2704
2705 mNumViews = layoutQualifier.numViews;
2706 }
Martin Radev802abe02016-08-04 17:48:32 +03002707 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002708 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002709 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002710 {
Martin Radev802abe02016-08-04 17:48:32 +03002711 return;
2712 }
2713
2714 if (typeQualifier.qualifier != EvqUniform)
2715 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002716 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2717 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002718 return;
2719 }
2720
2721 if (mShaderVersion < 300)
2722 {
2723 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2724 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002725 return;
2726 }
2727
Olli Etuaho09b04a22016-12-15 13:30:26 +00002728 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002729
2730 if (layoutQualifier.matrixPacking != EmpUnspecified)
2731 {
2732 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2733 }
2734
2735 if (layoutQualifier.blockStorage != EbsUnspecified)
2736 {
2737 mDefaultBlockStorage = layoutQualifier.blockStorage;
2738 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002739 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002740}
2741
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002742TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2743 const TFunction &function,
2744 const TSourceLoc &location,
2745 bool insertParametersToSymbolTable)
2746{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002747 checkIsNotReserved(location, function.getName());
2748
Olli Etuahofe486322017-03-21 09:30:54 +00002749 TIntermFunctionPrototype *prototype =
2750 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002751 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2752 // point to the data that already exists in the symbol table.
2753 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2754 prototype->setLine(location);
2755
2756 for (size_t i = 0; i < function.getParamCount(); i++)
2757 {
2758 const TConstParameter &param = function.getParam(i);
2759
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002760 TIntermSymbol *symbol = nullptr;
2761
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002762 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2763 // be used for unused args).
2764 if (param.name != nullptr)
2765 {
2766 TVariable *variable = new TVariable(param.name, *param.type);
2767
2768 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002769 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002770 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002771 if (symbolTable.declare(variable))
2772 {
2773 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2774 variable->getType());
2775 }
2776 else
2777 {
2778 error(location, "redefinition", variable->getName().c_str());
2779 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002780 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002781 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002782 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002783 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002784 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2785 // symbol.
2786 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002787 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002788 symbol->setLine(location);
2789 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002790 }
2791 return prototype;
2792}
2793
Olli Etuaho16c745a2017-01-16 17:02:27 +00002794TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2795 const TFunction &parsedFunction,
2796 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002797{
Olli Etuaho476197f2016-10-11 13:59:08 +01002798 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2799 // first declaration. Either way the instance in the symbol table is used to track whether the
2800 // function is declared multiple times.
2801 TFunction *function = static_cast<TFunction *>(
2802 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2803 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002804 {
2805 // ESSL 1.00.17 section 4.2.7.
2806 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2807 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002808 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002809 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002810
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002811 TIntermFunctionPrototype *prototype =
2812 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002813
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002814 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002815
2816 if (!symbolTable.atGlobalLevel())
2817 {
2818 // ESSL 3.00.4 section 4.2.4.
2819 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002820 }
2821
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002822 return prototype;
2823}
2824
Olli Etuaho336b1472016-10-05 16:37:55 +01002825TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002826 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002827 TIntermBlock *functionBody,
2828 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002829{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002830 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002831 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2832 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002833 error(location, "function does not return a value:",
2834 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002835 }
2836
Olli Etuahof51fdd22016-10-03 10:03:40 +01002837 if (functionBody == nullptr)
2838 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002839 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002840 functionBody->setLine(location);
2841 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002842 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002843 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002844 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002845
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002846 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002847 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002848}
2849
Olli Etuaho476197f2016-10-11 13:59:08 +01002850void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2851 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002852 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002853{
Olli Etuaho476197f2016-10-11 13:59:08 +01002854 ASSERT(function);
2855 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002856 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002857 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002858
2859 if (builtIn)
2860 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002861 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002862 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002863 else
Jamie Madill185fb402015-06-12 15:48:48 -04002864 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002865 TFunction *prevDec = static_cast<TFunction *>(
2866 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2867
2868 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2869 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2870 // occurance.
2871 if (*function != prevDec)
2872 {
2873 // Swap the parameters of the previous declaration to the parameters of the function
2874 // definition (parameter names may differ).
2875 prevDec->swapParameters(**function);
2876
2877 // The function definition will share the same symbol as any previous declaration.
2878 *function = prevDec;
2879 }
2880
2881 if ((*function)->isDefined())
2882 {
2883 error(location, "function already has a body", (*function)->getName().c_str());
2884 }
2885
2886 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002887 }
Jamie Madill185fb402015-06-12 15:48:48 -04002888
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002889 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002890 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002891 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002892
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002893 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002894 setLoopNestingLevel(0);
2895}
2896
Jamie Madillb98c3a82015-07-23 14:26:04 -04002897TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002898{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002899 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002900 // We don't know at this point whether this is a function definition or a prototype.
2901 // The definition production code will check for redefinitions.
2902 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002903 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002904 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2905 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002906 //
2907 TFunction *prevDec =
2908 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302909
Martin Radevda6254b2016-12-14 17:00:36 +02002910 if (getShaderVersion() >= 300 &&
2911 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2912 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302913 {
Martin Radevda6254b2016-12-14 17:00:36 +02002914 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302915 // Therefore overloading or redefining builtin functions is an error.
2916 error(location, "Name of a built-in function cannot be redeclared as function",
2917 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302918 }
2919 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002920 {
2921 if (prevDec->getReturnType() != function->getReturnType())
2922 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002923 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002924 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002925 }
2926 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2927 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002928 if (prevDec->getParam(i).type->getQualifier() !=
2929 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002930 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002931 error(location,
2932 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002933 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002934 }
2935 }
2936 }
2937
2938 //
2939 // Check for previously declared variables using the same name.
2940 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002941 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002942 if (prevSym)
2943 {
2944 if (!prevSym->isFunction())
2945 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002946 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002947 }
2948 }
2949 else
2950 {
2951 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002952 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002953 }
2954
2955 // We're at the inner scope level of the function's arguments and body statement.
2956 // Add the function prototype to the surrounding scope instead.
2957 symbolTable.getOuterLevel()->insert(function);
2958
Olli Etuaho78d13742017-01-18 13:06:10 +00002959 // Raise error message if main function takes any parameters or return anything other than void
2960 if (function->getName() == "main")
2961 {
2962 if (function->getParamCount() > 0)
2963 {
2964 error(location, "function cannot take any parameter(s)", "main");
2965 }
2966 if (function->getReturnType().getBasicType() != EbtVoid)
2967 {
2968 error(location, "main function cannot return a value",
2969 function->getReturnType().getBasicString());
2970 }
2971 }
2972
Jamie Madill185fb402015-06-12 15:48:48 -04002973 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002974 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2975 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002976 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2977 //
2978 return function;
2979}
2980
Olli Etuaho9de84a52016-06-14 17:36:01 +03002981TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2982 const TString *name,
2983 const TSourceLoc &location)
2984{
2985 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2986 {
2987 error(location, "no qualifiers allowed for function return",
2988 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002989 }
2990 if (!type.layoutQualifier.isEmpty())
2991 {
2992 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002993 }
jchen10cc2a10e2017-05-03 14:05:12 +08002994 // make sure an opaque type is not involved as well...
2995 std::string reason(getBasicString(type.getBasicType()));
2996 reason += "s can't be function return values";
2997 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002998 if (mShaderVersion < 300)
2999 {
3000 // Array return values are forbidden, but there's also no valid syntax for declaring array
3001 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003002 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003003
3004 if (type.isStructureContainingArrays())
3005 {
3006 // ESSL 1.00.17 section 6.1 Function Definitions
3007 error(location, "structures containing arrays can't be function return values",
3008 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003009 }
3010 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003011
3012 // Add the function as a prototype after parsing it (we do not support recursion)
3013 return new TFunction(name, new TType(type));
3014}
3015
Olli Etuahocce89652017-06-19 16:04:09 +03003016TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3017{
Olli Etuahocce89652017-06-19 16:04:09 +03003018 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3019 return new TFunction(name, returnType);
3020}
3021
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003022TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003023{
Olli Etuahocce89652017-06-19 16:04:09 +03003024 if (mShaderVersion < 300 && publicType.array)
3025 {
3026 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3027 "[]");
3028 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003029 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003030 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003031 error(publicType.getLine(), "constructor can't be a structure definition",
3032 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003033 }
3034
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003035 TType *type = new TType(publicType);
3036 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003037 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003038 error(publicType.getLine(), "cannot construct this type",
3039 getBasicString(publicType.getBasicType()));
3040 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003041 }
3042
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003043 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003044}
3045
Olli Etuahocce89652017-06-19 16:04:09 +03003046TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3047 const TString *name,
3048 const TSourceLoc &nameLoc)
3049{
3050 if (publicType.getBasicType() == EbtVoid)
3051 {
3052 error(nameLoc, "illegal use of type 'void'", name->c_str());
3053 }
3054 checkIsNotReserved(nameLoc, *name);
3055 TType *type = new TType(publicType);
3056 TParameter param = {name, type};
3057 return param;
3058}
3059
3060TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3061 const TSourceLoc &identifierLoc,
3062 TIntermTyped *arraySize,
3063 const TSourceLoc &arrayLoc,
3064 TPublicType *type)
3065{
3066 checkIsValidTypeForArray(arrayLoc, *type);
3067 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3068 type->setArraySize(size);
3069 return parseParameterDeclarator(*type, identifier, identifierLoc);
3070}
3071
Jamie Madillb98c3a82015-07-23 14:26:04 -04003072// This function is used to test for the correctness of the parameters passed to various constructor
3073// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074//
Olli Etuaho856c4972016-08-08 11:38:39 +03003075// 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 +00003076//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003077TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003078 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303079 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080{
Olli Etuaho856c4972016-08-08 11:38:39 +03003081 if (type.isUnsizedArray())
3082 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003083 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003084 {
3085 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3086 type.setArraySize(1u);
3087 return TIntermTyped::CreateZero(type);
3088 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003089 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003090 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003091
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003092 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003093 {
Olli Etuaho72d10202017-01-19 15:58:30 +00003094 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003095 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003096
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003097 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003098 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003100 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3101 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003102 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003103 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003104 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003105 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106}
3107
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003108//
3109// Interface/uniform blocks
3110//
Olli Etuaho13389b62016-10-16 11:48:18 +01003111TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003112 const TTypeQualifierBuilder &typeQualifierBuilder,
3113 const TSourceLoc &nameLine,
3114 const TString &blockName,
3115 TFieldList *fieldList,
3116 const TString *instanceName,
3117 const TSourceLoc &instanceLine,
3118 TIntermTyped *arrayIndex,
3119 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003120{
Olli Etuaho856c4972016-08-08 11:38:39 +03003121 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003122
Olli Etuaho77ba4082016-12-16 12:01:18 +00003123 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003124
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003125 if (typeQualifier.qualifier != EvqUniform)
3126 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003127 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3128 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003129 }
3130
Martin Radev70866b82016-07-22 15:27:42 +03003131 if (typeQualifier.invariant)
3132 {
3133 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3134 }
3135
Olli Etuaho43364892017-02-13 16:00:12 +00003136 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3137
jchen10af713a22017-04-19 09:10:56 +08003138 // add array index
3139 unsigned int arraySize = 0;
3140 if (arrayIndex != nullptr)
3141 {
3142 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3143 }
3144
3145 if (mShaderVersion < 310)
3146 {
3147 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3148 }
3149 else
3150 {
3151 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3152 arraySize);
3153 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003154
Andrei Volykhina5527072017-03-22 16:46:30 +03003155 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3156
Jamie Madill099c0f32013-06-20 11:55:52 -04003157 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003158 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003159
Jamie Madill099c0f32013-06-20 11:55:52 -04003160 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3161 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003162 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003163 }
3164
Jamie Madill1566ef72013-06-20 11:55:54 -04003165 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3166 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003167 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003168 }
3169
Olli Etuaho856c4972016-08-08 11:38:39 +03003170 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003171
Martin Radev2cc85b32016-08-05 16:22:53 +03003172 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3173
Arun Patole7e7e68d2015-05-22 12:02:25 +05303174 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3175 if (!symbolTable.declare(blockNameSymbol))
3176 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003177 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003178 }
3179
Jamie Madill98493dd2013-07-08 14:39:03 -04003180 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303181 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3182 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003183 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303184 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003185 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 {
jchen10cc2a10e2017-05-03 14:05:12 +08003187 std::string reason("unsupported type - ");
3188 reason += fieldType->getBasicString();
3189 reason += " types are not allowed in interface blocks";
3190 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003191 }
3192
Jamie Madill98493dd2013-07-08 14:39:03 -04003193 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003194 switch (qualifier)
3195 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003196 case EvqGlobal:
3197 case EvqUniform:
3198 break;
3199 default:
3200 error(field->line(), "invalid qualifier on interface block member",
3201 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003202 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003203 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003204
Martin Radev70866b82016-07-22 15:27:42 +03003205 if (fieldType->isInvariant())
3206 {
3207 error(field->line(), "invalid qualifier on interface block member", "invariant");
3208 }
3209
Jamie Madilla5efff92013-06-06 11:56:47 -04003210 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003212 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003213 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003214
Jamie Madill98493dd2013-07-08 14:39:03 -04003215 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003216 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003217 error(field->line(), "invalid layout qualifier: cannot be used here",
3218 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003219 }
3220
Jamie Madill98493dd2013-07-08 14:39:03 -04003221 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003222 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003224 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003225 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003226 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003227 warning(field->line(),
3228 "extraneous layout qualifier: only has an effect on matrix types",
3229 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003230 }
3231
Jamie Madill98493dd2013-07-08 14:39:03 -04003232 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003233 }
3234
Jamie Madillb98c3a82015-07-23 14:26:04 -04003235 TInterfaceBlock *interfaceBlock =
3236 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3237 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3238 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003239
3240 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003241 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003242
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003244 {
3245 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003246 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3247 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003248 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303249 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003250
3251 // set parent pointer of the field variable
3252 fieldType->setInterfaceBlock(interfaceBlock);
3253
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003255 fieldVariable->setQualifier(typeQualifier.qualifier);
3256
Arun Patole7e7e68d2015-05-22 12:02:25 +05303257 if (!symbolTable.declare(fieldVariable))
3258 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003259 error(field->line(), "redefinition of an interface block member name",
3260 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003261 }
3262 }
3263 }
3264 else
3265 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003266 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003267
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003268 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303269 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003270 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003271
Arun Patole7e7e68d2015-05-22 12:02:25 +05303272 if (!symbolTable.declare(instanceTypeDef))
3273 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003274 error(instanceLine, "redefinition of an interface block instance name",
3275 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003276 }
3277
Jamie Madillb98c3a82015-07-23 14:26:04 -04003278 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003279 symbolName = instanceTypeDef->getName();
3280 }
3281
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003282 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3283 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003284 TIntermDeclaration *declaration = new TIntermDeclaration();
3285 declaration->appendDeclarator(blockSymbol);
3286 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003287
3288 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003289 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003290}
3291
Olli Etuaho383b7912016-08-05 11:22:59 +03003292void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003293{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003294 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003295
3296 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003297 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303298 if (mStructNestingLevel > 1)
3299 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003300 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003301 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003302}
3303
3304void TParseContext::exitStructDeclaration()
3305{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003306 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003307}
3308
Olli Etuaho8a176262016-08-16 14:23:01 +03003309void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003310{
Jamie Madillacb4b812016-11-07 13:50:29 -05003311 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303312 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003313 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003314 }
3315
Arun Patole7e7e68d2015-05-22 12:02:25 +05303316 if (field.type()->getBasicType() != EbtStruct)
3317 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003318 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003319 }
3320
3321 // We're already inside a structure definition at this point, so add
3322 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303323 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3324 {
Jamie Madill41a49272014-03-18 16:10:13 -04003325 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003326 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3327 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003328 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003329 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003330 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003331 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003332}
3333
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003334//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003335// Parse an array index expression
3336//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003337TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3338 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303339 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003340{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003341 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3342 {
3343 if (baseExpression->getAsSymbolNode())
3344 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303345 error(location, " left of '[' is not of type array, matrix, or vector ",
3346 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003347 }
3348 else
3349 {
3350 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3351 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003352
Olli Etuaho56229f12017-07-10 14:16:33 +03003353 return TIntermTyped::CreateZero(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003354 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003355
Jamie Madill21c1e452014-12-29 11:33:41 -05003356 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3357
Olli Etuaho36b05142015-11-12 13:10:42 +02003358 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3359 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3360 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3361 // index is a constant expression.
3362 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3363 {
3364 if (baseExpression->isInterfaceBlock())
3365 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003366 error(location,
3367 "array indexes for interface blocks arrays must be constant integral expressions",
3368 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003369 }
3370 else if (baseExpression->getQualifier() == EvqFragmentOut)
3371 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003372 error(location,
3373 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003374 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003375 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3376 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003377 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003378 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003379 }
3380
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003381 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003382 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003383 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3384 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3385 // constant fold expressions that are not constant expressions). The most compatible way to
3386 // handle this case is to report a warning instead of an error and force the index to be in
3387 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003388 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003389 int index = 0;
3390 if (indexConstantUnion->getBasicType() == EbtInt)
3391 {
3392 index = indexConstantUnion->getIConst(0);
3393 }
3394 else if (indexConstantUnion->getBasicType() == EbtUInt)
3395 {
3396 index = static_cast<int>(indexConstantUnion->getUConst(0));
3397 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003398
3399 int safeIndex = -1;
3400
3401 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003402 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003403 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003404 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003405 if (mShaderSpec == SH_WEBGL2_SPEC)
3406 {
3407 // Error has been already generated if index is not const.
3408 if (indexExpression->getQualifier() == EvqConst)
3409 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003410 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003411 }
3412 safeIndex = 0;
3413 }
3414 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3415 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003416 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003417 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003418 "GL_EXT_draw_buffers is disabled",
3419 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003420 safeIndex = 0;
3421 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003422 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003423 // Only do generic out-of-range check if similar error hasn't already been reported.
3424 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003425 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003426 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3427 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003428 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003429 }
3430 }
3431 else if (baseExpression->isMatrix())
3432 {
3433 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003434 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003435 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003436 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003437 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003438 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003439 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3440 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003441 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003442 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003443
3444 ASSERT(safeIndex >= 0);
3445 // Data of constant unions can't be changed, because it may be shared with other
3446 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3447 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003448 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003450 TConstantUnion *safeConstantUnion = new TConstantUnion();
3451 safeConstantUnion->setIConst(safeIndex);
3452 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003453 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003454 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003455
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003456 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3457 node->setLine(location);
3458 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003459 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003460 else
3461 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003462 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3463 node->setLine(location);
3464 // Indirect indexing can never be constant folded.
3465 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003466 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467}
3468
Olli Etuaho90892fb2016-07-14 14:44:51 +03003469int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3470 const TSourceLoc &location,
3471 int index,
3472 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003473 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003474{
3475 if (index >= arraySize || index < 0)
3476 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003477 std::stringstream reasonStream;
3478 reasonStream << reason << " '" << index << "'";
3479 std::string token = reasonStream.str();
3480 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003481 if (index < 0)
3482 {
3483 return 0;
3484 }
3485 else
3486 {
3487 return arraySize - 1;
3488 }
3489 }
3490 return index;
3491}
3492
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3494 const TSourceLoc &dotLocation,
3495 const TString &fieldString,
3496 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003497{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498 if (baseExpression->isArray())
3499 {
3500 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003501 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003502 }
3503
3504 if (baseExpression->isVector())
3505 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003506 TVector<int> fieldOffsets;
3507 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3508 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003509 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003510 fieldOffsets.resize(1);
3511 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003512 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003513 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3514 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003515
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003516 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003517 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 else if (baseExpression->getBasicType() == EbtStruct)
3519 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303520 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003521 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003522 {
3523 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003524 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003525 }
3526 else
3527 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003528 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003529 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003531 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003532 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003533 {
3534 fieldFound = true;
3535 break;
3536 }
3537 }
3538 if (fieldFound)
3539 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003540 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3541 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003542 TIntermBinary *node =
3543 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3544 node->setLine(dotLocation);
3545 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 }
3547 else
3548 {
3549 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003550 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003551 }
3552 }
3553 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003554 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003555 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303556 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003557 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003558 {
3559 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003560 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003561 }
3562 else
3563 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003564 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003565 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003568 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003569 {
3570 fieldFound = true;
3571 break;
3572 }
3573 }
3574 if (fieldFound)
3575 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003576 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3577 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003578 TIntermBinary *node =
3579 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3580 node->setLine(dotLocation);
3581 // Indexing interface blocks can never be constant folded.
3582 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583 }
3584 else
3585 {
3586 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003587 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003588 }
3589 }
3590 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003591 else
3592 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003593 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003594 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003595 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303596 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003597 }
3598 else
3599 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303600 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003601 " field selection requires structure, vector, or interface block on left hand "
3602 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303603 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003604 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003605 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003606 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003607}
3608
Jamie Madillb98c3a82015-07-23 14:26:04 -04003609TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3610 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003611{
Martin Radev802abe02016-08-04 17:48:32 +03003612 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003613
3614 if (qualifierType == "shared")
3615 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003616 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003617 {
3618 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3619 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003620 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003621 }
3622 else if (qualifierType == "packed")
3623 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003624 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003625 {
3626 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3627 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003628 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003629 }
3630 else if (qualifierType == "std140")
3631 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003632 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003633 }
3634 else if (qualifierType == "row_major")
3635 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003636 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003637 }
3638 else if (qualifierType == "column_major")
3639 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003640 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003641 }
3642 else if (qualifierType == "location")
3643 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003644 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3645 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003646 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003647 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3648 mShaderType == GL_FRAGMENT_SHADER)
3649 {
3650 qualifier.yuv = true;
3651 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003652 else if (qualifierType == "rgba32f")
3653 {
3654 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3655 qualifier.imageInternalFormat = EiifRGBA32F;
3656 }
3657 else if (qualifierType == "rgba16f")
3658 {
3659 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3660 qualifier.imageInternalFormat = EiifRGBA16F;
3661 }
3662 else if (qualifierType == "r32f")
3663 {
3664 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3665 qualifier.imageInternalFormat = EiifR32F;
3666 }
3667 else if (qualifierType == "rgba8")
3668 {
3669 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3670 qualifier.imageInternalFormat = EiifRGBA8;
3671 }
3672 else if (qualifierType == "rgba8_snorm")
3673 {
3674 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3675 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3676 }
3677 else if (qualifierType == "rgba32i")
3678 {
3679 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3680 qualifier.imageInternalFormat = EiifRGBA32I;
3681 }
3682 else if (qualifierType == "rgba16i")
3683 {
3684 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3685 qualifier.imageInternalFormat = EiifRGBA16I;
3686 }
3687 else if (qualifierType == "rgba8i")
3688 {
3689 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3690 qualifier.imageInternalFormat = EiifRGBA8I;
3691 }
3692 else if (qualifierType == "r32i")
3693 {
3694 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3695 qualifier.imageInternalFormat = EiifR32I;
3696 }
3697 else if (qualifierType == "rgba32ui")
3698 {
3699 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3700 qualifier.imageInternalFormat = EiifRGBA32UI;
3701 }
3702 else if (qualifierType == "rgba16ui")
3703 {
3704 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3705 qualifier.imageInternalFormat = EiifRGBA16UI;
3706 }
3707 else if (qualifierType == "rgba8ui")
3708 {
3709 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3710 qualifier.imageInternalFormat = EiifRGBA8UI;
3711 }
3712 else if (qualifierType == "r32ui")
3713 {
3714 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3715 qualifier.imageInternalFormat = EiifR32UI;
3716 }
3717
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003718 else
3719 {
3720 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003721 }
3722
Jamie Madilla5efff92013-06-06 11:56:47 -04003723 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003724}
3725
Martin Radev802abe02016-08-04 17:48:32 +03003726void TParseContext::parseLocalSize(const TString &qualifierType,
3727 const TSourceLoc &qualifierTypeLine,
3728 int intValue,
3729 const TSourceLoc &intValueLine,
3730 const std::string &intValueString,
3731 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003732 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003733{
Olli Etuaho856c4972016-08-08 11:38:39 +03003734 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003735 if (intValue < 1)
3736 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003737 std::stringstream reasonStream;
3738 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3739 std::string reason = reasonStream.str();
3740 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003741 }
3742 (*localSize)[index] = intValue;
3743}
3744
Olli Etuaho09b04a22016-12-15 13:30:26 +00003745void TParseContext::parseNumViews(int intValue,
3746 const TSourceLoc &intValueLine,
3747 const std::string &intValueString,
3748 int *numViews)
3749{
3750 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3751 // specification.
3752 if (intValue < 1)
3753 {
3754 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3755 }
3756 *numViews = intValue;
3757}
3758
Jamie Madillb98c3a82015-07-23 14:26:04 -04003759TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3760 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003761 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303762 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003763{
Martin Radev802abe02016-08-04 17:48:32 +03003764 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003765
Martin Radev802abe02016-08-04 17:48:32 +03003766 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003767
Martin Radev802abe02016-08-04 17:48:32 +03003768 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003769 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003770 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003771 if (intValue < 0)
3772 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003773 error(intValueLine, "out of range: location must be non-negative",
3774 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003775 }
3776 else
3777 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003778 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003779 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003780 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003781 }
Olli Etuaho43364892017-02-13 16:00:12 +00003782 else if (qualifierType == "binding")
3783 {
3784 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3785 if (intValue < 0)
3786 {
3787 error(intValueLine, "out of range: binding must be non-negative",
3788 intValueString.c_str());
3789 }
3790 else
3791 {
3792 qualifier.binding = intValue;
3793 }
3794 }
jchen104cdac9e2017-05-08 11:01:20 +08003795 else if (qualifierType == "offset")
3796 {
3797 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3798 if (intValue < 0)
3799 {
3800 error(intValueLine, "out of range: offset must be non-negative",
3801 intValueString.c_str());
3802 }
3803 else
3804 {
3805 qualifier.offset = intValue;
3806 }
3807 }
Martin Radev802abe02016-08-04 17:48:32 +03003808 else if (qualifierType == "local_size_x")
3809 {
3810 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3811 &qualifier.localSize);
3812 }
3813 else if (qualifierType == "local_size_y")
3814 {
3815 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3816 &qualifier.localSize);
3817 }
3818 else if (qualifierType == "local_size_z")
3819 {
3820 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3821 &qualifier.localSize);
3822 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003823 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003824 mShaderType == GL_VERTEX_SHADER)
3825 {
3826 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3827 }
Martin Radev802abe02016-08-04 17:48:32 +03003828 else
3829 {
3830 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003831 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003832
Jamie Madilla5efff92013-06-06 11:56:47 -04003833 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003834}
3835
Olli Etuaho613b9592016-09-05 12:05:53 +03003836TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3837{
3838 return new TTypeQualifierBuilder(
3839 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3840 mShaderVersion);
3841}
3842
Olli Etuahocce89652017-06-19 16:04:09 +03003843TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3844 const TSourceLoc &loc)
3845{
3846 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3847 return new TStorageQualifierWrapper(qualifier, loc);
3848}
3849
3850TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3851{
3852 if (getShaderType() == GL_VERTEX_SHADER)
3853 {
3854 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3855 }
3856 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3857}
3858
3859TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3860{
3861 if (declaringFunction())
3862 {
3863 return new TStorageQualifierWrapper(EvqIn, loc);
3864 }
3865 if (getShaderType() == GL_FRAGMENT_SHADER)
3866 {
3867 if (mShaderVersion < 300)
3868 {
3869 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3870 }
3871 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3872 }
3873 if (getShaderType() == GL_VERTEX_SHADER)
3874 {
3875 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3876 {
3877 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3878 }
3879 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3880 }
3881 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3882}
3883
3884TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3885{
3886 if (declaringFunction())
3887 {
3888 return new TStorageQualifierWrapper(EvqOut, loc);
3889 }
3890 if (mShaderVersion < 300)
3891 {
3892 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3893 }
3894 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3895 {
3896 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3897 }
3898 if (getShaderType() == GL_VERTEX_SHADER)
3899 {
3900 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3901 }
3902 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3903}
3904
3905TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3906{
3907 if (!declaringFunction())
3908 {
3909 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3910 }
3911 return new TStorageQualifierWrapper(EvqInOut, loc);
3912}
3913
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003915 TLayoutQualifier rightQualifier,
3916 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003917{
Martin Radevc28888b2016-07-22 15:27:42 +03003918 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003919 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003920}
3921
Olli Etuahocce89652017-06-19 16:04:09 +03003922TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3923{
3924 checkIsNotReserved(loc, *identifier);
3925 TType *type = new TType(EbtVoid, EbpUndefined);
3926 return new TField(type, identifier, loc);
3927}
3928
3929TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3930 const TSourceLoc &loc,
3931 TIntermTyped *arraySize,
3932 const TSourceLoc &arraySizeLoc)
3933{
3934 checkIsNotReserved(loc, *identifier);
3935
3936 TType *type = new TType(EbtVoid, EbpUndefined);
3937 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3938 type->setArraySize(size);
3939
3940 return new TField(type, identifier, loc);
3941}
3942
Olli Etuaho4de340a2016-12-16 09:32:03 +00003943TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3944 const TFieldList *newlyAddedFields,
3945 const TSourceLoc &location)
3946{
3947 for (TField *field : *newlyAddedFields)
3948 {
3949 for (TField *oldField : *processedFields)
3950 {
3951 if (oldField->name() == field->name())
3952 {
3953 error(location, "duplicate field name in structure", field->name().c_str());
3954 }
3955 }
3956 processedFields->push_back(field);
3957 }
3958 return processedFields;
3959}
3960
Martin Radev70866b82016-07-22 15:27:42 +03003961TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3962 const TTypeQualifierBuilder &typeQualifierBuilder,
3963 TPublicType *typeSpecifier,
3964 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003965{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003966 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003967
Martin Radev70866b82016-07-22 15:27:42 +03003968 typeSpecifier->qualifier = typeQualifier.qualifier;
3969 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003970 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003971 typeSpecifier->invariant = typeQualifier.invariant;
3972 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303973 {
Martin Radev70866b82016-07-22 15:27:42 +03003974 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003975 }
Martin Radev70866b82016-07-22 15:27:42 +03003976 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003977}
3978
Jamie Madillb98c3a82015-07-23 14:26:04 -04003979TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3980 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003981{
Martin Radev4a9cd802016-09-01 16:51:51 +03003982 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3983 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003984
Martin Radev4a9cd802016-09-01 16:51:51 +03003985 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003986
Martin Radev4a9cd802016-09-01 16:51:51 +03003987 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003988
Arun Patole7e7e68d2015-05-22 12:02:25 +05303989 for (unsigned int i = 0; i < fieldList->size(); ++i)
3990 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003991 //
3992 // Careful not to replace already known aspects of type, like array-ness
3993 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303994 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003995 type->setBasicType(typeSpecifier.getBasicType());
3996 type->setPrimarySize(typeSpecifier.getPrimarySize());
3997 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003998 type->setPrecision(typeSpecifier.precision);
3999 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004000 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004001 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004002 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004003
4004 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304005 if (type->isArray())
4006 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004007 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004008 }
4009 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004010 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004011 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304012 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004013 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004014 }
4015
Martin Radev4a9cd802016-09-01 16:51:51 +03004016 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017 }
4018
Jamie Madill98493dd2013-07-08 14:39:03 -04004019 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004020}
4021
Martin Radev4a9cd802016-09-01 16:51:51 +03004022TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4023 const TSourceLoc &nameLine,
4024 const TString *structName,
4025 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304027 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004028 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029
Jamie Madill9b820842015-02-12 10:40:10 -05004030 // Store a bool in the struct if we're at global scope, to allow us to
4031 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004032 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004033
Jamie Madill98493dd2013-07-08 14:39:03 -04004034 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004035 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004036 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304037 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4038 if (!symbolTable.declare(userTypeDef))
4039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004040 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004041 }
4042 }
4043
4044 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004045 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004046 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004048 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004049 switch (qualifier)
4050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004051 case EvqGlobal:
4052 case EvqTemporary:
4053 break;
4054 default:
4055 error(field.line(), "invalid qualifier on struct member",
4056 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004057 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004058 }
Martin Radev70866b82016-07-22 15:27:42 +03004059 if (field.type()->isInvariant())
4060 {
4061 error(field.line(), "invalid qualifier on struct member", "invariant");
4062 }
jchen104cdac9e2017-05-08 11:01:20 +08004063 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4064 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004065 {
4066 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4067 }
4068
Olli Etuaho43364892017-02-13 16:00:12 +00004069 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4070
4071 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004072
4073 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074 }
4075
Martin Radev4a9cd802016-09-01 16:51:51 +03004076 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004077 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 exitStructDeclaration();
4079
Martin Radev4a9cd802016-09-01 16:51:51 +03004080 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081}
4082
Jamie Madillb98c3a82015-07-23 14:26:04 -04004083TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004084 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004085 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004086{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004087 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004088 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004089 init->isVector())
4090 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004091 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4092 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004093 return nullptr;
4094 }
4095
Olli Etuahoac5274d2015-02-20 10:19:08 +02004096 if (statementList)
4097 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004098 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004099 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004100 return nullptr;
4101 }
4102 }
4103
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004104 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4105 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004106 return node;
4107}
4108
4109TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4110{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004111 if (mSwitchNestingLevel == 0)
4112 {
4113 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004114 return nullptr;
4115 }
4116 if (condition == nullptr)
4117 {
4118 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004119 return nullptr;
4120 }
4121 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004122 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004123 {
4124 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004125 }
4126 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004127 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4128 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4129 // fold in case labels.
4130 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004131 {
4132 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004133 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004134 TIntermCase *node = new TIntermCase(condition);
4135 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004136 return node;
4137}
4138
4139TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4140{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004141 if (mSwitchNestingLevel == 0)
4142 {
4143 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004144 return nullptr;
4145 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004146 TIntermCase *node = new TIntermCase(nullptr);
4147 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004148 return node;
4149}
4150
Jamie Madillb98c3a82015-07-23 14:26:04 -04004151TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4152 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004153 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004154{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004155 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004156
4157 switch (op)
4158 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004159 case EOpLogicalNot:
4160 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4161 child->isVector())
4162 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004163 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004164 return nullptr;
4165 }
4166 break;
4167 case EOpBitwiseNot:
4168 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4169 child->isMatrix() || child->isArray())
4170 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004171 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004172 return nullptr;
4173 }
4174 break;
4175 case EOpPostIncrement:
4176 case EOpPreIncrement:
4177 case EOpPostDecrement:
4178 case EOpPreDecrement:
4179 case EOpNegative:
4180 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004181 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4182 child->getBasicType() == EbtBool || child->isArray() ||
4183 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004184 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004185 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004186 return nullptr;
4187 }
4188 // Operators for built-ins are already type checked against their prototype.
4189 default:
4190 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004191 }
4192
Olli Etuahof119a262016-08-19 15:54:22 +03004193 TIntermUnary *node = new TIntermUnary(op, child);
4194 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004195
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004196 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004197}
4198
Olli Etuaho09b22472015-02-11 11:47:26 +02004199TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4200{
Olli Etuahocce89652017-06-19 16:04:09 +03004201 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004202 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004203 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004204 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004205 return child;
4206 }
4207 return node;
4208}
4209
Jamie Madillb98c3a82015-07-23 14:26:04 -04004210TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4211 TIntermTyped *child,
4212 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004213{
Olli Etuaho856c4972016-08-08 11:38:39 +03004214 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004215 return addUnaryMath(op, child, loc);
4216}
4217
Jamie Madillb98c3a82015-07-23 14:26:04 -04004218bool TParseContext::binaryOpCommonCheck(TOperator op,
4219 TIntermTyped *left,
4220 TIntermTyped *right,
4221 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004222{
jchen10b4cf5652017-05-05 18:51:17 +08004223 // Check opaque types are not allowed to be operands in expressions other than array indexing
4224 // and structure member selection.
4225 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4226 {
4227 switch (op)
4228 {
4229 case EOpIndexDirect:
4230 case EOpIndexIndirect:
4231 break;
4232 case EOpIndexDirectStruct:
4233 UNREACHABLE();
4234
4235 default:
4236 error(loc, "Invalid operation for variables with an opaque type",
4237 GetOperatorString(op));
4238 return false;
4239 }
4240 }
jchen10cc2a10e2017-05-03 14:05:12 +08004241
Olli Etuaho244be012016-08-18 15:26:02 +03004242 if (left->getType().getStruct() || right->getType().getStruct())
4243 {
4244 switch (op)
4245 {
4246 case EOpIndexDirectStruct:
4247 ASSERT(left->getType().getStruct());
4248 break;
4249 case EOpEqual:
4250 case EOpNotEqual:
4251 case EOpAssign:
4252 case EOpInitialize:
4253 if (left->getType() != right->getType())
4254 {
4255 return false;
4256 }
4257 break;
4258 default:
4259 error(loc, "Invalid operation for structs", GetOperatorString(op));
4260 return false;
4261 }
4262 }
4263
Olli Etuaho94050052017-05-08 14:17:44 +03004264 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4265 {
4266 switch (op)
4267 {
4268 case EOpIndexDirectInterfaceBlock:
4269 ASSERT(left->getType().getInterfaceBlock());
4270 break;
4271 default:
4272 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4273 return false;
4274 }
4275 }
4276
Olli Etuahod6b14282015-03-17 14:31:35 +02004277 if (left->isArray() || right->isArray())
4278 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004279 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004280 {
4281 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4282 return false;
4283 }
4284
4285 if (left->isArray() != right->isArray())
4286 {
4287 error(loc, "array / non-array mismatch", GetOperatorString(op));
4288 return false;
4289 }
4290
4291 switch (op)
4292 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004293 case EOpEqual:
4294 case EOpNotEqual:
4295 case EOpAssign:
4296 case EOpInitialize:
4297 break;
4298 default:
4299 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4300 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004301 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004302 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004303 if (left->getArraySize() != right->getArraySize())
4304 {
4305 error(loc, "array size mismatch", GetOperatorString(op));
4306 return false;
4307 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004308 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004309
4310 // Check ops which require integer / ivec parameters
4311 bool isBitShift = false;
4312 switch (op)
4313 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004314 case EOpBitShiftLeft:
4315 case EOpBitShiftRight:
4316 case EOpBitShiftLeftAssign:
4317 case EOpBitShiftRightAssign:
4318 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4319 // check that the basic type is an integer type.
4320 isBitShift = true;
4321 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4322 {
4323 return false;
4324 }
4325 break;
4326 case EOpBitwiseAnd:
4327 case EOpBitwiseXor:
4328 case EOpBitwiseOr:
4329 case EOpBitwiseAndAssign:
4330 case EOpBitwiseXorAssign:
4331 case EOpBitwiseOrAssign:
4332 // It is enough to check the type of only one operand, since later it
4333 // is checked that the operand types match.
4334 if (!IsInteger(left->getBasicType()))
4335 {
4336 return false;
4337 }
4338 break;
4339 default:
4340 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004341 }
4342
4343 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4344 // So the basic type should usually match.
4345 if (!isBitShift && left->getBasicType() != right->getBasicType())
4346 {
4347 return false;
4348 }
4349
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004350 // Check that:
4351 // 1. Type sizes match exactly on ops that require that.
4352 // 2. Restrictions for structs that contain arrays or samplers are respected.
4353 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004354 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004356 case EOpAssign:
4357 case EOpInitialize:
4358 case EOpEqual:
4359 case EOpNotEqual:
4360 // ESSL 1.00 sections 5.7, 5.8, 5.9
4361 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4362 {
4363 error(loc, "undefined operation for structs containing arrays",
4364 GetOperatorString(op));
4365 return false;
4366 }
4367 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4368 // we interpret the spec so that this extends to structs containing samplers,
4369 // similarly to ESSL 1.00 spec.
4370 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4371 left->getType().isStructureContainingSamplers())
4372 {
4373 error(loc, "undefined operation for structs containing samplers",
4374 GetOperatorString(op));
4375 return false;
4376 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004377
Olli Etuahoe1805592017-01-02 16:41:20 +00004378 if ((left->getNominalSize() != right->getNominalSize()) ||
4379 (left->getSecondarySize() != right->getSecondarySize()))
4380 {
4381 error(loc, "dimension mismatch", GetOperatorString(op));
4382 return false;
4383 }
4384 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004385 case EOpLessThan:
4386 case EOpGreaterThan:
4387 case EOpLessThanEqual:
4388 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004389 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004390 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004391 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004392 return false;
4393 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004394 break;
4395 case EOpAdd:
4396 case EOpSub:
4397 case EOpDiv:
4398 case EOpIMod:
4399 case EOpBitShiftLeft:
4400 case EOpBitShiftRight:
4401 case EOpBitwiseAnd:
4402 case EOpBitwiseXor:
4403 case EOpBitwiseOr:
4404 case EOpAddAssign:
4405 case EOpSubAssign:
4406 case EOpDivAssign:
4407 case EOpIModAssign:
4408 case EOpBitShiftLeftAssign:
4409 case EOpBitShiftRightAssign:
4410 case EOpBitwiseAndAssign:
4411 case EOpBitwiseXorAssign:
4412 case EOpBitwiseOrAssign:
4413 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4414 {
4415 return false;
4416 }
4417
4418 // Are the sizes compatible?
4419 if (left->getNominalSize() != right->getNominalSize() ||
4420 left->getSecondarySize() != right->getSecondarySize())
4421 {
4422 // If the nominal sizes of operands do not match:
4423 // One of them must be a scalar.
4424 if (!left->isScalar() && !right->isScalar())
4425 return false;
4426
4427 // In the case of compound assignment other than multiply-assign,
4428 // the right side needs to be a scalar. Otherwise a vector/matrix
4429 // would be assigned to a scalar. A scalar can't be shifted by a
4430 // vector either.
4431 if (!right->isScalar() &&
4432 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4433 return false;
4434 }
4435 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004436 default:
4437 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004438 }
4439
Olli Etuahod6b14282015-03-17 14:31:35 +02004440 return true;
4441}
4442
Olli Etuaho1dded802016-08-18 18:13:13 +03004443bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4444 const TType &left,
4445 const TType &right)
4446{
4447 switch (op)
4448 {
4449 case EOpMul:
4450 case EOpMulAssign:
4451 return left.getNominalSize() == right.getNominalSize() &&
4452 left.getSecondarySize() == right.getSecondarySize();
4453 case EOpVectorTimesScalar:
4454 return true;
4455 case EOpVectorTimesScalarAssign:
4456 ASSERT(!left.isMatrix() && !right.isMatrix());
4457 return left.isVector() && !right.isVector();
4458 case EOpVectorTimesMatrix:
4459 return left.getNominalSize() == right.getRows();
4460 case EOpVectorTimesMatrixAssign:
4461 ASSERT(!left.isMatrix() && right.isMatrix());
4462 return left.isVector() && left.getNominalSize() == right.getRows() &&
4463 left.getNominalSize() == right.getCols();
4464 case EOpMatrixTimesVector:
4465 return left.getCols() == right.getNominalSize();
4466 case EOpMatrixTimesScalar:
4467 return true;
4468 case EOpMatrixTimesScalarAssign:
4469 ASSERT(left.isMatrix() && !right.isMatrix());
4470 return !right.isVector();
4471 case EOpMatrixTimesMatrix:
4472 return left.getCols() == right.getRows();
4473 case EOpMatrixTimesMatrixAssign:
4474 ASSERT(left.isMatrix() && right.isMatrix());
4475 // We need to check two things:
4476 // 1. The matrix multiplication step is valid.
4477 // 2. The result will have the same number of columns as the lvalue.
4478 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4479
4480 default:
4481 UNREACHABLE();
4482 return false;
4483 }
4484}
4485
Jamie Madillb98c3a82015-07-23 14:26:04 -04004486TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4487 TIntermTyped *left,
4488 TIntermTyped *right,
4489 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004490{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004491 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004492 return nullptr;
4493
Olli Etuahofc1806e2015-03-17 13:03:11 +02004494 switch (op)
4495 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004496 case EOpEqual:
4497 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004498 case EOpLessThan:
4499 case EOpGreaterThan:
4500 case EOpLessThanEqual:
4501 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004502 break;
4503 case EOpLogicalOr:
4504 case EOpLogicalXor:
4505 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004506 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4507 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004508 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004509 {
4510 return nullptr;
4511 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004512 // Basic types matching should have been already checked.
4513 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004514 break;
4515 case EOpAdd:
4516 case EOpSub:
4517 case EOpDiv:
4518 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004519 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4520 !right->getType().getStruct());
4521 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004522 {
4523 return nullptr;
4524 }
4525 break;
4526 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004527 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4528 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004529 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004530 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004531 {
4532 return nullptr;
4533 }
4534 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004535 default:
4536 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004537 }
4538
Olli Etuaho1dded802016-08-18 18:13:13 +03004539 if (op == EOpMul)
4540 {
4541 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4542 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4543 {
4544 return nullptr;
4545 }
4546 }
4547
Olli Etuaho3fdec912016-08-18 15:08:06 +03004548 TIntermBinary *node = new TIntermBinary(op, left, right);
4549 node->setLine(loc);
4550
Olli Etuaho3fdec912016-08-18 15:08:06 +03004551 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004552 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004553}
4554
Jamie Madillb98c3a82015-07-23 14:26:04 -04004555TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4556 TIntermTyped *left,
4557 TIntermTyped *right,
4558 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004559{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004560 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004561 if (node == 0)
4562 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004563 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4564 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004565 return left;
4566 }
4567 return node;
4568}
4569
Jamie Madillb98c3a82015-07-23 14:26:04 -04004570TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4571 TIntermTyped *left,
4572 TIntermTyped *right,
4573 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004574{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004575 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004576 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004577 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004578 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4579 right->getCompleteString());
Olli Etuaho56229f12017-07-10 14:16:33 +03004580 node = TIntermTyped::CreateZero(TType(EbtBool, EbpUndefined, EvqConst));
4581 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004582 }
4583 return node;
4584}
4585
Olli Etuaho13389b62016-10-16 11:48:18 +01004586TIntermBinary *TParseContext::createAssign(TOperator op,
4587 TIntermTyped *left,
4588 TIntermTyped *right,
4589 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004590{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004591 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004592 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004593 if (op == EOpMulAssign)
4594 {
4595 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4596 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4597 {
4598 return nullptr;
4599 }
4600 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004601 TIntermBinary *node = new TIntermBinary(op, left, right);
4602 node->setLine(loc);
4603
Olli Etuaho3fdec912016-08-18 15:08:06 +03004604 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004605 }
4606 return nullptr;
4607}
4608
Jamie Madillb98c3a82015-07-23 14:26:04 -04004609TIntermTyped *TParseContext::addAssign(TOperator op,
4610 TIntermTyped *left,
4611 TIntermTyped *right,
4612 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004613{
Olli Etuahocce89652017-06-19 16:04:09 +03004614 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004615 TIntermTyped *node = createAssign(op, left, right, loc);
4616 if (node == nullptr)
4617 {
4618 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004619 return left;
4620 }
4621 return node;
4622}
4623
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004624TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4625 TIntermTyped *right,
4626 const TSourceLoc &loc)
4627{
Corentin Wallez0d959252016-07-12 17:26:32 -04004628 // WebGL2 section 5.26, the following results in an error:
4629 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004630 if (mShaderSpec == SH_WEBGL2_SPEC &&
4631 (left->isArray() || left->getBasicType() == EbtVoid ||
4632 left->getType().isStructureContainingArrays() || right->isArray() ||
4633 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004634 {
4635 error(loc,
4636 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4637 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004638 }
4639
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004640 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4641 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4642 commaNode->getTypePointer()->setQualifier(resultQualifier);
4643 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004644}
4645
Olli Etuaho49300862015-02-20 14:54:49 +02004646TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4647{
4648 switch (op)
4649 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004650 case EOpContinue:
4651 if (mLoopNestingLevel <= 0)
4652 {
4653 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004654 }
4655 break;
4656 case EOpBreak:
4657 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4658 {
4659 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660 }
4661 break;
4662 case EOpReturn:
4663 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4664 {
4665 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004666 }
4667 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004668 case EOpKill:
4669 if (mShaderType != GL_FRAGMENT_SHADER)
4670 {
4671 error(loc, "discard supported in fragment shaders only", "discard");
4672 }
4673 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004674 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004675 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004676 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004677 }
Olli Etuahocce89652017-06-19 16:04:09 +03004678 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004679}
4680
Jamie Madillb98c3a82015-07-23 14:26:04 -04004681TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004682 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004684{
Olli Etuahocce89652017-06-19 16:04:09 +03004685 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004686 {
Olli Etuahocce89652017-06-19 16:04:09 +03004687 ASSERT(op == EOpReturn);
4688 mFunctionReturnsValue = true;
4689 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4690 {
4691 error(loc, "void function cannot return a value", "return");
4692 }
4693 else if (*mCurrentFunctionType != expression->getType())
4694 {
4695 error(loc, "function return is not matching type:", "return");
4696 }
Olli Etuaho49300862015-02-20 14:54:49 +02004697 }
Olli Etuahocce89652017-06-19 16:04:09 +03004698 TIntermBranch *node = new TIntermBranch(op, expression);
4699 node->setLine(loc);
4700 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004701}
4702
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004703void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4704{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004705 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004706 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004707 TIntermNode *offset = nullptr;
4708 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004709 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4710 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4711 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004712 {
4713 offset = arguments->back();
4714 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004715 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004716 {
4717 // A bias parameter might follow the offset parameter.
4718 ASSERT(arguments->size() >= 3);
4719 offset = (*arguments)[2];
4720 }
4721 if (offset != nullptr)
4722 {
4723 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4724 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4725 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004726 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004727 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004728 }
4729 else
4730 {
4731 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4732 size_t size = offsetConstantUnion->getType().getObjectSize();
4733 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4734 for (size_t i = 0u; i < size; ++i)
4735 {
4736 int offsetValue = values[i].getIConst();
4737 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4738 {
4739 std::stringstream tokenStream;
4740 tokenStream << offsetValue;
4741 std::string token = tokenStream.str();
4742 error(offset->getLine(), "Texture offset value out of valid range",
4743 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004744 }
4745 }
4746 }
4747 }
4748}
4749
Martin Radev2cc85b32016-08-05 16:22:53 +03004750// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4751void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4752{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004753 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004754 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4755
4756 if (name.compare(0, 5, "image") == 0)
4757 {
4758 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004759 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004760
Olli Etuaho485eefd2017-02-14 17:40:06 +00004761 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004762
4763 if (name.compare(5, 5, "Store") == 0)
4764 {
4765 if (memoryQualifier.readonly)
4766 {
4767 error(imageNode->getLine(),
4768 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004769 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004770 }
4771 }
4772 else if (name.compare(5, 4, "Load") == 0)
4773 {
4774 if (memoryQualifier.writeonly)
4775 {
4776 error(imageNode->getLine(),
4777 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004778 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004779 }
4780 }
4781 }
4782}
4783
4784// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4785void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4786 const TFunction *functionDefinition,
4787 const TIntermAggregate *functionCall)
4788{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004789 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004790
4791 const TIntermSequence &arguments = *functionCall->getSequence();
4792
4793 ASSERT(functionDefinition->getParamCount() == arguments.size());
4794
4795 for (size_t i = 0; i < arguments.size(); ++i)
4796 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004797 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4798 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004799 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4800 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4801
4802 if (IsImage(functionArgumentType.getBasicType()))
4803 {
4804 const TMemoryQualifier &functionArgumentMemoryQualifier =
4805 functionArgumentType.getMemoryQualifier();
4806 const TMemoryQualifier &functionParameterMemoryQualifier =
4807 functionParameterType.getMemoryQualifier();
4808 if (functionArgumentMemoryQualifier.readonly &&
4809 !functionParameterMemoryQualifier.readonly)
4810 {
4811 error(functionCall->getLine(),
4812 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004813 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004814 }
4815
4816 if (functionArgumentMemoryQualifier.writeonly &&
4817 !functionParameterMemoryQualifier.writeonly)
4818 {
4819 error(functionCall->getLine(),
4820 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004821 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004822 }
Martin Radev049edfa2016-11-11 14:35:37 +02004823
4824 if (functionArgumentMemoryQualifier.coherent &&
4825 !functionParameterMemoryQualifier.coherent)
4826 {
4827 error(functionCall->getLine(),
4828 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004829 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004830 }
4831
4832 if (functionArgumentMemoryQualifier.volatileQualifier &&
4833 !functionParameterMemoryQualifier.volatileQualifier)
4834 {
4835 error(functionCall->getLine(),
4836 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004837 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004838 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004839 }
4840 }
4841}
4842
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004843TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004844{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004845 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004846}
4847
4848TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004849 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004850 TIntermNode *thisNode,
4851 const TSourceLoc &loc)
4852{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004853 if (thisNode != nullptr)
4854 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004855 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004856 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004857
4858 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004859 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004860 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004861 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004862 }
4863 else
4864 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004865 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004866 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4867 }
4868}
4869
4870TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4871 TIntermSequence *arguments,
4872 TIntermNode *thisNode,
4873 const TSourceLoc &loc)
4874{
4875 TConstantUnion *unionArray = new TConstantUnion[1];
4876 int arraySize = 0;
4877 TIntermTyped *typedThis = thisNode->getAsTyped();
4878 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4879 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4880 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4881 // So accessing fnCall->getName() below is safe.
4882 if (fnCall->getName() != "length")
4883 {
4884 error(loc, "invalid method", fnCall->getName().c_str());
4885 }
4886 else if (!arguments->empty())
4887 {
4888 error(loc, "method takes no parameters", "length");
4889 }
4890 else if (typedThis == nullptr || !typedThis->isArray())
4891 {
4892 error(loc, "length can only be called on arrays", "length");
4893 }
4894 else
4895 {
4896 arraySize = typedThis->getArraySize();
4897 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004898 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004899 // This code path can be hit with expressions like these:
4900 // (a = b).length()
4901 // (func()).length()
4902 // (int[3](0, 1, 2)).length()
4903 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4904 // expression.
4905 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4906 // spec section 5.9 which allows "An array, vector or matrix expression with the
4907 // length method applied".
4908 error(loc, "length can only be called on array names, not on array expressions",
4909 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004910 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004911 }
4912 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03004913 TIntermConstantUnion *node =
4914 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
4915 node->setLine(loc);
4916 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004917}
4918
4919TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4920 TIntermSequence *arguments,
4921 const TSourceLoc &loc)
4922{
4923 // First find by unmangled name to check whether the function name has been
4924 // hidden by a variable name or struct typename.
4925 // If a function is found, check for one with a matching argument list.
4926 bool builtIn;
4927 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4928 if (symbol != nullptr && !symbol->isFunction())
4929 {
4930 error(loc, "function name expected", fnCall->getName().c_str());
4931 }
4932 else
4933 {
4934 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4935 mShaderVersion, &builtIn);
4936 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004937 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004938 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4939 }
4940 else
4941 {
4942 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004943 //
4944 // A declared function.
4945 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004946 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004947 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004948 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004949 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004950 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004951 if (builtIn && op != EOpNull)
4952 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004953 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004954 if (fnCandidate->getParamCount() == 1)
4955 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004956 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004957 TIntermNode *unaryParamNode = arguments->front();
4958 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004959 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004960 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004961 }
4962 else
4963 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004964 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004965 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004966 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004967
4968 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004969 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304970
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004971 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05304972 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004973 // See if we can constant fold a built-in. Note that this may be possible
4974 // even if it is not const-qualified.
4975 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304976 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004977 else
4978 {
4979 return callNode;
4980 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004981 }
4982 }
4983 else
4984 {
4985 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004986 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004987
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004988 // If builtIn == false, the function is user defined - could be an overloaded
4989 // built-in as well.
4990 // if builtIn == true, it's a builtIn function with no op associated with it.
4991 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004992 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004993 {
Olli Etuahofe486322017-03-21 09:30:54 +00004994 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004995 checkTextureOffsetConst(callNode);
4996 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004997 }
4998 else
4999 {
Olli Etuahofe486322017-03-21 09:30:54 +00005000 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005001 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005002 }
5003
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005004 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005005
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005006 callNode->setLine(loc);
5007
5008 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005009 }
5010 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005011 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005012
5013 // Error message was already written. Put on a dummy node for error recovery.
5014 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005015}
5016
Jamie Madillb98c3a82015-07-23 14:26:04 -04005017TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005018 TIntermTyped *trueExpression,
5019 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005020 const TSourceLoc &loc)
5021{
Olli Etuaho56229f12017-07-10 14:16:33 +03005022 if (!checkIsScalarBool(loc, cond))
5023 {
5024 return falseExpression;
5025 }
Olli Etuaho52901742015-04-15 13:42:45 +03005026
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005027 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005028 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005029 std::stringstream reasonStream;
5030 reasonStream << "mismatching ternary operator operand types '"
5031 << trueExpression->getCompleteString() << " and '"
5032 << falseExpression->getCompleteString() << "'";
5033 std::string reason = reasonStream.str();
5034 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005035 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005036 }
Olli Etuahode318b22016-10-25 16:18:25 +01005037 if (IsOpaqueType(trueExpression->getBasicType()))
5038 {
5039 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005040 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005041 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5042 // Note that structs containing opaque types don't need to be checked as structs are
5043 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005044 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005045 return falseExpression;
5046 }
5047
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005048 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005049 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005050 // ESSL 3.00.6 section 5.7:
5051 // Ternary operator support is optional for arrays. No certainty that it works across all
5052 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5053 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005054 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005055 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005056 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005057 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005058 }
Olli Etuaho94050052017-05-08 14:17:44 +03005059 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5060 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005061 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005062 return falseExpression;
5063 }
5064
Corentin Wallez0d959252016-07-12 17:26:32 -04005065 // WebGL2 section 5.26, the following results in an error:
5066 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005067 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005068 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005069 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005070 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005071 }
5072
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005073 // Note that the node resulting from here can be a constant union without being qualified as
5074 // constant.
5075 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5076 node->setLine(loc);
5077
5078 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005079}
Olli Etuaho49300862015-02-20 14:54:49 +02005080
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005081//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005082// Parse an array of strings using yyparse.
5083//
5084// Returns 0 for success.
5085//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005086int PaParseStrings(size_t count,
5087 const char *const string[],
5088 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305089 TParseContext *context)
5090{
Yunchao He4f285442017-04-21 12:15:49 +08005091 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005092 return 1;
5093
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005094 if (glslang_initialize(context))
5095 return 1;
5096
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005097 int error = glslang_scan(count, string, length, context);
5098 if (!error)
5099 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005100
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005101 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005102
alokp@chromium.org6b495712012-06-29 00:06:58 +00005103 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005104}
Jamie Madill45bcc782016-11-07 13:58:48 -05005105
5106} // namespace sh