blob: af722ce2f82357641c973c4e529c9ca9d90890e3 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
35bool ContainsSampler(const TType &type)
36{
37 if (IsSampler(type.getBasicType()))
38 return true;
39
jchen10cc2a10e2017-05-03 14:05:12 +080040 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030041 {
42 const TFieldList &fields = type.getStruct()->fields();
43 for (unsigned int i = 0; i < fields.size(); ++i)
44 {
45 if (ContainsSampler(*fields[i]->type()))
46 return true;
47 }
48 }
49
50 return false;
51}
52
Olli Etuaho485eefd2017-02-14 17:40:06 +000053// Get a token from an image argument to use as an error message token.
54const char *GetImageArgumentToken(TIntermTyped *imageNode)
55{
56 ASSERT(IsImage(imageNode->getBasicType()));
57 while (imageNode->getAsBinaryNode() &&
58 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
59 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
60 {
61 imageNode = imageNode->getAsBinaryNode()->getLeft();
62 }
63 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
64 if (imageSymbol)
65 {
66 return imageSymbol->getSymbol().c_str();
67 }
68 return "image";
69}
70
Olli Etuahocce89652017-06-19 16:04:09 +030071bool CanSetDefaultPrecisionOnType(const TPublicType &type)
72{
73 if (!SupportsPrecision(type.getBasicType()))
74 {
75 return false;
76 }
77 if (type.getBasicType() == EbtUInt)
78 {
79 // ESSL 3.00.4 section 4.5.4
80 return false;
81 }
82 if (type.isAggregate())
83 {
84 // Not allowed to set for aggregate types
85 return false;
86 }
87 return true;
88}
89
Martin Radev2cc85b32016-08-05 16:22:53 +030090} // namespace
91
jchen104cdac9e2017-05-08 11:01:20 +080092// This tracks each binding point's current default offset for inheritance of subsequent
93// variables using the same binding, and keeps offsets unique and non overlapping.
94// See GLSL ES 3.1, section 4.4.6.
95class TParseContext::AtomicCounterBindingState
96{
97 public:
98 AtomicCounterBindingState() : mDefaultOffset(0) {}
99 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
100 // newly inserted span.
101 int insertSpan(int start, size_t length)
102 {
103 gl::RangeI newSpan(start, start + static_cast<int>(length));
104 for (const auto &span : mSpans)
105 {
106 if (newSpan.intersects(span))
107 {
108 return -1;
109 }
110 }
111 mSpans.push_back(newSpan);
112 mDefaultOffset = newSpan.high();
113 return start;
114 }
115 // Inserts a new span starting from the default offset.
116 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
117 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
118
119 private:
120 int mDefaultOffset;
121 std::vector<gl::RangeI> mSpans;
122};
123
Jamie Madillacb4b812016-11-07 13:50:29 -0500124TParseContext::TParseContext(TSymbolTable &symt,
125 TExtensionBehavior &ext,
126 sh::GLenum type,
127 ShShaderSpec spec,
128 ShCompileOptions options,
129 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000130 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500131 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300132 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300133 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500134 mShaderType(type),
135 mShaderSpec(spec),
136 mCompileOptions(options),
137 mShaderVersion(100),
138 mTreeRoot(nullptr),
139 mLoopNestingLevel(0),
140 mStructNestingLevel(0),
141 mSwitchNestingLevel(0),
142 mCurrentFunctionType(nullptr),
143 mFunctionReturnsValue(false),
144 mChecksPrecisionErrors(checksPrecErrors),
145 mFragmentPrecisionHighOnESSL1(false),
146 mDefaultMatrixPacking(EmpColumnMajor),
147 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000148 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500149 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000150 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500151 mShaderVersion,
152 mShaderType,
153 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000154 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500155 mScanner(nullptr),
156 mUsesFragData(false),
157 mUsesFragColor(false),
158 mUsesSecondaryOutputs(false),
159 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
160 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000161 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500162 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000163 mNumViews(-1),
164 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000165 mMaxImageUnits(resources.MaxImageUnits),
166 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000167 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800168 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800169 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jamie Madillacb4b812016-11-07 13:50:29 -0500170 mDeclaringFunction(false)
171{
172 mComputeShaderLocalSize.fill(-1);
173}
174
jchen104cdac9e2017-05-08 11:01:20 +0800175TParseContext::~TParseContext()
176{
177}
178
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300179bool TParseContext::parseVectorFields(const TSourceLoc &line,
180 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400181 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300182 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300184 ASSERT(fieldOffsets);
185 size_t fieldCount = compString.size();
186 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000188 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000189 return false;
190 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300191 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192
Jamie Madillb98c3a82015-07-23 14:26:04 -0400193 enum
194 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000195 exyzw,
196 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000197 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000198 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300200 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530201 {
202 switch (compString[i])
203 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400204 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300205 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400206 fieldSet[i] = exyzw;
207 break;
208 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300209 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210 fieldSet[i] = ergba;
211 break;
212 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300213 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400214 fieldSet[i] = estpq;
215 break;
216 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300217 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400218 fieldSet[i] = exyzw;
219 break;
220 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300221 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400222 fieldSet[i] = ergba;
223 break;
224 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300225 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400226 fieldSet[i] = estpq;
227 break;
228 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300229 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400230 fieldSet[i] = exyzw;
231 break;
232 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300233 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400234 fieldSet[i] = ergba;
235 break;
236 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300237 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400238 fieldSet[i] = estpq;
239 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240
Jamie Madillb98c3a82015-07-23 14:26:04 -0400241 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300242 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400243 fieldSet[i] = exyzw;
244 break;
245 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400247 fieldSet[i] = ergba;
248 break;
249 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300250 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400251 fieldSet[i] = estpq;
252 break;
253 default:
254 error(line, "illegal vector field selection", compString.c_str());
255 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 }
257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300259 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300261 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400263 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 return false;
265 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 if (i > 0)
268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400269 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 error(line, "illegal - vector component fields not from the same set",
272 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000273 return false;
274 }
275 }
276 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000278 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279}
280
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281///////////////////////////////////////////////////////////////////////
282//
283// Errors
284//
285////////////////////////////////////////////////////////////////////////
286
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287//
288// Used by flex/bison to output all syntax and parsing errors.
289//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000290void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000292 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293}
294
Olli Etuaho4de340a2016-12-16 09:32:03 +0000295void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530296{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000297 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000298}
299
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200300void TParseContext::outOfRangeError(bool isError,
301 const TSourceLoc &loc,
302 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000303 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200304{
305 if (isError)
306 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000307 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200308 }
309 else
310 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000311 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200312 }
313}
314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000315//
316// Same error message for all places assignments don't work.
317//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530318void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000320 std::stringstream reasonStream;
321 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
322 std::string reason = reasonStream.str();
323 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324}
325
326//
327// Same error message for all places unary operations don't work.
328//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000331 std::stringstream reasonStream;
332 reasonStream << "wrong operand type - no operation '" << op
333 << "' exists that takes an operand of type " << operand
334 << " (or there is no acceptable conversion)";
335 std::string reason = reasonStream.str();
336 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337}
338
339//
340// Same error message for all binary operations don't work.
341//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400342void TParseContext::binaryOpError(const TSourceLoc &line,
343 const char *op,
344 TString left,
345 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000347 std::stringstream reasonStream;
348 reasonStream << "wrong operand types - no operation '" << op
349 << "' exists that takes a left-hand operand of type '" << left
350 << "' and a right operand of type '" << right
351 << "' (or there is no acceptable conversion)";
352 std::string reason = reasonStream.str();
353 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354}
355
Olli Etuaho856c4972016-08-08 11:38:39 +0300356void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
357 TPrecision precision,
358 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530359{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400360 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300361 return;
Martin Radev70866b82016-07-22 15:27:42 +0300362
363 if (precision != EbpUndefined && !SupportsPrecision(type))
364 {
365 error(line, "illegal type for precision qualifier", getBasicString(type));
366 }
367
Olli Etuaho183d7e22015-11-20 15:59:09 +0200368 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530369 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200370 switch (type)
371 {
372 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400373 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300374 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200375 case EbtInt:
376 case EbtUInt:
377 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400378 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300379 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200380 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800381 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200382 {
jchen10cc2a10e2017-05-03 14:05:12 +0800383 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300384 return;
385 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200386 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000387 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000388}
389
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390// Both test and if necessary, spit out an error, to see if the node is really
391// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300392bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500394 TIntermSymbol *symNode = node->getAsSymbolNode();
395 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100396 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
397
398 if (swizzleNode)
399 {
400 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
401 if (ok && swizzleNode->hasDuplicateOffsets())
402 {
403 error(line, " l-value of swizzle cannot have duplicate components", op);
404 return false;
405 }
406 return ok;
407 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408
Arun Patole7e7e68d2015-05-22 12:02:25 +0530409 if (binaryNode)
410 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400411 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400413 case EOpIndexDirect:
414 case EOpIndexIndirect:
415 case EOpIndexDirectStruct:
416 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300417 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400418 default:
419 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000421 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300422 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000423 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424
jchen10cc2a10e2017-05-03 14:05:12 +0800425 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426 switch (node->getQualifier())
427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400428 case EvqConst:
429 message = "can't modify a const";
430 break;
431 case EvqConstReadOnly:
432 message = "can't modify a const";
433 break;
434 case EvqAttribute:
435 message = "can't modify an attribute";
436 break;
437 case EvqFragmentIn:
438 message = "can't modify an input";
439 break;
440 case EvqVertexIn:
441 message = "can't modify an input";
442 break;
443 case EvqUniform:
444 message = "can't modify a uniform";
445 break;
446 case EvqVaryingIn:
447 message = "can't modify a varying";
448 break;
449 case EvqFragCoord:
450 message = "can't modify gl_FragCoord";
451 break;
452 case EvqFrontFacing:
453 message = "can't modify gl_FrontFacing";
454 break;
455 case EvqPointCoord:
456 message = "can't modify gl_PointCoord";
457 break;
Martin Radevb0883602016-08-04 17:48:58 +0300458 case EvqNumWorkGroups:
459 message = "can't modify gl_NumWorkGroups";
460 break;
461 case EvqWorkGroupSize:
462 message = "can't modify gl_WorkGroupSize";
463 break;
464 case EvqWorkGroupID:
465 message = "can't modify gl_WorkGroupID";
466 break;
467 case EvqLocalInvocationID:
468 message = "can't modify gl_LocalInvocationID";
469 break;
470 case EvqGlobalInvocationID:
471 message = "can't modify gl_GlobalInvocationID";
472 break;
473 case EvqLocalInvocationIndex:
474 message = "can't modify gl_LocalInvocationIndex";
475 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300476 case EvqViewIDOVR:
477 message = "can't modify gl_ViewID_OVR";
478 break;
Martin Radev802abe02016-08-04 17:48:32 +0300479 case EvqComputeIn:
480 message = "can't modify work group size variable";
481 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400482 default:
483 //
484 // Type that can't be written to?
485 //
486 if (node->getBasicType() == EbtVoid)
487 {
488 message = "can't modify void";
489 }
jchen10cc2a10e2017-05-03 14:05:12 +0800490 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400491 {
jchen10cc2a10e2017-05-03 14:05:12 +0800492 message = "can't modify a variable with type ";
493 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300494 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496
jchen10cc2a10e2017-05-03 14:05:12 +0800497 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530498 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000499 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500
Olli Etuaho8a176262016-08-16 14:23:01 +0300501 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 //
505 // Everything else is okay, no error.
506 //
jchen10cc2a10e2017-05-03 14:05:12 +0800507 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300508 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000510 //
511 // If we get here, we have an error and a message.
512 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530513 if (symNode)
514 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000515 const char *symbol = symNode->getSymbol().c_str();
516 std::stringstream reasonStream;
517 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
518 std::string reason = reasonStream.str();
519 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000520 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530521 else
522 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000523 std::stringstream reasonStream;
524 reasonStream << "l-value required (" << message << ")";
525 std::string reason = reasonStream.str();
526 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000527 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000528
Olli Etuaho8a176262016-08-16 14:23:01 +0300529 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530}
531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532// Both test, and if necessary spit out an error, to see if the node is really
533// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300534void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000535{
Olli Etuaho383b7912016-08-05 11:22:59 +0300536 if (node->getQualifier() != EvqConst)
537 {
538 error(node->getLine(), "constant expression required", "");
539 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540}
541
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542// Both test, and if necessary spit out an error, to see if the node is really
543// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300544void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545{
Olli Etuaho383b7912016-08-05 11:22:59 +0300546 if (!node->isScalarInt())
547 {
548 error(node->getLine(), "integer expression required", token);
549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550}
551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552// Both test, and if necessary spit out an error, to see if we are currently
553// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800554bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555{
Olli Etuaho856c4972016-08-08 11:38:39 +0300556 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300557 {
558 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800559 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300560 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800561 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562}
563
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300564// ESSL 3.00.5 sections 3.8 and 3.9.
565// If it starts "gl_" or contains two consecutive underscores, it's reserved.
566// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300567bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530569 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300570 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530571 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300572 error(line, reservedErrMsg, "gl_");
573 return false;
574 }
575 if (sh::IsWebGLBasedSpec(mShaderSpec))
576 {
577 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530578 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300579 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300580 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300582 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530583 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300584 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300585 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 }
587 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300588 if (identifier.find("__") != TString::npos)
589 {
590 error(line,
591 "identifiers containing two consecutive underscores (__) are reserved as "
592 "possible future keywords",
593 identifier.c_str());
594 return false;
595 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300596 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597}
598
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300599// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300600bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800601 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300602 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800604 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530605 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200606 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300607 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000608 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300610 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530611 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300612 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200613 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300614 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200615 {
jchen10cc2a10e2017-05-03 14:05:12 +0800616 std::string reason("cannot convert a variable with type ");
617 reason += getBasicString(argTyped->getBasicType());
618 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300619 return false;
620 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200621 if (argTyped->getBasicType() == EbtVoid)
622 {
623 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300624 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200625 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627
Olli Etuaho856c4972016-08-08 11:38:39 +0300628 if (type.isArray())
629 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300630 // The size of an unsized constructor should already have been determined.
631 ASSERT(!type.isUnsizedArray());
632 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
633 {
634 error(line, "array constructor needs one argument per array element", "constructor");
635 return false;
636 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300637 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
638 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800639 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300640 {
641 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500642 if (argType.isArray())
643 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300644 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500645 return false;
646 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300647 if (!argType.sameElementType(type))
648 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000649 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300650 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300651 }
652 }
653 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300654 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300655 {
656 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300657 if (fields.size() != arguments->size())
658 {
659 error(line,
660 "Number of constructor parameters does not match the number of structure fields",
661 "constructor");
662 return false;
663 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300664
665 for (size_t i = 0; i < fields.size(); i++)
666 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800667 if (i >= arguments->size() ||
668 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300669 {
670 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000671 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300672 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300673 }
674 }
675 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300676 else
677 {
678 // We're constructing a scalar, vector, or matrix.
679
680 // Note: It's okay to have too many components available, but not okay to have unused
681 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
682 // there is an extra argument, so 'overFull' will become true.
683
684 size_t size = 0;
685 bool full = false;
686 bool overFull = false;
687 bool matrixArg = false;
688 for (TIntermNode *arg : *arguments)
689 {
690 const TIntermTyped *argTyped = arg->getAsTyped();
691 ASSERT(argTyped != nullptr);
692
Olli Etuaho487b63a2017-05-23 15:55:09 +0300693 if (argTyped->getBasicType() == EbtStruct)
694 {
695 error(line, "a struct cannot be used as a constructor argument for this type",
696 "constructor");
697 return false;
698 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300699 if (argTyped->getType().isArray())
700 {
701 error(line, "constructing from a non-dereferenced array", "constructor");
702 return false;
703 }
704 if (argTyped->getType().isMatrix())
705 {
706 matrixArg = true;
707 }
708
709 size += argTyped->getType().getObjectSize();
710 if (full)
711 {
712 overFull = true;
713 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300714 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300715 {
716 full = true;
717 }
718 }
719
720 if (type.isMatrix() && matrixArg)
721 {
722 if (arguments->size() != 1)
723 {
724 error(line, "constructing matrix from matrix can only take one argument",
725 "constructor");
726 return false;
727 }
728 }
729 else
730 {
731 if (size != 1 && size < type.getObjectSize())
732 {
733 error(line, "not enough data provided for construction", "constructor");
734 return false;
735 }
736 if (overFull)
737 {
738 error(line, "too many arguments", "constructor");
739 return false;
740 }
741 }
742 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300743
Olli Etuaho8a176262016-08-16 14:23:01 +0300744 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
Jamie Madillb98c3a82015-07-23 14:26:04 -0400747// This function checks to see if a void variable has been declared and raise an error message for
748// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749//
750// returns true in case of an error
751//
Olli Etuaho856c4972016-08-08 11:38:39 +0300752bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400753 const TString &identifier,
754 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300756 if (type == EbtVoid)
757 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000758 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300759 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
Olli Etuaho8a176262016-08-16 14:23:01 +0300762 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763}
764
Jamie Madillb98c3a82015-07-23 14:26:04 -0400765// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300766// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300767bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300769 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000771 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300772 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530773 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300774 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775}
776
Jamie Madillb98c3a82015-07-23 14:26:04 -0400777// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300778// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300779void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000780{
Martin Radev4a9cd802016-09-01 16:51:51 +0300781 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530782 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000783 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530784 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785}
786
jchen10cc2a10e2017-05-03 14:05:12 +0800787bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
788 const TTypeSpecifierNonArray &pType,
789 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530791 if (pType.type == EbtStruct)
792 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300793 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530794 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000795 std::stringstream reasonStream;
796 reasonStream << reason << " (structure contains a sampler)";
797 std::string reasonStr = reasonStream.str();
798 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300799 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 }
jchen10cc2a10e2017-05-03 14:05:12 +0800801 // only samplers need to be checked from structs, since other opaque types can't be struct
802 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300803 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530804 }
jchen10cc2a10e2017-05-03 14:05:12 +0800805 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530806 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000807 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300808 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
Olli Etuaho8a176262016-08-16 14:23:01 +0300811 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
Olli Etuaho856c4972016-08-08 11:38:39 +0300814void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
815 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400816{
817 if (pType.layoutQualifier.location != -1)
818 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400819 error(line, "location must only be specified for a single input or output variable",
820 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400821 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400822}
823
Olli Etuaho856c4972016-08-08 11:38:39 +0300824void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
825 const TLayoutQualifier &layoutQualifier)
826{
827 if (layoutQualifier.location != -1)
828 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000829 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
830 if (mShaderVersion >= 310)
831 {
832 errorMsg =
833 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
834 }
835 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300836 }
837}
838
Martin Radev2cc85b32016-08-05 16:22:53 +0300839void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
840 TQualifier qualifier,
841 const TType &type)
842{
Martin Radev2cc85b32016-08-05 16:22:53 +0300843 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800844 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
jchen10cc2a10e2017-05-03 14:05:12 +0800846 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300851unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000854
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200855 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
856 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
857 // fold as array size.
858 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300861 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho856c4972016-08-08 11:38:39 +0300864 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400865
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000866 if (constant->getBasicType() == EbtUInt)
867 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300868 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000869 }
870 else
871 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000873
Olli Etuaho856c4972016-08-08 11:38:39 +0300874 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000875 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400876 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300877 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000878 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400879
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400881 }
882
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400884 {
885 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300886 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 }
888
889 // The size of arrays is restricted here to prevent issues further down the
890 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
891 // 4096 registers so this should be reasonable even for aggressively optimizable code.
892 const unsigned int sizeLimit = 65536;
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 {
896 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300899
900 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300904bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
905 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906{
Olli Etuaho8a176262016-08-16 14:23:01 +0300907 if ((elementQualifier.qualifier == EvqAttribute) ||
908 (elementQualifier.qualifier == EvqVertexIn) ||
909 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300910 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400911 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300912 TType(elementQualifier).getQualifierString());
913 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000914 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915
Olli Etuaho8a176262016-08-16 14:23:01 +0300916 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
Olli Etuaho8a176262016-08-16 14:23:01 +0300919// See if this element type can be formed into an array.
920bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000922 //
923 // Can the type be an array?
924 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400926 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 error(line, "cannot declare arrays of arrays",
928 TType(elementType).getCompleteString().c_str());
929 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300931 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
932 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
933 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300934 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300935 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300936 {
937 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 TType(elementType).getCompleteString().c_str());
939 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941
Olli Etuaho8a176262016-08-16 14:23:01 +0300942 return true;
943}
944
945// Check if this qualified element type can be formed into an array.
946bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
947 const TPublicType &elementType)
948{
949 if (checkIsValidTypeForArray(indexLocation, elementType))
950 {
951 return checkIsValidQualifierForArray(indexLocation, elementType);
952 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954}
955
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300957void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
958 const TString &identifier,
959 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
Olli Etuaho3739d232015-04-08 12:23:44 +0300961 ASSERT(type != nullptr);
962 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000963 {
964 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300965 type->qualifier = EvqTemporary;
966
967 // Generate informative error messages for ESSL1.
968 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400969 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000970 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530971 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400972 "structures containing arrays may not be declared constant since they cannot be "
973 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530974 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000975 }
976 else
977 {
978 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
979 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300980 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300982 if (type->isUnsizedArray())
983 {
984 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
Olli Etuaho2935c582015-04-08 14:32:06 +0300988// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989// and update the symbol table.
990//
Olli Etuaho2935c582015-04-08 14:32:06 +0300991// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993bool TParseContext::declareVariable(const TSourceLoc &line,
994 const TString &identifier,
995 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300996 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997{
Olli Etuaho2935c582015-04-08 14:32:06 +0300998 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
Olli Etuaho43364892017-02-13 16:00:12 +00001000 checkBindingIsValid(line, type);
1001
Olli Etuaho856c4972016-08-08 11:38:39 +03001002 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003
Olli Etuaho2935c582015-04-08 14:32:06 +03001004 // gl_LastFragData may be redeclared with a new precision qualifier
1005 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1008 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001011 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001012 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001013 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001014 }
1015 }
1016 else
1017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001018 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1019 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 return false;
1021 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001025 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026
Olli Etuaho2935c582015-04-08 14:32:06 +03001027 (*variable) = new TVariable(&identifier, type);
1028 if (!symbolTable.declare(*variable))
1029 {
1030 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001031 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001032 return false;
1033 }
1034
Olli Etuaho8a176262016-08-16 14:23:01 +03001035 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 return false;
1037
1038 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
Martin Radev70866b82016-07-22 15:27:42 +03001041void TParseContext::checkIsParameterQualifierValid(
1042 const TSourceLoc &line,
1043 const TTypeQualifierBuilder &typeQualifierBuilder,
1044 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301045{
Olli Etuahocce89652017-06-19 16:04:09 +03001046 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001047 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001048
1049 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301050 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001051 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1052 }
1053
1054 if (!IsImage(type->getBasicType()))
1055 {
Olli Etuaho43364892017-02-13 16:00:12 +00001056 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001057 }
1058 else
1059 {
1060 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062
Martin Radev70866b82016-07-22 15:27:42 +03001063 type->setQualifier(typeQualifier.qualifier);
1064
1065 if (typeQualifier.precision != EbpUndefined)
1066 {
1067 type->setPrecision(typeQualifier.precision);
1068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069}
1070
Olli Etuaho856c4972016-08-08 11:38:39 +03001071bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001072{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001073 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001074 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301075 if (iter == extBehavior.end())
1076 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001077 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001078 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001079 }
zmo@google.comf5450912011-09-09 01:37:19 +00001080 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1082 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001083 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1084 // associated with more than one extension.
1085 if (extension == "GL_OVR_multiview")
1086 {
1087 return checkCanUseExtension(line, "GL_OVR_multiview2");
1088 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001089 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001090 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001091 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301092 if (iter->second == EBhWarn)
1093 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001094 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001095 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001096 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
Olli Etuaho8a176262016-08-16 14:23:01 +03001098 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001101// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1102// compile-time or link-time errors are the same whether or not the declaration is empty".
1103// This function implements all the checks that are done on qualifiers regardless of if the
1104// declaration is empty.
1105void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1106 const sh::TLayoutQualifier &layoutQualifier,
1107 const TSourceLoc &location)
1108{
1109 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1110 {
1111 error(location, "Shared memory declarations cannot have layout specified", "layout");
1112 }
1113
1114 if (layoutQualifier.matrixPacking != EmpUnspecified)
1115 {
1116 error(location, "layout qualifier only valid for interface blocks",
1117 getMatrixPackingString(layoutQualifier.matrixPacking));
1118 return;
1119 }
1120
1121 if (layoutQualifier.blockStorage != EbsUnspecified)
1122 {
1123 error(location, "layout qualifier only valid for interface blocks",
1124 getBlockStorageString(layoutQualifier.blockStorage));
1125 return;
1126 }
1127
1128 if (qualifier == EvqFragmentOut)
1129 {
1130 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1131 {
1132 error(location, "invalid layout qualifier combination", "yuv");
1133 return;
1134 }
1135 }
1136 else
1137 {
1138 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1139 }
1140
Olli Etuaho95468d12017-05-04 11:14:34 +03001141 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1142 // parsing steps. So it needs to be checked here.
1143 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1144 {
1145 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1146 }
1147
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001148 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1149 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1150 {
1151 canHaveLocation = true;
1152 // We're not checking whether the uniform location is in range here since that depends on
1153 // the type of the variable.
1154 // The type can only be fully determined for non-empty declarations.
1155 }
1156 if (!canHaveLocation)
1157 {
1158 checkLocationIsNotSpecified(location, layoutQualifier);
1159 }
1160}
1161
jchen104cdac9e2017-05-08 11:01:20 +08001162void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1163 const TSourceLoc &location)
1164{
1165 if (publicType.precision != EbpHigh)
1166 {
1167 error(location, "Can only be highp", "atomic counter");
1168 }
1169 // dEQP enforces compile error if location is specified. See uniform_location.test.
1170 if (publicType.layoutQualifier.location != -1)
1171 {
1172 error(location, "location must not be set for atomic_uint", "layout");
1173 }
1174 if (publicType.layoutQualifier.binding == -1)
1175 {
1176 error(location, "no binding specified", "atomic counter");
1177 }
1178}
1179
Martin Radevb8b01222016-11-20 23:25:53 +02001180void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1181 const TSourceLoc &location)
1182{
1183 if (publicType.isUnsizedArray())
1184 {
1185 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1186 // error. It is assumed that this applies to empty declarations as well.
1187 error(location, "empty array declaration needs to specify a size", "");
1188 }
Martin Radevb8b01222016-11-20 23:25:53 +02001189}
1190
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001191// These checks are done for all declarations that are non-empty. They're done for non-empty
1192// declarations starting a declarator list, and declarators that follow an empty declaration.
1193void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1194 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001195{
Olli Etuahofa33d582015-04-09 14:33:12 +03001196 switch (publicType.qualifier)
1197 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001198 case EvqVaryingIn:
1199 case EvqVaryingOut:
1200 case EvqAttribute:
1201 case EvqVertexIn:
1202 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001203 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001204 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001205 {
1206 error(identifierLocation, "cannot be used with a structure",
1207 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001208 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001209 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001210
Jamie Madillb98c3a82015-07-23 14:26:04 -04001211 default:
1212 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001213 }
jchen10cc2a10e2017-05-03 14:05:12 +08001214 std::string reason(getBasicString(publicType.getBasicType()));
1215 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001216 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001217 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001218 {
1219 return;
1220 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001221
Andrei Volykhina5527072017-03-22 16:46:30 +03001222 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1223 publicType.qualifier != EvqConst) &&
1224 publicType.getBasicType() == EbtYuvCscStandardEXT)
1225 {
1226 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1227 getQualifierString(publicType.qualifier));
1228 return;
1229 }
1230
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001231 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1232 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001233 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1234 // But invalid shaders may still reach here with an unsized array declaration.
1235 if (!publicType.isUnsizedArray())
1236 {
1237 TType type(publicType);
1238 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1239 publicType.layoutQualifier);
1240 }
1241 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001242
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001243 // check for layout qualifier issues
1244 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001245
Martin Radev2cc85b32016-08-05 16:22:53 +03001246 if (IsImage(publicType.getBasicType()))
1247 {
1248
1249 switch (layoutQualifier.imageInternalFormat)
1250 {
1251 case EiifRGBA32F:
1252 case EiifRGBA16F:
1253 case EiifR32F:
1254 case EiifRGBA8:
1255 case EiifRGBA8_SNORM:
1256 if (!IsFloatImage(publicType.getBasicType()))
1257 {
1258 error(identifierLocation,
1259 "internal image format requires a floating image type",
1260 getBasicString(publicType.getBasicType()));
1261 return;
1262 }
1263 break;
1264 case EiifRGBA32I:
1265 case EiifRGBA16I:
1266 case EiifRGBA8I:
1267 case EiifR32I:
1268 if (!IsIntegerImage(publicType.getBasicType()))
1269 {
1270 error(identifierLocation,
1271 "internal image format requires an integer image type",
1272 getBasicString(publicType.getBasicType()));
1273 return;
1274 }
1275 break;
1276 case EiifRGBA32UI:
1277 case EiifRGBA16UI:
1278 case EiifRGBA8UI:
1279 case EiifR32UI:
1280 if (!IsUnsignedImage(publicType.getBasicType()))
1281 {
1282 error(identifierLocation,
1283 "internal image format requires an unsigned image type",
1284 getBasicString(publicType.getBasicType()));
1285 return;
1286 }
1287 break;
1288 case EiifUnspecified:
1289 error(identifierLocation, "layout qualifier", "No image internal format specified");
1290 return;
1291 default:
1292 error(identifierLocation, "layout qualifier", "unrecognized token");
1293 return;
1294 }
1295
1296 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1297 switch (layoutQualifier.imageInternalFormat)
1298 {
1299 case EiifR32F:
1300 case EiifR32I:
1301 case EiifR32UI:
1302 break;
1303 default:
1304 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1305 {
1306 error(identifierLocation, "layout qualifier",
1307 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1308 "image variables must be qualified readonly and/or writeonly");
1309 return;
1310 }
1311 break;
1312 }
1313 }
1314 else
1315 {
Olli Etuaho43364892017-02-13 16:00:12 +00001316 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001317 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1318 }
jchen104cdac9e2017-05-08 11:01:20 +08001319
1320 if (IsAtomicCounter(publicType.getBasicType()))
1321 {
1322 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1323 }
1324 else
1325 {
1326 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1327 }
Olli Etuaho43364892017-02-13 16:00:12 +00001328}
Martin Radev2cc85b32016-08-05 16:22:53 +03001329
Olli Etuaho43364892017-02-13 16:00:12 +00001330void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1331{
1332 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1333 int arraySize = type.isArray() ? type.getArraySize() : 1;
1334 if (IsImage(type.getBasicType()))
1335 {
1336 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1337 }
1338 else if (IsSampler(type.getBasicType()))
1339 {
1340 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1341 }
jchen104cdac9e2017-05-08 11:01:20 +08001342 else if (IsAtomicCounter(type.getBasicType()))
1343 {
1344 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1345 }
Olli Etuaho43364892017-02-13 16:00:12 +00001346 else
1347 {
1348 ASSERT(!IsOpaqueType(type.getBasicType()));
1349 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001350 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001351}
1352
Olli Etuaho856c4972016-08-08 11:38:39 +03001353void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1354 const TString &layoutQualifierName,
1355 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001356{
1357
1358 if (mShaderVersion < versionRequired)
1359 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001360 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001361 }
1362}
1363
Olli Etuaho856c4972016-08-08 11:38:39 +03001364bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1365 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001366{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001367 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001368 for (size_t i = 0u; i < localSize.size(); ++i)
1369 {
1370 if (localSize[i] != -1)
1371 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001372 error(location,
1373 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1374 "global layout declaration",
1375 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001376 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001377 }
1378 }
1379
Olli Etuaho8a176262016-08-16 14:23:01 +03001380 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001381}
1382
Olli Etuaho43364892017-02-13 16:00:12 +00001383void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001384 TLayoutImageInternalFormat internalFormat)
1385{
1386 if (internalFormat != EiifUnspecified)
1387 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001388 error(location, "invalid layout qualifier: only valid when used with images",
1389 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001390 }
Olli Etuaho43364892017-02-13 16:00:12 +00001391}
1392
1393void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1394{
1395 if (binding != -1)
1396 {
1397 error(location,
1398 "invalid layout qualifier: only valid when used with opaque types or blocks",
1399 "binding");
1400 }
1401}
1402
jchen104cdac9e2017-05-08 11:01:20 +08001403void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1404{
1405 if (offset != -1)
1406 {
1407 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1408 "offset");
1409 }
1410}
1411
Olli Etuaho43364892017-02-13 16:00:12 +00001412void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1413{
1414 // Expects arraySize to be 1 when setting binding for only a single variable.
1415 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1416 {
1417 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1418 }
1419}
1420
1421void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1422 int binding,
1423 int arraySize)
1424{
1425 // Expects arraySize to be 1 when setting binding for only a single variable.
1426 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1427 {
1428 error(location, "sampler binding greater than maximum texture units", "binding");
1429 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001430}
1431
jchen10af713a22017-04-19 09:10:56 +08001432void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1433{
1434 int size = (arraySize == 0 ? 1 : arraySize);
1435 if (binding + size > mMaxUniformBufferBindings)
1436 {
1437 error(location, "interface block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1438 "binding");
1439 }
1440}
jchen104cdac9e2017-05-08 11:01:20 +08001441void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1442{
1443 if (binding >= mMaxAtomicCounterBindings)
1444 {
1445 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1446 "binding");
1447 }
1448}
jchen10af713a22017-04-19 09:10:56 +08001449
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001450void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1451 int objectLocationCount,
1452 const TLayoutQualifier &layoutQualifier)
1453{
1454 int loc = layoutQualifier.location;
1455 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1456 {
1457 error(location, "Uniform location out of range", "location");
1458 }
1459}
1460
Andrei Volykhina5527072017-03-22 16:46:30 +03001461void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1462{
1463 if (yuv != false)
1464 {
1465 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1466 }
1467}
1468
Olli Etuaho383b7912016-08-05 11:22:59 +03001469void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001470 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001471{
1472 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1473 {
1474 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1475 if (qual == EvqOut || qual == EvqInOut)
1476 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001477 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001478 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001479 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001480 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001481 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001482 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001483 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001484 }
1485 }
1486 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001487}
1488
Martin Radev70866b82016-07-22 15:27:42 +03001489void TParseContext::checkInvariantVariableQualifier(bool invariant,
1490 const TQualifier qualifier,
1491 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001492{
Martin Radev70866b82016-07-22 15:27:42 +03001493 if (!invariant)
1494 return;
1495
1496 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001497 {
Martin Radev70866b82016-07-22 15:27:42 +03001498 // input variables in the fragment shader can be also qualified as invariant
1499 if (!sh::CanBeInvariantESSL1(qualifier))
1500 {
1501 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1502 }
1503 }
1504 else
1505 {
1506 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1507 {
1508 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1509 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001510 }
1511}
1512
Arun Patole7e7e68d2015-05-22 12:02:25 +05301513bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001514{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001515 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001516 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1517 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001518}
1519
Arun Patole7e7e68d2015-05-22 12:02:25 +05301520bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001521{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001522 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001523}
1524
Jamie Madillb98c3a82015-07-23 14:26:04 -04001525void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1526 const char *extName,
1527 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001528{
1529 pp::SourceLocation srcLoc;
1530 srcLoc.file = loc.first_file;
1531 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001532 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001533}
1534
Jamie Madillb98c3a82015-07-23 14:26:04 -04001535void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1536 const char *name,
1537 const char *value,
1538 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001539{
1540 pp::SourceLocation srcLoc;
1541 srcLoc.file = loc.first_file;
1542 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001543 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001544}
1545
Martin Radev4c4c8e72016-08-04 12:25:34 +03001546sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001547{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001548 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001549 for (size_t i = 0u; i < result.size(); ++i)
1550 {
1551 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1552 {
1553 result[i] = 1;
1554 }
1555 else
1556 {
1557 result[i] = mComputeShaderLocalSize[i];
1558 }
1559 }
1560 return result;
1561}
1562
Olli Etuaho56229f12017-07-10 14:16:33 +03001563TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1564 const TSourceLoc &line)
1565{
1566 TIntermConstantUnion *node = new TIntermConstantUnion(
1567 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1568 node->setLine(line);
1569 return node;
1570}
1571
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572/////////////////////////////////////////////////////////////////////////////////
1573//
1574// Non-Errors.
1575//
1576/////////////////////////////////////////////////////////////////////////////////
1577
Jamie Madill5c097022014-08-20 16:38:32 -04001578const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1579 const TString *name,
1580 const TSymbol *symbol)
1581{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001582 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001583
1584 if (!symbol)
1585 {
1586 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001587 }
1588 else if (!symbol->isVariable())
1589 {
1590 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001591 }
1592 else
1593 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001594 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001595
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001596 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001597 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001598 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001599 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001600 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001601
1602 // Reject shaders using both gl_FragData and gl_FragColor
1603 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001604 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001605 {
1606 mUsesFragData = true;
1607 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001608 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001609 {
1610 mUsesFragColor = true;
1611 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001612 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1613 {
1614 mUsesSecondaryOutputs = true;
1615 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001616
1617 // This validation is not quite correct - it's only an error to write to
1618 // both FragData and FragColor. For simplicity, and because users shouldn't
1619 // be rewarded for reading from undefined varaibles, return an error
1620 // if they are both referenced, rather than assigned.
1621 if (mUsesFragData && mUsesFragColor)
1622 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001623 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1624 if (mUsesSecondaryOutputs)
1625 {
1626 errorMessage =
1627 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1628 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1629 }
1630 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001631 }
Martin Radevb0883602016-08-04 17:48:58 +03001632
1633 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1634 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1635 qualifier == EvqWorkGroupSize)
1636 {
1637 error(location,
1638 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1639 "gl_WorkGroupSize");
1640 }
Jamie Madill5c097022014-08-20 16:38:32 -04001641 }
1642
1643 if (!variable)
1644 {
1645 TType type(EbtFloat, EbpUndefined);
1646 TVariable *fakeVariable = new TVariable(name, type);
1647 symbolTable.declare(fakeVariable);
1648 variable = fakeVariable;
1649 }
1650
1651 return variable;
1652}
1653
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001654TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1655 const TString *name,
1656 const TSymbol *symbol)
1657{
1658 const TVariable *variable = getNamedVariable(location, name, symbol);
1659
Olli Etuaho09b04a22016-12-15 13:30:26 +00001660 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1661 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1662 {
1663 // WEBGL_multiview spec
1664 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1665 "gl_ViewID_OVR");
1666 }
1667
Olli Etuaho56229f12017-07-10 14:16:33 +03001668 TIntermTyped *node = nullptr;
1669
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001670 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001671 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001672 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001673 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001674 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001675 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1676 mComputeShaderLocalSizeDeclared)
1677 {
1678 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1679 // needs to be added to the AST as a constant and not as a symbol.
1680 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1681 TConstantUnion *constArray = new TConstantUnion[3];
1682 for (size_t i = 0; i < 3; ++i)
1683 {
1684 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1685 }
1686
1687 ASSERT(variable->getType().getBasicType() == EbtUInt);
1688 ASSERT(variable->getType().getObjectSize() == 3);
1689
1690 TType type(variable->getType());
1691 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001692 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001693 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001694 else
1695 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001696 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001697 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001698 ASSERT(node != nullptr);
1699 node->setLine(location);
1700 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001701}
1702
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703// Initializers show up in several places in the grammar. Have one set of
1704// code to handle them here.
1705//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001706// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001707bool TParseContext::executeInitializer(const TSourceLoc &line,
1708 const TString &identifier,
1709 const TPublicType &pType,
1710 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001711 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712{
Olli Etuaho13389b62016-10-16 11:48:18 +01001713 ASSERT(initNode != nullptr);
1714 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001715 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716
Olli Etuaho2935c582015-04-08 14:32:06 +03001717 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001718 if (type.isUnsizedArray())
1719 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001720 // We have not checked yet whether the initializer actually is an array or not.
1721 if (initializer->isArray())
1722 {
1723 type.setArraySize(initializer->getArraySize());
1724 }
1725 else
1726 {
1727 // Having a non-array initializer for an unsized array will result in an error later,
1728 // so we don't generate an error message here.
1729 type.setArraySize(1u);
1730 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001731 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001732 if (!declareVariable(line, identifier, type, &variable))
1733 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001734 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
Olli Etuahob0c645e2015-05-12 14:25:36 +03001737 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001738 if (symbolTable.atGlobalLevel() &&
1739 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001740 {
1741 // Error message does not completely match behavior with ESSL 1.00, but
1742 // we want to steer developers towards only using constant expressions.
1743 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001744 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001745 }
1746 if (globalInitWarning)
1747 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 warning(
1749 line,
1750 "global variable initializers should be constant expressions "
1751 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1752 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001753 }
1754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001755 //
1756 // identifier must be of type constant, a global, or a temporary
1757 //
1758 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301759 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1760 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001761 error(line, " cannot initialize this type of qualifier ",
1762 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001763 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001764 }
1765 //
1766 // test for and propagate constant
1767 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768
Arun Patole7e7e68d2015-05-22 12:02:25 +05301769 if (qualifier == EvqConst)
1770 {
1771 if (qualifier != initializer->getType().getQualifier())
1772 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001773 std::stringstream reasonStream;
1774 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1775 << "'";
1776 std::string reason = reasonStream.str();
1777 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001778 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001779 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001780 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301781 if (type != initializer->getType())
1782 {
1783 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001784 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001785 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001786 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001787 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001788
1789 // Save the constant folded value to the variable if possible. For example array
1790 // initializers are not folded, since that way copying the array literal to multiple places
1791 // in the shader is avoided.
1792 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1793 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301794 if (initializer->getAsConstantUnion())
1795 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001796 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001797 ASSERT(*initNode == nullptr);
1798 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301799 }
1800 else if (initializer->getAsSymbolNode())
1801 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001802 const TSymbol *symbol =
1803 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1804 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001806 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001807 if (constArray)
1808 {
1809 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001810 ASSERT(*initNode == nullptr);
1811 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001812 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001813 }
1814 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001816 TIntermSymbol *intermSymbol =
1817 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1818 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001819 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1820 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001821 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001822 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001823 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
Olli Etuaho914b79a2017-06-19 16:03:19 +03001826 return true;
1827}
1828
1829TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1830 const TString &identifier,
1831 TIntermTyped *initializer,
1832 const TSourceLoc &loc)
1833{
1834 checkIsScalarBool(loc, pType);
1835 TIntermBinary *initNode = nullptr;
1836 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1837 {
1838 // The initializer is valid. The init condition needs to have a node - either the
1839 // initializer node, or a constant node in case the initialized variable is const and won't
1840 // be recorded in the AST.
1841 if (initNode == nullptr)
1842 {
1843 return initializer;
1844 }
1845 else
1846 {
1847 TIntermDeclaration *declaration = new TIntermDeclaration();
1848 declaration->appendDeclarator(initNode);
1849 return declaration;
1850 }
1851 }
1852 return nullptr;
1853}
1854
1855TIntermNode *TParseContext::addLoop(TLoopType type,
1856 TIntermNode *init,
1857 TIntermNode *cond,
1858 TIntermTyped *expr,
1859 TIntermNode *body,
1860 const TSourceLoc &line)
1861{
1862 TIntermNode *node = nullptr;
1863 TIntermTyped *typedCond = nullptr;
1864 if (cond)
1865 {
1866 typedCond = cond->getAsTyped();
1867 }
1868 if (cond == nullptr || typedCond)
1869 {
Olli Etuahocce89652017-06-19 16:04:09 +03001870 if (type == ELoopDoWhile)
1871 {
1872 checkIsScalarBool(line, typedCond);
1873 }
1874 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1875 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1876 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1877 !typedCond->isVector()));
1878
Olli Etuaho3ec75682017-07-05 17:02:55 +03001879 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001880 node->setLine(line);
1881 return node;
1882 }
1883
Olli Etuahocce89652017-06-19 16:04:09 +03001884 ASSERT(type != ELoopDoWhile);
1885
Olli Etuaho914b79a2017-06-19 16:03:19 +03001886 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1887 ASSERT(declaration);
1888 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1889 ASSERT(declarator->getLeft()->getAsSymbolNode());
1890
1891 // The condition is a declaration. In the AST representation we don't support declarations as
1892 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1893 // the loop.
1894 TIntermBlock *block = new TIntermBlock();
1895
1896 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1897 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1898 block->appendStatement(declareCondition);
1899
1900 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1901 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03001902 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001903 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 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001920 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03001921 }
1922 else
1923 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03001924 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03001925 }
1926 }
1927
Olli Etuaho3ec75682017-07-05 17:02:55 +03001928 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03001929 node->setLine(loc);
1930
1931 return node;
1932}
1933
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001934void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1935{
1936 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1937 typeSpecifier->getBasicType());
1938
1939 if (mShaderVersion < 300 && typeSpecifier->array)
1940 {
1941 error(typeSpecifier->getLine(), "not supported", "first-class array");
1942 typeSpecifier->clearArrayness();
1943 }
1944}
1945
Martin Radev70866b82016-07-22 15:27:42 +03001946TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301947 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001948{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001949 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001950
Martin Radev70866b82016-07-22 15:27:42 +03001951 TPublicType returnType = typeSpecifier;
1952 returnType.qualifier = typeQualifier.qualifier;
1953 returnType.invariant = typeQualifier.invariant;
1954 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001955 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001956 returnType.precision = typeSpecifier.precision;
1957
1958 if (typeQualifier.precision != EbpUndefined)
1959 {
1960 returnType.precision = typeQualifier.precision;
1961 }
1962
Martin Radev4a9cd802016-09-01 16:51:51 +03001963 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1964 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001965
Martin Radev4a9cd802016-09-01 16:51:51 +03001966 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1967 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001968
Martin Radev4a9cd802016-09-01 16:51:51 +03001969 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001970
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001971 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001972 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001973 if (typeSpecifier.array)
1974 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001975 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001976 returnType.clearArrayness();
1977 }
1978
Martin Radev70866b82016-07-22 15:27:42 +03001979 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001980 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001981 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001982 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001983 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001984 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001985
Martin Radev70866b82016-07-22 15:27:42 +03001986 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001987 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001988 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001989 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001990 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001991 }
1992 }
1993 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001994 {
Martin Radev70866b82016-07-22 15:27:42 +03001995 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001996 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001997 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001998 }
Martin Radev70866b82016-07-22 15:27:42 +03001999 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2000 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002001 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002002 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2003 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002004 }
Martin Radev70866b82016-07-22 15:27:42 +03002005 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002006 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002007 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002008 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002009 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002010 }
2011
2012 return returnType;
2013}
2014
Olli Etuaho856c4972016-08-08 11:38:39 +03002015void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2016 const TPublicType &type,
2017 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002018{
2019 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002020 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002021 {
2022 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002023 }
2024
2025 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2026 switch (qualifier)
2027 {
2028 case EvqVertexIn:
2029 // ESSL 3.00 section 4.3.4
2030 if (type.array)
2031 {
2032 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002033 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002034 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002035 return;
2036 case EvqFragmentOut:
2037 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002038 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002039 {
2040 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002041 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002042 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002043 return;
2044 default:
2045 break;
2046 }
2047
2048 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2049 // restrictions.
2050 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002051 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2052 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002053 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2054 {
2055 error(qualifierLocation, "must use 'flat' interpolation here",
2056 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002057 }
2058
Martin Radev4a9cd802016-09-01 16:51:51 +03002059 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002060 {
2061 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2062 // These restrictions are only implied by the ESSL 3.00 spec, but
2063 // the ESSL 3.10 spec lists these restrictions explicitly.
2064 if (type.array)
2065 {
2066 error(qualifierLocation, "cannot be an array of structures",
2067 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002068 }
2069 if (type.isStructureContainingArrays())
2070 {
2071 error(qualifierLocation, "cannot be a structure containing an array",
2072 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002073 }
2074 if (type.isStructureContainingType(EbtStruct))
2075 {
2076 error(qualifierLocation, "cannot be a structure containing a structure",
2077 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002078 }
2079 if (type.isStructureContainingType(EbtBool))
2080 {
2081 error(qualifierLocation, "cannot be a structure containing a bool",
2082 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002083 }
2084 }
2085}
2086
Martin Radev2cc85b32016-08-05 16:22:53 +03002087void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2088{
2089 if (qualifier.getType() == QtStorage)
2090 {
2091 const TStorageQualifierWrapper &storageQualifier =
2092 static_cast<const TStorageQualifierWrapper &>(qualifier);
2093 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2094 !symbolTable.atGlobalLevel())
2095 {
2096 error(storageQualifier.getLine(),
2097 "Local variables can only use the const storage qualifier.",
2098 storageQualifier.getQualifierString().c_str());
2099 }
2100 }
2101}
2102
Olli Etuaho43364892017-02-13 16:00:12 +00002103void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002104 const TSourceLoc &location)
2105{
2106 if (memoryQualifier.readonly)
2107 {
2108 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002109 }
2110 if (memoryQualifier.writeonly)
2111 {
2112 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002113 }
Martin Radev049edfa2016-11-11 14:35:37 +02002114 if (memoryQualifier.coherent)
2115 {
2116 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002117 }
2118 if (memoryQualifier.restrictQualifier)
2119 {
2120 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002121 }
2122 if (memoryQualifier.volatileQualifier)
2123 {
2124 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002125 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002126}
2127
jchen104cdac9e2017-05-08 11:01:20 +08002128// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2129// intermediate tree.
2130void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2131 size_t size,
2132 bool forceAppend,
2133 const TSourceLoc &loc,
2134 TType &type)
2135{
2136 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2137 int offset;
2138 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2139 {
2140 offset = bindingState.appendSpan(size);
2141 }
2142 else
2143 {
2144 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2145 }
2146 if (offset == -1)
2147 {
2148 error(loc, "Offset overlapping", "atomic counter");
2149 return;
2150 }
2151 TLayoutQualifier qualifier = type.getLayoutQualifier();
2152 qualifier.offset = offset;
2153 type.setLayoutQualifier(qualifier);
2154}
2155
Olli Etuaho13389b62016-10-16 11:48:18 +01002156TIntermDeclaration *TParseContext::parseSingleDeclaration(
2157 TPublicType &publicType,
2158 const TSourceLoc &identifierOrTypeLocation,
2159 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002160{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002161 TType type(publicType);
2162 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2163 mDirectiveHandler.pragma().stdgl.invariantAll)
2164 {
2165 TQualifier qualifier = type.getQualifier();
2166
2167 // The directive handler has already taken care of rejecting invalid uses of this pragma
2168 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2169 // affected variable declarations:
2170 //
2171 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2172 // elsewhere, in TranslatorGLSL.)
2173 //
2174 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2175 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2176 // the way this is currently implemented we have to enable this compiler option before
2177 // parsing the shader and determining the shading language version it uses. If this were
2178 // implemented as a post-pass, the workaround could be more targeted.
2179 //
2180 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2181 // the specification, but there are desktop OpenGL drivers that expect that this is the
2182 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2183 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2184 {
2185 type.setInvariant(true);
2186 }
2187 }
2188
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002189 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2190 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002191
Olli Etuahobab4c082015-04-24 16:38:49 +03002192 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002193 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002194
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002195 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002196 if (emptyDeclaration)
2197 {
Martin Radevb8b01222016-11-20 23:25:53 +02002198 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002199 // In most cases we don't need to create a symbol node for an empty declaration.
2200 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2201 if (type.getBasicType() == EbtStruct)
2202 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002203 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002204 }
jchen104cdac9e2017-05-08 11:01:20 +08002205 else if (IsAtomicCounter(publicType.getBasicType()))
2206 {
2207 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2208 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002209 }
2210 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002211 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002212 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002213
Olli Etuaho856c4972016-08-08 11:38:39 +03002214 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002215
jchen104cdac9e2017-05-08 11:01:20 +08002216 if (IsAtomicCounter(publicType.getBasicType()))
2217 {
2218
2219 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2220 identifierOrTypeLocation, type);
2221 }
2222
Olli Etuaho2935c582015-04-08 14:32:06 +03002223 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002224 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002225
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002226 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002227 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002228 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002229 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002230 }
2231
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002232 TIntermDeclaration *declaration = new TIntermDeclaration();
2233 declaration->setLine(identifierOrTypeLocation);
2234 if (symbol)
2235 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002236 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002237 declaration->appendDeclarator(symbol);
2238 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002239 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002240}
2241
Olli Etuaho13389b62016-10-16 11:48:18 +01002242TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2243 const TSourceLoc &identifierLocation,
2244 const TString &identifier,
2245 const TSourceLoc &indexLocation,
2246 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002247{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002248 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002249
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002250 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2251 identifierLocation);
2252
2253 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002254
Olli Etuaho856c4972016-08-08 11:38:39 +03002255 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002256
Olli Etuaho8a176262016-08-16 14:23:01 +03002257 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002258
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002259 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002260
Olli Etuaho856c4972016-08-08 11:38:39 +03002261 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002262 // Make the type an array even if size check failed.
2263 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2264 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002265
jchen104cdac9e2017-05-08 11:01:20 +08002266 if (IsAtomicCounter(publicType.getBasicType()))
2267 {
2268 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2269 identifierLocation, arrayType);
2270 }
2271
Olli Etuaho2935c582015-04-08 14:32:06 +03002272 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002273 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002274
Olli Etuaho13389b62016-10-16 11:48:18 +01002275 TIntermDeclaration *declaration = new TIntermDeclaration();
2276 declaration->setLine(identifierLocation);
2277
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002278 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002279 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002280 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2281 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002282 declaration->appendDeclarator(symbol);
2283 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002284
Olli Etuaho13389b62016-10-16 11:48:18 +01002285 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002286}
2287
Olli Etuaho13389b62016-10-16 11:48:18 +01002288TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2289 const TSourceLoc &identifierLocation,
2290 const TString &identifier,
2291 const TSourceLoc &initLocation,
2292 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002293{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002294 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002295
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002296 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2297 identifierLocation);
2298
2299 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002300
Olli Etuaho13389b62016-10-16 11:48:18 +01002301 TIntermDeclaration *declaration = new TIntermDeclaration();
2302 declaration->setLine(identifierLocation);
2303
2304 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002305 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002306 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002307 if (initNode)
2308 {
2309 declaration->appendDeclarator(initNode);
2310 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002311 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002312 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002313}
2314
Olli Etuaho13389b62016-10-16 11:48:18 +01002315TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002316 TPublicType &publicType,
2317 const TSourceLoc &identifierLocation,
2318 const TString &identifier,
2319 const TSourceLoc &indexLocation,
2320 TIntermTyped *indexExpression,
2321 const TSourceLoc &initLocation,
2322 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002323{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002324 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002326 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2327 identifierLocation);
2328
2329 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002330
Olli Etuaho8a176262016-08-16 14:23:01 +03002331 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002332
2333 TPublicType arrayType(publicType);
2334
Olli Etuaho856c4972016-08-08 11:38:39 +03002335 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002336 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2337 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002338 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002339 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002340 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002341 }
2342 // Make the type an array even if size check failed.
2343 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2344 arrayType.setArraySize(size);
2345
Olli Etuaho13389b62016-10-16 11:48:18 +01002346 TIntermDeclaration *declaration = new TIntermDeclaration();
2347 declaration->setLine(identifierLocation);
2348
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002349 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002350 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002351 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002352 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002353 if (initNode)
2354 {
2355 declaration->appendDeclarator(initNode);
2356 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002357 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002358
2359 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002360}
2361
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002362TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002363 const TTypeQualifierBuilder &typeQualifierBuilder,
2364 const TSourceLoc &identifierLoc,
2365 const TString *identifier,
2366 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002367{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002368 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002369
Martin Radev70866b82016-07-22 15:27:42 +03002370 if (!typeQualifier.invariant)
2371 {
2372 error(identifierLoc, "Expected invariant", identifier->c_str());
2373 return nullptr;
2374 }
2375 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2376 {
2377 return nullptr;
2378 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002379 if (!symbol)
2380 {
2381 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002382 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002383 }
Martin Radev70866b82016-07-22 15:27:42 +03002384 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002385 {
Martin Radev70866b82016-07-22 15:27:42 +03002386 error(identifierLoc, "invariant declaration specifies qualifier",
2387 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002388 }
Martin Radev70866b82016-07-22 15:27:42 +03002389 if (typeQualifier.precision != EbpUndefined)
2390 {
2391 error(identifierLoc, "invariant declaration specifies precision",
2392 getPrecisionString(typeQualifier.precision));
2393 }
2394 if (!typeQualifier.layoutQualifier.isEmpty())
2395 {
2396 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2397 }
2398
2399 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2400 ASSERT(variable);
2401 const TType &type = variable->getType();
2402
2403 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2404 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002405 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002406
2407 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2408
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002409 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2410 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002411
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002412 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002413}
2414
Olli Etuaho13389b62016-10-16 11:48:18 +01002415void TParseContext::parseDeclarator(TPublicType &publicType,
2416 const TSourceLoc &identifierLocation,
2417 const TString &identifier,
2418 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002419{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002420 // If the declaration starting this declarator list was empty (example: int,), some checks were
2421 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002422 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002423 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002424 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2425 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002426 }
2427
Olli Etuaho856c4972016-08-08 11:38:39 +03002428 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002429
Olli Etuaho856c4972016-08-08 11:38:39 +03002430 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002431
Olli Etuaho2935c582015-04-08 14:32:06 +03002432 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002433 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002434 if (IsAtomicCounter(publicType.getBasicType()))
2435 {
2436 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2437 identifierLocation, type);
2438 }
Olli Etuaho43364892017-02-13 16:00:12 +00002439 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002440
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002441 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002442 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002443 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2444 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002445 declarationOut->appendDeclarator(symbol);
2446 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002447}
2448
Olli Etuaho13389b62016-10-16 11:48:18 +01002449void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2450 const TSourceLoc &identifierLocation,
2451 const TString &identifier,
2452 const TSourceLoc &arrayLocation,
2453 TIntermTyped *indexExpression,
2454 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002455{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002456 // If the declaration starting this declarator list was empty (example: int,), some checks were
2457 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002458 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002459 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002460 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2461 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002462 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002463
Olli Etuaho856c4972016-08-08 11:38:39 +03002464 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002465
Olli Etuaho856c4972016-08-08 11:38:39 +03002466 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002467
Olli Etuaho8a176262016-08-16 14:23:01 +03002468 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002469 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002470 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002471 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002472 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002473
jchen104cdac9e2017-05-08 11:01:20 +08002474 if (IsAtomicCounter(publicType.getBasicType()))
2475 {
2476 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2477 true, identifierLocation, arrayType);
2478 }
2479
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002480 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002481 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002482
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002483 TIntermSymbol *symbol = new TIntermSymbol(0, identifier, arrayType);
2484 symbol->setLine(identifierLocation);
2485 if (variable)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002486 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002487
Olli Etuaho13389b62016-10-16 11:48:18 +01002488 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002489 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002490}
2491
Olli Etuaho13389b62016-10-16 11:48:18 +01002492void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2493 const TSourceLoc &identifierLocation,
2494 const TString &identifier,
2495 const TSourceLoc &initLocation,
2496 TIntermTyped *initializer,
2497 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002498{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002499 // If the declaration starting this declarator list was empty (example: int,), some checks were
2500 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002501 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002502 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002503 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2504 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002505 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002506
Olli Etuaho856c4972016-08-08 11:38:39 +03002507 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002508
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002510 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002511 {
2512 //
2513 // build the intermediate representation
2514 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002515 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002516 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002517 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002518 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002519 }
2520}
2521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2523 const TSourceLoc &identifierLocation,
2524 const TString &identifier,
2525 const TSourceLoc &indexLocation,
2526 TIntermTyped *indexExpression,
2527 const TSourceLoc &initLocation,
2528 TIntermTyped *initializer,
2529 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002530{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002531 // If the declaration starting this declarator list was empty (example: int,), some checks were
2532 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002533 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002534 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002535 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2536 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002537 }
2538
Olli Etuaho856c4972016-08-08 11:38:39 +03002539 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002540
Olli Etuaho8a176262016-08-16 14:23:01 +03002541 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002542
2543 TPublicType arrayType(publicType);
2544
Olli Etuaho856c4972016-08-08 11:38:39 +03002545 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002546 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2547 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002548 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002549 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002550 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002551 }
2552 // Make the type an array even if size check failed.
2553 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2554 arrayType.setArraySize(size);
2555
2556 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002557 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002558 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002559 {
2560 if (initNode)
2561 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002562 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564 }
2565}
2566
jchen104cdac9e2017-05-08 11:01:20 +08002567void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2568 const TSourceLoc &location)
2569{
2570 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2571 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2572 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2573 {
2574 error(location, "Requires both binding and offset", "layout");
2575 return;
2576 }
2577 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2578}
2579
Olli Etuahocce89652017-06-19 16:04:09 +03002580void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2581 const TPublicType &type,
2582 const TSourceLoc &loc)
2583{
2584 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2585 !getFragmentPrecisionHigh())
2586 {
2587 error(loc, "precision is not supported in fragment shader", "highp");
2588 }
2589
2590 if (!CanSetDefaultPrecisionOnType(type))
2591 {
2592 error(loc, "illegal type argument for default precision qualifier",
2593 getBasicString(type.getBasicType()));
2594 return;
2595 }
2596 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2597}
2598
Martin Radev70866b82016-07-22 15:27:42 +03002599void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002600{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002601 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002602 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002603
Martin Radev70866b82016-07-22 15:27:42 +03002604 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2605 typeQualifier.line);
2606
Jamie Madillc2128ff2016-07-04 10:26:17 -04002607 // It should never be the case, but some strange parser errors can send us here.
2608 if (layoutQualifier.isEmpty())
2609 {
2610 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002611 return;
2612 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002613
Martin Radev802abe02016-08-04 17:48:32 +03002614 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002615 {
Olli Etuaho43364892017-02-13 16:00:12 +00002616 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002617 return;
2618 }
2619
Olli Etuaho43364892017-02-13 16:00:12 +00002620 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2621
2622 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002623
2624 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2625
Andrei Volykhina5527072017-03-22 16:46:30 +03002626 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2627
jchen104cdac9e2017-05-08 11:01:20 +08002628 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2629
Martin Radev802abe02016-08-04 17:48:32 +03002630 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002631 {
Martin Radev802abe02016-08-04 17:48:32 +03002632 if (mComputeShaderLocalSizeDeclared &&
2633 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2634 {
2635 error(typeQualifier.line, "Work group size does not match the previous declaration",
2636 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002637 return;
2638 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002639
Martin Radev802abe02016-08-04 17:48:32 +03002640 if (mShaderVersion < 310)
2641 {
2642 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002643 return;
2644 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002645
Martin Radev4c4c8e72016-08-04 12:25:34 +03002646 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002647 {
2648 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002649 return;
2650 }
2651
2652 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2653 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2654
2655 const TConstantUnion *maxComputeWorkGroupSizeData =
2656 maxComputeWorkGroupSize->getConstPointer();
2657
2658 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2659 {
2660 if (layoutQualifier.localSize[i] != -1)
2661 {
2662 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2663 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2664 if (mComputeShaderLocalSize[i] < 1 ||
2665 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2666 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002667 std::stringstream reasonStream;
2668 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2669 << maxComputeWorkGroupSizeValue;
2670 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002671
Olli Etuaho4de340a2016-12-16 09:32:03 +00002672 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002673 return;
2674 }
2675 }
2676 }
2677
2678 mComputeShaderLocalSizeDeclared = true;
2679 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002680 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002681 {
2682 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2683 // specification.
2684 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2685 {
2686 error(typeQualifier.line, "Number of views does not match the previous declaration",
2687 "layout");
2688 return;
2689 }
2690
2691 if (layoutQualifier.numViews == -1)
2692 {
2693 error(typeQualifier.line, "No num_views specified", "layout");
2694 return;
2695 }
2696
2697 if (layoutQualifier.numViews > mMaxNumViews)
2698 {
2699 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2700 "layout");
2701 return;
2702 }
2703
2704 mNumViews = layoutQualifier.numViews;
2705 }
Martin Radev802abe02016-08-04 17:48:32 +03002706 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002707 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002708 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002709 {
Martin Radev802abe02016-08-04 17:48:32 +03002710 return;
2711 }
2712
2713 if (typeQualifier.qualifier != EvqUniform)
2714 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002715 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2716 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002717 return;
2718 }
2719
2720 if (mShaderVersion < 300)
2721 {
2722 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2723 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002724 return;
2725 }
2726
Olli Etuaho09b04a22016-12-15 13:30:26 +00002727 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002728
2729 if (layoutQualifier.matrixPacking != EmpUnspecified)
2730 {
2731 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2732 }
2733
2734 if (layoutQualifier.blockStorage != EbsUnspecified)
2735 {
2736 mDefaultBlockStorage = layoutQualifier.blockStorage;
2737 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002738 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002739}
2740
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002741TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2742 const TFunction &function,
2743 const TSourceLoc &location,
2744 bool insertParametersToSymbolTable)
2745{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002746 checkIsNotReserved(location, function.getName());
2747
Olli Etuahofe486322017-03-21 09:30:54 +00002748 TIntermFunctionPrototype *prototype =
2749 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002750 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2751 // point to the data that already exists in the symbol table.
2752 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2753 prototype->setLine(location);
2754
2755 for (size_t i = 0; i < function.getParamCount(); i++)
2756 {
2757 const TConstParameter &param = function.getParam(i);
2758
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002759 TIntermSymbol *symbol = nullptr;
2760
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002761 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2762 // be used for unused args).
2763 if (param.name != nullptr)
2764 {
2765 TVariable *variable = new TVariable(param.name, *param.type);
2766
2767 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002768 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002769 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002770 if (symbolTable.declare(variable))
2771 {
2772 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2773 variable->getType());
2774 }
2775 else
2776 {
2777 error(location, "redefinition", variable->getName().c_str());
2778 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002779 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002780 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002781 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002782 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002783 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2784 // symbol.
2785 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002786 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002787 symbol->setLine(location);
2788 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002789 }
2790 return prototype;
2791}
2792
Olli Etuaho16c745a2017-01-16 17:02:27 +00002793TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2794 const TFunction &parsedFunction,
2795 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002796{
Olli Etuaho476197f2016-10-11 13:59:08 +01002797 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2798 // first declaration. Either way the instance in the symbol table is used to track whether the
2799 // function is declared multiple times.
2800 TFunction *function = static_cast<TFunction *>(
2801 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2802 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002803 {
2804 // ESSL 1.00.17 section 4.2.7.
2805 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2806 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002807 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002808 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002809
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002810 TIntermFunctionPrototype *prototype =
2811 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002812
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002813 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002814
2815 if (!symbolTable.atGlobalLevel())
2816 {
2817 // ESSL 3.00.4 section 4.2.4.
2818 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002819 }
2820
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002821 return prototype;
2822}
2823
Olli Etuaho336b1472016-10-05 16:37:55 +01002824TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002825 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002826 TIntermBlock *functionBody,
2827 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002828{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002829 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002830 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2831 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002832 error(location, "function does not return a value:",
2833 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002834 }
2835
Olli Etuahof51fdd22016-10-03 10:03:40 +01002836 if (functionBody == nullptr)
2837 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002838 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002839 functionBody->setLine(location);
2840 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002841 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002842 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002843 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002844
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002845 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002846 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002847}
2848
Olli Etuaho476197f2016-10-11 13:59:08 +01002849void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2850 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002851 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002852{
Olli Etuaho476197f2016-10-11 13:59:08 +01002853 ASSERT(function);
2854 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002855 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002856 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002857
2858 if (builtIn)
2859 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002860 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002861 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002862 else
Jamie Madill185fb402015-06-12 15:48:48 -04002863 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002864 TFunction *prevDec = static_cast<TFunction *>(
2865 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2866
2867 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2868 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2869 // occurance.
2870 if (*function != prevDec)
2871 {
2872 // Swap the parameters of the previous declaration to the parameters of the function
2873 // definition (parameter names may differ).
2874 prevDec->swapParameters(**function);
2875
2876 // The function definition will share the same symbol as any previous declaration.
2877 *function = prevDec;
2878 }
2879
2880 if ((*function)->isDefined())
2881 {
2882 error(location, "function already has a body", (*function)->getName().c_str());
2883 }
2884
2885 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002886 }
Jamie Madill185fb402015-06-12 15:48:48 -04002887
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002888 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002889 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002890 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002891
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002892 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002893 setLoopNestingLevel(0);
2894}
2895
Jamie Madillb98c3a82015-07-23 14:26:04 -04002896TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002897{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002898 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002899 // We don't know at this point whether this is a function definition or a prototype.
2900 // The definition production code will check for redefinitions.
2901 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002902 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002903 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2904 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002905 //
2906 TFunction *prevDec =
2907 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302908
Martin Radevda6254b2016-12-14 17:00:36 +02002909 if (getShaderVersion() >= 300 &&
2910 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2911 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302912 {
Martin Radevda6254b2016-12-14 17:00:36 +02002913 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302914 // Therefore overloading or redefining builtin functions is an error.
2915 error(location, "Name of a built-in function cannot be redeclared as function",
2916 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302917 }
2918 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002919 {
2920 if (prevDec->getReturnType() != function->getReturnType())
2921 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002922 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002923 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002924 }
2925 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002927 if (prevDec->getParam(i).type->getQualifier() !=
2928 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002929 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002930 error(location,
2931 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002932 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002933 }
2934 }
2935 }
2936
2937 //
2938 // Check for previously declared variables using the same name.
2939 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002940 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002941 if (prevSym)
2942 {
2943 if (!prevSym->isFunction())
2944 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002945 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002946 }
2947 }
2948 else
2949 {
2950 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002951 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002952 }
2953
2954 // We're at the inner scope level of the function's arguments and body statement.
2955 // Add the function prototype to the surrounding scope instead.
2956 symbolTable.getOuterLevel()->insert(function);
2957
Olli Etuaho78d13742017-01-18 13:06:10 +00002958 // Raise error message if main function takes any parameters or return anything other than void
2959 if (function->getName() == "main")
2960 {
2961 if (function->getParamCount() > 0)
2962 {
2963 error(location, "function cannot take any parameter(s)", "main");
2964 }
2965 if (function->getReturnType().getBasicType() != EbtVoid)
2966 {
2967 error(location, "main function cannot return a value",
2968 function->getReturnType().getBasicString());
2969 }
2970 }
2971
Jamie Madill185fb402015-06-12 15:48:48 -04002972 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002973 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2974 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002975 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2976 //
2977 return function;
2978}
2979
Olli Etuaho9de84a52016-06-14 17:36:01 +03002980TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2981 const TString *name,
2982 const TSourceLoc &location)
2983{
2984 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2985 {
2986 error(location, "no qualifiers allowed for function return",
2987 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002988 }
2989 if (!type.layoutQualifier.isEmpty())
2990 {
2991 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002992 }
jchen10cc2a10e2017-05-03 14:05:12 +08002993 // make sure an opaque type is not involved as well...
2994 std::string reason(getBasicString(type.getBasicType()));
2995 reason += "s can't be function return values";
2996 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002997 if (mShaderVersion < 300)
2998 {
2999 // Array return values are forbidden, but there's also no valid syntax for declaring array
3000 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003001 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003002
3003 if (type.isStructureContainingArrays())
3004 {
3005 // ESSL 1.00.17 section 6.1 Function Definitions
3006 error(location, "structures containing arrays can't be function return values",
3007 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003008 }
3009 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003010
3011 // Add the function as a prototype after parsing it (we do not support recursion)
3012 return new TFunction(name, new TType(type));
3013}
3014
Olli Etuahocce89652017-06-19 16:04:09 +03003015TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3016{
Olli Etuahocce89652017-06-19 16:04:09 +03003017 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3018 return new TFunction(name, returnType);
3019}
3020
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003021TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003022{
Olli Etuahocce89652017-06-19 16:04:09 +03003023 if (mShaderVersion < 300 && publicType.array)
3024 {
3025 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3026 "[]");
3027 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003028 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003029 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003030 error(publicType.getLine(), "constructor can't be a structure definition",
3031 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003032 }
3033
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003034 TType *type = new TType(publicType);
3035 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003036 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003037 error(publicType.getLine(), "cannot construct this type",
3038 getBasicString(publicType.getBasicType()));
3039 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003040 }
3041
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003042 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003043}
3044
Olli Etuahocce89652017-06-19 16:04:09 +03003045TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3046 const TString *name,
3047 const TSourceLoc &nameLoc)
3048{
3049 if (publicType.getBasicType() == EbtVoid)
3050 {
3051 error(nameLoc, "illegal use of type 'void'", name->c_str());
3052 }
3053 checkIsNotReserved(nameLoc, *name);
3054 TType *type = new TType(publicType);
3055 TParameter param = {name, type};
3056 return param;
3057}
3058
3059TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3060 const TSourceLoc &identifierLoc,
3061 TIntermTyped *arraySize,
3062 const TSourceLoc &arrayLoc,
3063 TPublicType *type)
3064{
3065 checkIsValidTypeForArray(arrayLoc, *type);
3066 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3067 type->setArraySize(size);
3068 return parseParameterDeclarator(*type, identifier, identifierLoc);
3069}
3070
Jamie Madillb98c3a82015-07-23 14:26:04 -04003071// This function is used to test for the correctness of the parameters passed to various constructor
3072// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003073//
Olli Etuaho856c4972016-08-08 11:38:39 +03003074// 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 +00003075//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003076TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003077 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303078 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079{
Olli Etuaho856c4972016-08-08 11:38:39 +03003080 if (type.isUnsizedArray())
3081 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003082 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003083 {
3084 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3085 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003086 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003087 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003088 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003089 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003090
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003091 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003092 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003093 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003094 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003095
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003096 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003097 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003098
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003099 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3100 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003101 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003102 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003103 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003104 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003105}
3106
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003107//
3108// Interface/uniform blocks
3109//
Olli Etuaho13389b62016-10-16 11:48:18 +01003110TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003111 const TTypeQualifierBuilder &typeQualifierBuilder,
3112 const TSourceLoc &nameLine,
3113 const TString &blockName,
3114 TFieldList *fieldList,
3115 const TString *instanceName,
3116 const TSourceLoc &instanceLine,
3117 TIntermTyped *arrayIndex,
3118 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003119{
Olli Etuaho856c4972016-08-08 11:38:39 +03003120 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003121
Olli Etuaho77ba4082016-12-16 12:01:18 +00003122 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003123
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003124 if (typeQualifier.qualifier != EvqUniform)
3125 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003126 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3127 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003128 }
3129
Martin Radev70866b82016-07-22 15:27:42 +03003130 if (typeQualifier.invariant)
3131 {
3132 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3133 }
3134
Olli Etuaho43364892017-02-13 16:00:12 +00003135 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3136
jchen10af713a22017-04-19 09:10:56 +08003137 // add array index
3138 unsigned int arraySize = 0;
3139 if (arrayIndex != nullptr)
3140 {
3141 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3142 }
3143
3144 if (mShaderVersion < 310)
3145 {
3146 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3147 }
3148 else
3149 {
3150 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3151 arraySize);
3152 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003153
Andrei Volykhina5527072017-03-22 16:46:30 +03003154 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3155
Jamie Madill099c0f32013-06-20 11:55:52 -04003156 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003157 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003158
Jamie Madill099c0f32013-06-20 11:55:52 -04003159 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3160 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003161 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003162 }
3163
Jamie Madill1566ef72013-06-20 11:55:54 -04003164 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3165 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003166 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003167 }
3168
Olli Etuaho856c4972016-08-08 11:38:39 +03003169 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003170
Martin Radev2cc85b32016-08-05 16:22:53 +03003171 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3172
Arun Patole7e7e68d2015-05-22 12:02:25 +05303173 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3174 if (!symbolTable.declare(blockNameSymbol))
3175 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003176 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003177 }
3178
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303180 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3181 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303183 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003184 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303185 {
jchen10cc2a10e2017-05-03 14:05:12 +08003186 std::string reason("unsupported type - ");
3187 reason += fieldType->getBasicString();
3188 reason += " types are not allowed in interface blocks";
3189 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003190 }
3191
Jamie Madill98493dd2013-07-08 14:39:03 -04003192 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003193 switch (qualifier)
3194 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003195 case EvqGlobal:
3196 case EvqUniform:
3197 break;
3198 default:
3199 error(field->line(), "invalid qualifier on interface block member",
3200 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003201 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003202 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003203
Martin Radev70866b82016-07-22 15:27:42 +03003204 if (fieldType->isInvariant())
3205 {
3206 error(field->line(), "invalid qualifier on interface block member", "invariant");
3207 }
3208
Jamie Madilla5efff92013-06-06 11:56:47 -04003209 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003210 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003211 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003212 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003213
Jamie Madill98493dd2013-07-08 14:39:03 -04003214 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003215 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003216 error(field->line(), "invalid layout qualifier: cannot be used here",
3217 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003218 }
3219
Jamie Madill98493dd2013-07-08 14:39:03 -04003220 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003221 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003222 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003223 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003224 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003225 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003226 warning(field->line(),
3227 "extraneous layout qualifier: only has an effect on matrix types",
3228 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003229 }
3230
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003232 }
3233
Jamie Madillb98c3a82015-07-23 14:26:04 -04003234 TInterfaceBlock *interfaceBlock =
3235 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3236 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3237 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003238
3239 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003240 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003241
Jamie Madill98493dd2013-07-08 14:39:03 -04003242 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003243 {
3244 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003245 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3246 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003247 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303248 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003249
3250 // set parent pointer of the field variable
3251 fieldType->setInterfaceBlock(interfaceBlock);
3252
Arun Patole7e7e68d2015-05-22 12:02:25 +05303253 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003254 fieldVariable->setQualifier(typeQualifier.qualifier);
3255
Arun Patole7e7e68d2015-05-22 12:02:25 +05303256 if (!symbolTable.declare(fieldVariable))
3257 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003258 error(field->line(), "redefinition of an interface block member name",
3259 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003260 }
3261 }
3262 }
3263 else
3264 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003265 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003266
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003267 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303268 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003269 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003270
Arun Patole7e7e68d2015-05-22 12:02:25 +05303271 if (!symbolTable.declare(instanceTypeDef))
3272 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003273 error(instanceLine, "redefinition of an interface block instance name",
3274 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003275 }
3276
Jamie Madillb98c3a82015-07-23 14:26:04 -04003277 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003278 symbolName = instanceTypeDef->getName();
3279 }
3280
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003281 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3282 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003283 TIntermDeclaration *declaration = new TIntermDeclaration();
3284 declaration->appendDeclarator(blockSymbol);
3285 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003286
3287 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003288 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003289}
3290
Olli Etuaho383b7912016-08-05 11:22:59 +03003291void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003292{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003293 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003294
3295 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003296 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303297 if (mStructNestingLevel > 1)
3298 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003299 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003300 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003301}
3302
3303void TParseContext::exitStructDeclaration()
3304{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003305 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003306}
3307
Olli Etuaho8a176262016-08-16 14:23:01 +03003308void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003309{
Jamie Madillacb4b812016-11-07 13:50:29 -05003310 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303311 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003312 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003313 }
3314
Arun Patole7e7e68d2015-05-22 12:02:25 +05303315 if (field.type()->getBasicType() != EbtStruct)
3316 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003317 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003318 }
3319
3320 // We're already inside a structure definition at this point, so add
3321 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303322 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3323 {
Jamie Madill41a49272014-03-18 16:10:13 -04003324 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003325 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3326 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003327 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003328 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003329 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003330 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003331}
3332
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003333//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003334// Parse an array index expression
3335//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003336TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3337 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303338 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003339{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003340 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3341 {
3342 if (baseExpression->getAsSymbolNode())
3343 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303344 error(location, " left of '[' is not of type array, matrix, or vector ",
3345 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003346 }
3347 else
3348 {
3349 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3350 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003351
Olli Etuaho3ec75682017-07-05 17:02:55 +03003352 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003353 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003354
Jamie Madill21c1e452014-12-29 11:33:41 -05003355 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3356
Olli Etuaho36b05142015-11-12 13:10:42 +02003357 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3358 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3359 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3360 // index is a constant expression.
3361 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3362 {
3363 if (baseExpression->isInterfaceBlock())
3364 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003365 error(location,
3366 "array indexes for interface blocks arrays must be constant integral expressions",
3367 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003368 }
3369 else if (baseExpression->getQualifier() == EvqFragmentOut)
3370 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003371 error(location,
3372 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003373 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003374 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3375 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003376 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003377 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003378 }
3379
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003380 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003381 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003382 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3383 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3384 // constant fold expressions that are not constant expressions). The most compatible way to
3385 // handle this case is to report a warning instead of an error and force the index to be in
3386 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003387 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003388 int index = 0;
3389 if (indexConstantUnion->getBasicType() == EbtInt)
3390 {
3391 index = indexConstantUnion->getIConst(0);
3392 }
3393 else if (indexConstantUnion->getBasicType() == EbtUInt)
3394 {
3395 index = static_cast<int>(indexConstantUnion->getUConst(0));
3396 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003397
3398 int safeIndex = -1;
3399
3400 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003401 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003402 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003403 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003404 if (mShaderSpec == SH_WEBGL2_SPEC)
3405 {
3406 // Error has been already generated if index is not const.
3407 if (indexExpression->getQualifier() == EvqConst)
3408 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003409 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003410 }
3411 safeIndex = 0;
3412 }
3413 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3414 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003415 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003416 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003417 "GL_EXT_draw_buffers is disabled",
3418 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003419 safeIndex = 0;
3420 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003421 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003422 // Only do generic out-of-range check if similar error hasn't already been reported.
3423 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003424 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003425 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3426 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003427 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003428 }
3429 }
3430 else if (baseExpression->isMatrix())
3431 {
3432 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003433 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003434 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003435 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003436 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003437 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003438 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3439 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003440 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003441 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003442
3443 ASSERT(safeIndex >= 0);
3444 // Data of constant unions can't be changed, because it may be shared with other
3445 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3446 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003447 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003448 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003449 TConstantUnion *safeConstantUnion = new TConstantUnion();
3450 safeConstantUnion->setIConst(safeIndex);
3451 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003452 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003453 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003454
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003455 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3456 node->setLine(location);
3457 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003458 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003459 else
3460 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003461 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3462 node->setLine(location);
3463 // Indirect indexing can never be constant folded.
3464 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003465 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003466}
3467
Olli Etuaho90892fb2016-07-14 14:44:51 +03003468int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3469 const TSourceLoc &location,
3470 int index,
3471 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003472 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003473{
3474 if (index >= arraySize || index < 0)
3475 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003476 std::stringstream reasonStream;
3477 reasonStream << reason << " '" << index << "'";
3478 std::string token = reasonStream.str();
3479 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003480 if (index < 0)
3481 {
3482 return 0;
3483 }
3484 else
3485 {
3486 return arraySize - 1;
3487 }
3488 }
3489 return index;
3490}
3491
Jamie Madillb98c3a82015-07-23 14:26:04 -04003492TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3493 const TSourceLoc &dotLocation,
3494 const TString &fieldString,
3495 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003496{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003497 if (baseExpression->isArray())
3498 {
3499 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003500 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003501 }
3502
3503 if (baseExpression->isVector())
3504 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003505 TVector<int> fieldOffsets;
3506 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3507 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003508 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003509 fieldOffsets.resize(1);
3510 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003511 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003512 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3513 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003514
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003515 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003516 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003517 else if (baseExpression->getBasicType() == EbtStruct)
3518 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303519 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003520 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003521 {
3522 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003523 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003524 }
3525 else
3526 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003527 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003528 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003529 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003530 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003531 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003532 {
3533 fieldFound = true;
3534 break;
3535 }
3536 }
3537 if (fieldFound)
3538 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003539 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003540 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003541 TIntermBinary *node =
3542 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3543 node->setLine(dotLocation);
3544 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003545 }
3546 else
3547 {
3548 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003549 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003550 }
3551 }
3552 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003553 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003554 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303555 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003556 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003557 {
3558 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003559 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003560 }
3561 else
3562 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003563 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003564 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003565 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003566 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003567 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003568 {
3569 fieldFound = true;
3570 break;
3571 }
3572 }
3573 if (fieldFound)
3574 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003575 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003576 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003577 TIntermBinary *node =
3578 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3579 node->setLine(dotLocation);
3580 // Indexing interface blocks can never be constant folded.
3581 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003582 }
3583 else
3584 {
3585 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003586 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003587 }
3588 }
3589 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003590 else
3591 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003592 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003593 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003594 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303595 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003596 }
3597 else
3598 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303599 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003600 " field selection requires structure, vector, or interface block on left hand "
3601 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303602 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003603 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003604 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003605 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003606}
3607
Jamie Madillb98c3a82015-07-23 14:26:04 -04003608TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3609 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003610{
Martin Radev802abe02016-08-04 17:48:32 +03003611 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003612
3613 if (qualifierType == "shared")
3614 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003615 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003616 {
3617 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3618 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003619 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003620 }
3621 else if (qualifierType == "packed")
3622 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003623 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003624 {
3625 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3626 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003627 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003628 }
3629 else if (qualifierType == "std140")
3630 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003631 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003632 }
3633 else if (qualifierType == "row_major")
3634 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003635 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003636 }
3637 else if (qualifierType == "column_major")
3638 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003639 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003640 }
3641 else if (qualifierType == "location")
3642 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003643 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3644 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003645 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003646 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3647 mShaderType == GL_FRAGMENT_SHADER)
3648 {
3649 qualifier.yuv = true;
3650 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003651 else if (qualifierType == "rgba32f")
3652 {
3653 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3654 qualifier.imageInternalFormat = EiifRGBA32F;
3655 }
3656 else if (qualifierType == "rgba16f")
3657 {
3658 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3659 qualifier.imageInternalFormat = EiifRGBA16F;
3660 }
3661 else if (qualifierType == "r32f")
3662 {
3663 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3664 qualifier.imageInternalFormat = EiifR32F;
3665 }
3666 else if (qualifierType == "rgba8")
3667 {
3668 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3669 qualifier.imageInternalFormat = EiifRGBA8;
3670 }
3671 else if (qualifierType == "rgba8_snorm")
3672 {
3673 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3674 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3675 }
3676 else if (qualifierType == "rgba32i")
3677 {
3678 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3679 qualifier.imageInternalFormat = EiifRGBA32I;
3680 }
3681 else if (qualifierType == "rgba16i")
3682 {
3683 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3684 qualifier.imageInternalFormat = EiifRGBA16I;
3685 }
3686 else if (qualifierType == "rgba8i")
3687 {
3688 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3689 qualifier.imageInternalFormat = EiifRGBA8I;
3690 }
3691 else if (qualifierType == "r32i")
3692 {
3693 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3694 qualifier.imageInternalFormat = EiifR32I;
3695 }
3696 else if (qualifierType == "rgba32ui")
3697 {
3698 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3699 qualifier.imageInternalFormat = EiifRGBA32UI;
3700 }
3701 else if (qualifierType == "rgba16ui")
3702 {
3703 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3704 qualifier.imageInternalFormat = EiifRGBA16UI;
3705 }
3706 else if (qualifierType == "rgba8ui")
3707 {
3708 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3709 qualifier.imageInternalFormat = EiifRGBA8UI;
3710 }
3711 else if (qualifierType == "r32ui")
3712 {
3713 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3714 qualifier.imageInternalFormat = EiifR32UI;
3715 }
3716
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003717 else
3718 {
3719 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003720 }
3721
Jamie Madilla5efff92013-06-06 11:56:47 -04003722 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003723}
3724
Martin Radev802abe02016-08-04 17:48:32 +03003725void TParseContext::parseLocalSize(const TString &qualifierType,
3726 const TSourceLoc &qualifierTypeLine,
3727 int intValue,
3728 const TSourceLoc &intValueLine,
3729 const std::string &intValueString,
3730 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003731 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003732{
Olli Etuaho856c4972016-08-08 11:38:39 +03003733 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003734 if (intValue < 1)
3735 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003736 std::stringstream reasonStream;
3737 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3738 std::string reason = reasonStream.str();
3739 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003740 }
3741 (*localSize)[index] = intValue;
3742}
3743
Olli Etuaho09b04a22016-12-15 13:30:26 +00003744void TParseContext::parseNumViews(int intValue,
3745 const TSourceLoc &intValueLine,
3746 const std::string &intValueString,
3747 int *numViews)
3748{
3749 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3750 // specification.
3751 if (intValue < 1)
3752 {
3753 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3754 }
3755 *numViews = intValue;
3756}
3757
Jamie Madillb98c3a82015-07-23 14:26:04 -04003758TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3759 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003760 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303761 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003762{
Martin Radev802abe02016-08-04 17:48:32 +03003763 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003764
Martin Radev802abe02016-08-04 17:48:32 +03003765 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003766
Martin Radev802abe02016-08-04 17:48:32 +03003767 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003768 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003769 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003770 if (intValue < 0)
3771 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003772 error(intValueLine, "out of range: location must be non-negative",
3773 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003774 }
3775 else
3776 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003777 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003778 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003779 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003780 }
Olli Etuaho43364892017-02-13 16:00:12 +00003781 else if (qualifierType == "binding")
3782 {
3783 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3784 if (intValue < 0)
3785 {
3786 error(intValueLine, "out of range: binding must be non-negative",
3787 intValueString.c_str());
3788 }
3789 else
3790 {
3791 qualifier.binding = intValue;
3792 }
3793 }
jchen104cdac9e2017-05-08 11:01:20 +08003794 else if (qualifierType == "offset")
3795 {
3796 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3797 if (intValue < 0)
3798 {
3799 error(intValueLine, "out of range: offset must be non-negative",
3800 intValueString.c_str());
3801 }
3802 else
3803 {
3804 qualifier.offset = intValue;
3805 }
3806 }
Martin Radev802abe02016-08-04 17:48:32 +03003807 else if (qualifierType == "local_size_x")
3808 {
3809 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3810 &qualifier.localSize);
3811 }
3812 else if (qualifierType == "local_size_y")
3813 {
3814 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3815 &qualifier.localSize);
3816 }
3817 else if (qualifierType == "local_size_z")
3818 {
3819 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3820 &qualifier.localSize);
3821 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003822 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003823 mShaderType == GL_VERTEX_SHADER)
3824 {
3825 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3826 }
Martin Radev802abe02016-08-04 17:48:32 +03003827 else
3828 {
3829 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003830 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003831
Jamie Madilla5efff92013-06-06 11:56:47 -04003832 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003833}
3834
Olli Etuaho613b9592016-09-05 12:05:53 +03003835TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3836{
3837 return new TTypeQualifierBuilder(
3838 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3839 mShaderVersion);
3840}
3841
Olli Etuahocce89652017-06-19 16:04:09 +03003842TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3843 const TSourceLoc &loc)
3844{
3845 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3846 return new TStorageQualifierWrapper(qualifier, loc);
3847}
3848
3849TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3850{
3851 if (getShaderType() == GL_VERTEX_SHADER)
3852 {
3853 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3854 }
3855 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3856}
3857
3858TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3859{
3860 if (declaringFunction())
3861 {
3862 return new TStorageQualifierWrapper(EvqIn, loc);
3863 }
3864 if (getShaderType() == GL_FRAGMENT_SHADER)
3865 {
3866 if (mShaderVersion < 300)
3867 {
3868 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3869 }
3870 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3871 }
3872 if (getShaderType() == GL_VERTEX_SHADER)
3873 {
3874 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3875 {
3876 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3877 }
3878 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3879 }
3880 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3881}
3882
3883TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3884{
3885 if (declaringFunction())
3886 {
3887 return new TStorageQualifierWrapper(EvqOut, loc);
3888 }
3889 if (mShaderVersion < 300)
3890 {
3891 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3892 }
3893 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3894 {
3895 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3896 }
3897 if (getShaderType() == GL_VERTEX_SHADER)
3898 {
3899 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3900 }
3901 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3902}
3903
3904TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3905{
3906 if (!declaringFunction())
3907 {
3908 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3909 }
3910 return new TStorageQualifierWrapper(EvqInOut, loc);
3911}
3912
Jamie Madillb98c3a82015-07-23 14:26:04 -04003913TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003914 TLayoutQualifier rightQualifier,
3915 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003916{
Martin Radevc28888b2016-07-22 15:27:42 +03003917 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003918 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003919}
3920
Olli Etuahocce89652017-06-19 16:04:09 +03003921TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3922{
3923 checkIsNotReserved(loc, *identifier);
3924 TType *type = new TType(EbtVoid, EbpUndefined);
3925 return new TField(type, identifier, loc);
3926}
3927
3928TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3929 const TSourceLoc &loc,
3930 TIntermTyped *arraySize,
3931 const TSourceLoc &arraySizeLoc)
3932{
3933 checkIsNotReserved(loc, *identifier);
3934
3935 TType *type = new TType(EbtVoid, EbpUndefined);
3936 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3937 type->setArraySize(size);
3938
3939 return new TField(type, identifier, loc);
3940}
3941
Olli Etuaho4de340a2016-12-16 09:32:03 +00003942TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3943 const TFieldList *newlyAddedFields,
3944 const TSourceLoc &location)
3945{
3946 for (TField *field : *newlyAddedFields)
3947 {
3948 for (TField *oldField : *processedFields)
3949 {
3950 if (oldField->name() == field->name())
3951 {
3952 error(location, "duplicate field name in structure", field->name().c_str());
3953 }
3954 }
3955 processedFields->push_back(field);
3956 }
3957 return processedFields;
3958}
3959
Martin Radev70866b82016-07-22 15:27:42 +03003960TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3961 const TTypeQualifierBuilder &typeQualifierBuilder,
3962 TPublicType *typeSpecifier,
3963 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003964{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003965 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003966
Martin Radev70866b82016-07-22 15:27:42 +03003967 typeSpecifier->qualifier = typeQualifier.qualifier;
3968 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003969 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003970 typeSpecifier->invariant = typeQualifier.invariant;
3971 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303972 {
Martin Radev70866b82016-07-22 15:27:42 +03003973 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003974 }
Martin Radev70866b82016-07-22 15:27:42 +03003975 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003976}
3977
Jamie Madillb98c3a82015-07-23 14:26:04 -04003978TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3979 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003980{
Martin Radev4a9cd802016-09-01 16:51:51 +03003981 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3982 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003983
Martin Radev4a9cd802016-09-01 16:51:51 +03003984 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003985
Martin Radev4a9cd802016-09-01 16:51:51 +03003986 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003987
Arun Patole7e7e68d2015-05-22 12:02:25 +05303988 for (unsigned int i = 0; i < fieldList->size(); ++i)
3989 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003990 //
3991 // Careful not to replace already known aspects of type, like array-ness
3992 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303993 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003994 type->setBasicType(typeSpecifier.getBasicType());
3995 type->setPrimarySize(typeSpecifier.getPrimarySize());
3996 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003997 type->setPrecision(typeSpecifier.precision);
3998 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003999 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004000 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004001 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004002
4003 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304004 if (type->isArray())
4005 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004006 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004007 }
4008 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004009 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004010 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304011 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004012 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004013 }
4014
Martin Radev4a9cd802016-09-01 16:51:51 +03004015 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004016 }
4017
Jamie Madill98493dd2013-07-08 14:39:03 -04004018 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004019}
4020
Martin Radev4a9cd802016-09-01 16:51:51 +03004021TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4022 const TSourceLoc &nameLine,
4023 const TString *structName,
4024 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004025{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304026 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004027 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004028
Jamie Madill9b820842015-02-12 10:40:10 -05004029 // Store a bool in the struct if we're at global scope, to allow us to
4030 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004031 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004032
Jamie Madill98493dd2013-07-08 14:39:03 -04004033 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004034 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004035 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304036 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4037 if (!symbolTable.declare(userTypeDef))
4038 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004039 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004040 }
4041 }
4042
4043 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004044 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004045 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004046 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004047 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004048 switch (qualifier)
4049 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004050 case EvqGlobal:
4051 case EvqTemporary:
4052 break;
4053 default:
4054 error(field.line(), "invalid qualifier on struct member",
4055 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004056 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004057 }
Martin Radev70866b82016-07-22 15:27:42 +03004058 if (field.type()->isInvariant())
4059 {
4060 error(field.line(), "invalid qualifier on struct member", "invariant");
4061 }
jchen104cdac9e2017-05-08 11:01:20 +08004062 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4063 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004064 {
4065 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4066 }
4067
Olli Etuaho43364892017-02-13 16:00:12 +00004068 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4069
4070 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004071
4072 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004073 }
4074
Martin Radev4a9cd802016-09-01 16:51:51 +03004075 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004076 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004077 exitStructDeclaration();
4078
Martin Radev4a9cd802016-09-01 16:51:51 +03004079 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004080}
4081
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004083 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004084 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004085{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004086 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004087 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004088 init->isVector())
4089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004090 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4091 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004092 return nullptr;
4093 }
4094
Olli Etuahoac5274d2015-02-20 10:19:08 +02004095 if (statementList)
4096 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004097 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004098 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004099 return nullptr;
4100 }
4101 }
4102
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004103 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4104 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004105 return node;
4106}
4107
4108TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4109{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004110 if (mSwitchNestingLevel == 0)
4111 {
4112 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004113 return nullptr;
4114 }
4115 if (condition == nullptr)
4116 {
4117 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004118 return nullptr;
4119 }
4120 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004121 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004122 {
4123 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004124 }
4125 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004126 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4127 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4128 // fold in case labels.
4129 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004130 {
4131 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004132 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004133 TIntermCase *node = new TIntermCase(condition);
4134 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004135 return node;
4136}
4137
4138TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4139{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004140 if (mSwitchNestingLevel == 0)
4141 {
4142 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004143 return nullptr;
4144 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004145 TIntermCase *node = new TIntermCase(nullptr);
4146 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004147 return node;
4148}
4149
Jamie Madillb98c3a82015-07-23 14:26:04 -04004150TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4151 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004152 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004153{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004154 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004155
4156 switch (op)
4157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004158 case EOpLogicalNot:
4159 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4160 child->isVector())
4161 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004162 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004163 return nullptr;
4164 }
4165 break;
4166 case EOpBitwiseNot:
4167 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4168 child->isMatrix() || child->isArray())
4169 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004170 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004171 return nullptr;
4172 }
4173 break;
4174 case EOpPostIncrement:
4175 case EOpPreIncrement:
4176 case EOpPostDecrement:
4177 case EOpPreDecrement:
4178 case EOpNegative:
4179 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004180 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4181 child->getBasicType() == EbtBool || child->isArray() ||
4182 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004183 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004184 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185 return nullptr;
4186 }
4187 // Operators for built-ins are already type checked against their prototype.
4188 default:
4189 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004190 }
4191
Olli Etuahof119a262016-08-19 15:54:22 +03004192 TIntermUnary *node = new TIntermUnary(op, child);
4193 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004194
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004195 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004196}
4197
Olli Etuaho09b22472015-02-11 11:47:26 +02004198TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4199{
Olli Etuahocce89652017-06-19 16:04:09 +03004200 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004201 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004202 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004203 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004204 return child;
4205 }
4206 return node;
4207}
4208
Jamie Madillb98c3a82015-07-23 14:26:04 -04004209TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4210 TIntermTyped *child,
4211 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004212{
Olli Etuaho856c4972016-08-08 11:38:39 +03004213 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004214 return addUnaryMath(op, child, loc);
4215}
4216
Jamie Madillb98c3a82015-07-23 14:26:04 -04004217bool TParseContext::binaryOpCommonCheck(TOperator op,
4218 TIntermTyped *left,
4219 TIntermTyped *right,
4220 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004221{
jchen10b4cf5652017-05-05 18:51:17 +08004222 // Check opaque types are not allowed to be operands in expressions other than array indexing
4223 // and structure member selection.
4224 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4225 {
4226 switch (op)
4227 {
4228 case EOpIndexDirect:
4229 case EOpIndexIndirect:
4230 break;
4231 case EOpIndexDirectStruct:
4232 UNREACHABLE();
4233
4234 default:
4235 error(loc, "Invalid operation for variables with an opaque type",
4236 GetOperatorString(op));
4237 return false;
4238 }
4239 }
jchen10cc2a10e2017-05-03 14:05:12 +08004240
Olli Etuaho244be012016-08-18 15:26:02 +03004241 if (left->getType().getStruct() || right->getType().getStruct())
4242 {
4243 switch (op)
4244 {
4245 case EOpIndexDirectStruct:
4246 ASSERT(left->getType().getStruct());
4247 break;
4248 case EOpEqual:
4249 case EOpNotEqual:
4250 case EOpAssign:
4251 case EOpInitialize:
4252 if (left->getType() != right->getType())
4253 {
4254 return false;
4255 }
4256 break;
4257 default:
4258 error(loc, "Invalid operation for structs", GetOperatorString(op));
4259 return false;
4260 }
4261 }
4262
Olli Etuaho94050052017-05-08 14:17:44 +03004263 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4264 {
4265 switch (op)
4266 {
4267 case EOpIndexDirectInterfaceBlock:
4268 ASSERT(left->getType().getInterfaceBlock());
4269 break;
4270 default:
4271 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4272 return false;
4273 }
4274 }
4275
Olli Etuahod6b14282015-03-17 14:31:35 +02004276 if (left->isArray() || right->isArray())
4277 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004278 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004279 {
4280 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4281 return false;
4282 }
4283
4284 if (left->isArray() != right->isArray())
4285 {
4286 error(loc, "array / non-array mismatch", GetOperatorString(op));
4287 return false;
4288 }
4289
4290 switch (op)
4291 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004292 case EOpEqual:
4293 case EOpNotEqual:
4294 case EOpAssign:
4295 case EOpInitialize:
4296 break;
4297 default:
4298 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4299 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004300 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004301 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004302 if (left->getArraySize() != right->getArraySize())
4303 {
4304 error(loc, "array size mismatch", GetOperatorString(op));
4305 return false;
4306 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004307 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004308
4309 // Check ops which require integer / ivec parameters
4310 bool isBitShift = false;
4311 switch (op)
4312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004313 case EOpBitShiftLeft:
4314 case EOpBitShiftRight:
4315 case EOpBitShiftLeftAssign:
4316 case EOpBitShiftRightAssign:
4317 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4318 // check that the basic type is an integer type.
4319 isBitShift = true;
4320 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4321 {
4322 return false;
4323 }
4324 break;
4325 case EOpBitwiseAnd:
4326 case EOpBitwiseXor:
4327 case EOpBitwiseOr:
4328 case EOpBitwiseAndAssign:
4329 case EOpBitwiseXorAssign:
4330 case EOpBitwiseOrAssign:
4331 // It is enough to check the type of only one operand, since later it
4332 // is checked that the operand types match.
4333 if (!IsInteger(left->getBasicType()))
4334 {
4335 return false;
4336 }
4337 break;
4338 default:
4339 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004340 }
4341
4342 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4343 // So the basic type should usually match.
4344 if (!isBitShift && left->getBasicType() != right->getBasicType())
4345 {
4346 return false;
4347 }
4348
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004349 // Check that:
4350 // 1. Type sizes match exactly on ops that require that.
4351 // 2. Restrictions for structs that contain arrays or samplers are respected.
4352 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004353 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004354 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004355 case EOpAssign:
4356 case EOpInitialize:
4357 case EOpEqual:
4358 case EOpNotEqual:
4359 // ESSL 1.00 sections 5.7, 5.8, 5.9
4360 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4361 {
4362 error(loc, "undefined operation for structs containing arrays",
4363 GetOperatorString(op));
4364 return false;
4365 }
4366 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4367 // we interpret the spec so that this extends to structs containing samplers,
4368 // similarly to ESSL 1.00 spec.
4369 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4370 left->getType().isStructureContainingSamplers())
4371 {
4372 error(loc, "undefined operation for structs containing samplers",
4373 GetOperatorString(op));
4374 return false;
4375 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004376
Olli Etuahoe1805592017-01-02 16:41:20 +00004377 if ((left->getNominalSize() != right->getNominalSize()) ||
4378 (left->getSecondarySize() != right->getSecondarySize()))
4379 {
4380 error(loc, "dimension mismatch", GetOperatorString(op));
4381 return false;
4382 }
4383 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004384 case EOpLessThan:
4385 case EOpGreaterThan:
4386 case EOpLessThanEqual:
4387 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004388 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004389 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004390 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004391 return false;
4392 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004393 break;
4394 case EOpAdd:
4395 case EOpSub:
4396 case EOpDiv:
4397 case EOpIMod:
4398 case EOpBitShiftLeft:
4399 case EOpBitShiftRight:
4400 case EOpBitwiseAnd:
4401 case EOpBitwiseXor:
4402 case EOpBitwiseOr:
4403 case EOpAddAssign:
4404 case EOpSubAssign:
4405 case EOpDivAssign:
4406 case EOpIModAssign:
4407 case EOpBitShiftLeftAssign:
4408 case EOpBitShiftRightAssign:
4409 case EOpBitwiseAndAssign:
4410 case EOpBitwiseXorAssign:
4411 case EOpBitwiseOrAssign:
4412 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4413 {
4414 return false;
4415 }
4416
4417 // Are the sizes compatible?
4418 if (left->getNominalSize() != right->getNominalSize() ||
4419 left->getSecondarySize() != right->getSecondarySize())
4420 {
4421 // If the nominal sizes of operands do not match:
4422 // One of them must be a scalar.
4423 if (!left->isScalar() && !right->isScalar())
4424 return false;
4425
4426 // In the case of compound assignment other than multiply-assign,
4427 // the right side needs to be a scalar. Otherwise a vector/matrix
4428 // would be assigned to a scalar. A scalar can't be shifted by a
4429 // vector either.
4430 if (!right->isScalar() &&
4431 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4432 return false;
4433 }
4434 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004435 default:
4436 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004437 }
4438
Olli Etuahod6b14282015-03-17 14:31:35 +02004439 return true;
4440}
4441
Olli Etuaho1dded802016-08-18 18:13:13 +03004442bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4443 const TType &left,
4444 const TType &right)
4445{
4446 switch (op)
4447 {
4448 case EOpMul:
4449 case EOpMulAssign:
4450 return left.getNominalSize() == right.getNominalSize() &&
4451 left.getSecondarySize() == right.getSecondarySize();
4452 case EOpVectorTimesScalar:
4453 return true;
4454 case EOpVectorTimesScalarAssign:
4455 ASSERT(!left.isMatrix() && !right.isMatrix());
4456 return left.isVector() && !right.isVector();
4457 case EOpVectorTimesMatrix:
4458 return left.getNominalSize() == right.getRows();
4459 case EOpVectorTimesMatrixAssign:
4460 ASSERT(!left.isMatrix() && right.isMatrix());
4461 return left.isVector() && left.getNominalSize() == right.getRows() &&
4462 left.getNominalSize() == right.getCols();
4463 case EOpMatrixTimesVector:
4464 return left.getCols() == right.getNominalSize();
4465 case EOpMatrixTimesScalar:
4466 return true;
4467 case EOpMatrixTimesScalarAssign:
4468 ASSERT(left.isMatrix() && !right.isMatrix());
4469 return !right.isVector();
4470 case EOpMatrixTimesMatrix:
4471 return left.getCols() == right.getRows();
4472 case EOpMatrixTimesMatrixAssign:
4473 ASSERT(left.isMatrix() && right.isMatrix());
4474 // We need to check two things:
4475 // 1. The matrix multiplication step is valid.
4476 // 2. The result will have the same number of columns as the lvalue.
4477 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4478
4479 default:
4480 UNREACHABLE();
4481 return false;
4482 }
4483}
4484
Jamie Madillb98c3a82015-07-23 14:26:04 -04004485TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4486 TIntermTyped *left,
4487 TIntermTyped *right,
4488 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004489{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004490 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004491 return nullptr;
4492
Olli Etuahofc1806e2015-03-17 13:03:11 +02004493 switch (op)
4494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004495 case EOpEqual:
4496 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004497 case EOpLessThan:
4498 case EOpGreaterThan:
4499 case EOpLessThanEqual:
4500 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004501 break;
4502 case EOpLogicalOr:
4503 case EOpLogicalXor:
4504 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004505 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4506 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004507 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004508 {
4509 return nullptr;
4510 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004511 // Basic types matching should have been already checked.
4512 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004513 break;
4514 case EOpAdd:
4515 case EOpSub:
4516 case EOpDiv:
4517 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004518 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4519 !right->getType().getStruct());
4520 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004521 {
4522 return nullptr;
4523 }
4524 break;
4525 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004526 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4527 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004528 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004529 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004530 {
4531 return nullptr;
4532 }
4533 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004534 default:
4535 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004536 }
4537
Olli Etuaho1dded802016-08-18 18:13:13 +03004538 if (op == EOpMul)
4539 {
4540 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4541 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4542 {
4543 return nullptr;
4544 }
4545 }
4546
Olli Etuaho3fdec912016-08-18 15:08:06 +03004547 TIntermBinary *node = new TIntermBinary(op, left, right);
4548 node->setLine(loc);
4549
Olli Etuaho3fdec912016-08-18 15:08:06 +03004550 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004551 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004552}
4553
Jamie Madillb98c3a82015-07-23 14:26:04 -04004554TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4555 TIntermTyped *left,
4556 TIntermTyped *right,
4557 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004558{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004559 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004560 if (node == 0)
4561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004562 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4563 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004564 return left;
4565 }
4566 return node;
4567}
4568
Jamie Madillb98c3a82015-07-23 14:26:04 -04004569TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4570 TIntermTyped *left,
4571 TIntermTyped *right,
4572 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004573{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004574 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004575 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004576 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004577 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4578 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03004579 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03004580 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004581 }
4582 return node;
4583}
4584
Olli Etuaho13389b62016-10-16 11:48:18 +01004585TIntermBinary *TParseContext::createAssign(TOperator op,
4586 TIntermTyped *left,
4587 TIntermTyped *right,
4588 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004589{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004590 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004591 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004592 if (op == EOpMulAssign)
4593 {
4594 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4595 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4596 {
4597 return nullptr;
4598 }
4599 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004600 TIntermBinary *node = new TIntermBinary(op, left, right);
4601 node->setLine(loc);
4602
Olli Etuaho3fdec912016-08-18 15:08:06 +03004603 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004604 }
4605 return nullptr;
4606}
4607
Jamie Madillb98c3a82015-07-23 14:26:04 -04004608TIntermTyped *TParseContext::addAssign(TOperator op,
4609 TIntermTyped *left,
4610 TIntermTyped *right,
4611 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004612{
Olli Etuahocce89652017-06-19 16:04:09 +03004613 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004614 TIntermTyped *node = createAssign(op, left, right, loc);
4615 if (node == nullptr)
4616 {
4617 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004618 return left;
4619 }
4620 return node;
4621}
4622
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004623TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4624 TIntermTyped *right,
4625 const TSourceLoc &loc)
4626{
Corentin Wallez0d959252016-07-12 17:26:32 -04004627 // WebGL2 section 5.26, the following results in an error:
4628 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004629 if (mShaderSpec == SH_WEBGL2_SPEC &&
4630 (left->isArray() || left->getBasicType() == EbtVoid ||
4631 left->getType().isStructureContainingArrays() || right->isArray() ||
4632 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004633 {
4634 error(loc,
4635 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4636 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004637 }
4638
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004639 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4640 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4641 commaNode->getTypePointer()->setQualifier(resultQualifier);
4642 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004643}
4644
Olli Etuaho49300862015-02-20 14:54:49 +02004645TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4646{
4647 switch (op)
4648 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004649 case EOpContinue:
4650 if (mLoopNestingLevel <= 0)
4651 {
4652 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004653 }
4654 break;
4655 case EOpBreak:
4656 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4657 {
4658 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004659 }
4660 break;
4661 case EOpReturn:
4662 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4663 {
4664 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004665 }
4666 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004667 case EOpKill:
4668 if (mShaderType != GL_FRAGMENT_SHADER)
4669 {
4670 error(loc, "discard supported in fragment shaders only", "discard");
4671 }
4672 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004673 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004674 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004675 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004676 }
Olli Etuahocce89652017-06-19 16:04:09 +03004677 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004678}
4679
Jamie Madillb98c3a82015-07-23 14:26:04 -04004680TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004681 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004682 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004683{
Olli Etuahocce89652017-06-19 16:04:09 +03004684 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004685 {
Olli Etuahocce89652017-06-19 16:04:09 +03004686 ASSERT(op == EOpReturn);
4687 mFunctionReturnsValue = true;
4688 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4689 {
4690 error(loc, "void function cannot return a value", "return");
4691 }
4692 else if (*mCurrentFunctionType != expression->getType())
4693 {
4694 error(loc, "function return is not matching type:", "return");
4695 }
Olli Etuaho49300862015-02-20 14:54:49 +02004696 }
Olli Etuahocce89652017-06-19 16:04:09 +03004697 TIntermBranch *node = new TIntermBranch(op, expression);
4698 node->setLine(loc);
4699 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004700}
4701
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004702void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4703{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004704 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004705 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004706 TIntermNode *offset = nullptr;
4707 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004708 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4709 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4710 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004711 {
4712 offset = arguments->back();
4713 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004714 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004715 {
4716 // A bias parameter might follow the offset parameter.
4717 ASSERT(arguments->size() >= 3);
4718 offset = (*arguments)[2];
4719 }
4720 if (offset != nullptr)
4721 {
4722 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4723 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4724 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004725 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004726 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004727 }
4728 else
4729 {
4730 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4731 size_t size = offsetConstantUnion->getType().getObjectSize();
4732 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4733 for (size_t i = 0u; i < size; ++i)
4734 {
4735 int offsetValue = values[i].getIConst();
4736 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4737 {
4738 std::stringstream tokenStream;
4739 tokenStream << offsetValue;
4740 std::string token = tokenStream.str();
4741 error(offset->getLine(), "Texture offset value out of valid range",
4742 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004743 }
4744 }
4745 }
4746 }
4747}
4748
Martin Radev2cc85b32016-08-05 16:22:53 +03004749// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4750void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4751{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004752 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004753 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4754
4755 if (name.compare(0, 5, "image") == 0)
4756 {
4757 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004758 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004759
Olli Etuaho485eefd2017-02-14 17:40:06 +00004760 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004761
4762 if (name.compare(5, 5, "Store") == 0)
4763 {
4764 if (memoryQualifier.readonly)
4765 {
4766 error(imageNode->getLine(),
4767 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004768 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004769 }
4770 }
4771 else if (name.compare(5, 4, "Load") == 0)
4772 {
4773 if (memoryQualifier.writeonly)
4774 {
4775 error(imageNode->getLine(),
4776 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004777 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004778 }
4779 }
4780 }
4781}
4782
4783// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4784void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4785 const TFunction *functionDefinition,
4786 const TIntermAggregate *functionCall)
4787{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004788 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004789
4790 const TIntermSequence &arguments = *functionCall->getSequence();
4791
4792 ASSERT(functionDefinition->getParamCount() == arguments.size());
4793
4794 for (size_t i = 0; i < arguments.size(); ++i)
4795 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004796 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4797 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004798 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4799 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4800
4801 if (IsImage(functionArgumentType.getBasicType()))
4802 {
4803 const TMemoryQualifier &functionArgumentMemoryQualifier =
4804 functionArgumentType.getMemoryQualifier();
4805 const TMemoryQualifier &functionParameterMemoryQualifier =
4806 functionParameterType.getMemoryQualifier();
4807 if (functionArgumentMemoryQualifier.readonly &&
4808 !functionParameterMemoryQualifier.readonly)
4809 {
4810 error(functionCall->getLine(),
4811 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004812 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004813 }
4814
4815 if (functionArgumentMemoryQualifier.writeonly &&
4816 !functionParameterMemoryQualifier.writeonly)
4817 {
4818 error(functionCall->getLine(),
4819 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004820 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004821 }
Martin Radev049edfa2016-11-11 14:35:37 +02004822
4823 if (functionArgumentMemoryQualifier.coherent &&
4824 !functionParameterMemoryQualifier.coherent)
4825 {
4826 error(functionCall->getLine(),
4827 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004828 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004829 }
4830
4831 if (functionArgumentMemoryQualifier.volatileQualifier &&
4832 !functionParameterMemoryQualifier.volatileQualifier)
4833 {
4834 error(functionCall->getLine(),
4835 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004836 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004837 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004838 }
4839 }
4840}
4841
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004842TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004843{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004844 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004845}
4846
4847TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004848 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004849 TIntermNode *thisNode,
4850 const TSourceLoc &loc)
4851{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004852 if (thisNode != nullptr)
4853 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004854 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004855 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004856
4857 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004858 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004859 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004860 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004861 }
4862 else
4863 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004864 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004865 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4866 }
4867}
4868
4869TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4870 TIntermSequence *arguments,
4871 TIntermNode *thisNode,
4872 const TSourceLoc &loc)
4873{
4874 TConstantUnion *unionArray = new TConstantUnion[1];
4875 int arraySize = 0;
4876 TIntermTyped *typedThis = thisNode->getAsTyped();
4877 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4878 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4879 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4880 // So accessing fnCall->getName() below is safe.
4881 if (fnCall->getName() != "length")
4882 {
4883 error(loc, "invalid method", fnCall->getName().c_str());
4884 }
4885 else if (!arguments->empty())
4886 {
4887 error(loc, "method takes no parameters", "length");
4888 }
4889 else if (typedThis == nullptr || !typedThis->isArray())
4890 {
4891 error(loc, "length can only be called on arrays", "length");
4892 }
4893 else
4894 {
4895 arraySize = typedThis->getArraySize();
4896 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004897 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004898 // This code path can be hit with expressions like these:
4899 // (a = b).length()
4900 // (func()).length()
4901 // (int[3](0, 1, 2)).length()
4902 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4903 // expression.
4904 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4905 // spec section 5.9 which allows "An array, vector or matrix expression with the
4906 // length method applied".
4907 error(loc, "length can only be called on array names, not on array expressions",
4908 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004909 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004910 }
4911 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03004912 TIntermConstantUnion *node =
4913 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
4914 node->setLine(loc);
4915 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004916}
4917
4918TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4919 TIntermSequence *arguments,
4920 const TSourceLoc &loc)
4921{
4922 // First find by unmangled name to check whether the function name has been
4923 // hidden by a variable name or struct typename.
4924 // If a function is found, check for one with a matching argument list.
4925 bool builtIn;
4926 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4927 if (symbol != nullptr && !symbol->isFunction())
4928 {
4929 error(loc, "function name expected", fnCall->getName().c_str());
4930 }
4931 else
4932 {
4933 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4934 mShaderVersion, &builtIn);
4935 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004936 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004937 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4938 }
4939 else
4940 {
4941 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004942 //
4943 // A declared function.
4944 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004945 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004946 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004947 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004948 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004949 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004950 if (builtIn && op != EOpNull)
4951 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004952 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004953 if (fnCandidate->getParamCount() == 1)
4954 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004955 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004956 TIntermNode *unaryParamNode = arguments->front();
4957 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004958 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004959 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004960 }
4961 else
4962 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004963 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004964 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004965 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004966
4967 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004968 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304969
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004970 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05304971 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004972 // See if we can constant fold a built-in. Note that this may be possible
4973 // even if it is not const-qualified.
4974 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304975 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004976 else
4977 {
4978 return callNode;
4979 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004980 }
4981 }
4982 else
4983 {
4984 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004985 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004986
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004987 // If builtIn == false, the function is user defined - could be an overloaded
4988 // built-in as well.
4989 // if builtIn == true, it's a builtIn function with no op associated with it.
4990 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004991 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004992 {
Olli Etuahofe486322017-03-21 09:30:54 +00004993 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004994 checkTextureOffsetConst(callNode);
4995 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004996 }
4997 else
4998 {
Olli Etuahofe486322017-03-21 09:30:54 +00004999 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005000 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005001 }
5002
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005003 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005004
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005005 callNode->setLine(loc);
5006
5007 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005008 }
5009 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005010 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005011
5012 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005013 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005014}
5015
Jamie Madillb98c3a82015-07-23 14:26:04 -04005016TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005017 TIntermTyped *trueExpression,
5018 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005019 const TSourceLoc &loc)
5020{
Olli Etuaho56229f12017-07-10 14:16:33 +03005021 if (!checkIsScalarBool(loc, cond))
5022 {
5023 return falseExpression;
5024 }
Olli Etuaho52901742015-04-15 13:42:45 +03005025
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005026 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005027 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005028 std::stringstream reasonStream;
5029 reasonStream << "mismatching ternary operator operand types '"
5030 << trueExpression->getCompleteString() << " and '"
5031 << falseExpression->getCompleteString() << "'";
5032 std::string reason = reasonStream.str();
5033 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005034 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005035 }
Olli Etuahode318b22016-10-25 16:18:25 +01005036 if (IsOpaqueType(trueExpression->getBasicType()))
5037 {
5038 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005039 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005040 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5041 // Note that structs containing opaque types don't need to be checked as structs are
5042 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005043 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005044 return falseExpression;
5045 }
5046
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005047 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005048 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005049 // ESSL 3.00.6 section 5.7:
5050 // Ternary operator support is optional for arrays. No certainty that it works across all
5051 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5052 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005053 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005054 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005055 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005056 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005057 }
Olli Etuaho94050052017-05-08 14:17:44 +03005058 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5059 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005060 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005061 return falseExpression;
5062 }
5063
Corentin Wallez0d959252016-07-12 17:26:32 -04005064 // WebGL2 section 5.26, the following results in an error:
5065 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005066 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005067 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005068 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005069 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005070 }
5071
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005072 // Note that the node resulting from here can be a constant union without being qualified as
5073 // constant.
5074 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5075 node->setLine(loc);
5076
5077 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005078}
Olli Etuaho49300862015-02-20 14:54:49 +02005079
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005080//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005081// Parse an array of strings using yyparse.
5082//
5083// Returns 0 for success.
5084//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005085int PaParseStrings(size_t count,
5086 const char *const string[],
5087 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305088 TParseContext *context)
5089{
Yunchao He4f285442017-04-21 12:15:49 +08005090 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005091 return 1;
5092
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005093 if (glslang_initialize(context))
5094 return 1;
5095
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005096 int error = glslang_scan(count, string, length, context);
5097 if (!error)
5098 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005099
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005100 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005101
alokp@chromium.org6b495712012-06-29 00:06:58 +00005102 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005103}
Jamie Madill45bcc782016-11-07 13:58:48 -05005104
5105} // namespace sh