blob: 80889b3c7afd1bad368ca52bae36984f9406704a [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 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002484 {
2485 TIntermSymbol *symbol =
2486 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2487 symbol->setLine(identifierLocation);
2488 declarationOut->appendDeclarator(symbol);
2489 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002490 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002491}
2492
Olli Etuaho13389b62016-10-16 11:48:18 +01002493void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2494 const TSourceLoc &identifierLocation,
2495 const TString &identifier,
2496 const TSourceLoc &initLocation,
2497 TIntermTyped *initializer,
2498 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002499{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002500 // If the declaration starting this declarator list was empty (example: int,), some checks were
2501 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002502 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002503 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002504 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2505 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002506 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002507
Olli Etuaho856c4972016-08-08 11:38:39 +03002508 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002511 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002512 {
2513 //
2514 // build the intermediate representation
2515 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002516 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002517 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002518 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002519 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002520 }
2521}
2522
Olli Etuaho13389b62016-10-16 11:48:18 +01002523void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2524 const TSourceLoc &identifierLocation,
2525 const TString &identifier,
2526 const TSourceLoc &indexLocation,
2527 TIntermTyped *indexExpression,
2528 const TSourceLoc &initLocation,
2529 TIntermTyped *initializer,
2530 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002531{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 // If the declaration starting this declarator list was empty (example: int,), some checks were
2533 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002535 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2537 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002538 }
2539
Olli Etuaho856c4972016-08-08 11:38:39 +03002540 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002541
Olli Etuaho8a176262016-08-16 14:23:01 +03002542 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002543
2544 TPublicType arrayType(publicType);
2545
Olli Etuaho856c4972016-08-08 11:38:39 +03002546 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2548 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002549 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002550 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002551 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 }
2553 // Make the type an array even if size check failed.
2554 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2555 arrayType.setArraySize(size);
2556
2557 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002558 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002559 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560 {
2561 if (initNode)
2562 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002563 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002564 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002565 }
2566}
2567
jchen104cdac9e2017-05-08 11:01:20 +08002568void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2569 const TSourceLoc &location)
2570{
2571 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2572 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2573 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2574 {
2575 error(location, "Requires both binding and offset", "layout");
2576 return;
2577 }
2578 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2579}
2580
Olli Etuahocce89652017-06-19 16:04:09 +03002581void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2582 const TPublicType &type,
2583 const TSourceLoc &loc)
2584{
2585 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2586 !getFragmentPrecisionHigh())
2587 {
2588 error(loc, "precision is not supported in fragment shader", "highp");
2589 }
2590
2591 if (!CanSetDefaultPrecisionOnType(type))
2592 {
2593 error(loc, "illegal type argument for default precision qualifier",
2594 getBasicString(type.getBasicType()));
2595 return;
2596 }
2597 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2598}
2599
Martin Radev70866b82016-07-22 15:27:42 +03002600void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002601{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002602 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002603 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002604
Martin Radev70866b82016-07-22 15:27:42 +03002605 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2606 typeQualifier.line);
2607
Jamie Madillc2128ff2016-07-04 10:26:17 -04002608 // It should never be the case, but some strange parser errors can send us here.
2609 if (layoutQualifier.isEmpty())
2610 {
2611 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002612 return;
2613 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002614
Martin Radev802abe02016-08-04 17:48:32 +03002615 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002616 {
Olli Etuaho43364892017-02-13 16:00:12 +00002617 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002618 return;
2619 }
2620
Olli Etuaho43364892017-02-13 16:00:12 +00002621 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2622
2623 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002624
2625 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2626
Andrei Volykhina5527072017-03-22 16:46:30 +03002627 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2628
jchen104cdac9e2017-05-08 11:01:20 +08002629 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2630
Martin Radev802abe02016-08-04 17:48:32 +03002631 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002632 {
Martin Radev802abe02016-08-04 17:48:32 +03002633 if (mComputeShaderLocalSizeDeclared &&
2634 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2635 {
2636 error(typeQualifier.line, "Work group size does not match the previous declaration",
2637 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002638 return;
2639 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002640
Martin Radev802abe02016-08-04 17:48:32 +03002641 if (mShaderVersion < 310)
2642 {
2643 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002644 return;
2645 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002646
Martin Radev4c4c8e72016-08-04 12:25:34 +03002647 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002648 {
2649 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002650 return;
2651 }
2652
2653 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2654 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2655
2656 const TConstantUnion *maxComputeWorkGroupSizeData =
2657 maxComputeWorkGroupSize->getConstPointer();
2658
2659 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2660 {
2661 if (layoutQualifier.localSize[i] != -1)
2662 {
2663 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2664 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2665 if (mComputeShaderLocalSize[i] < 1 ||
2666 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2667 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002668 std::stringstream reasonStream;
2669 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2670 << maxComputeWorkGroupSizeValue;
2671 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002672
Olli Etuaho4de340a2016-12-16 09:32:03 +00002673 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002674 return;
2675 }
2676 }
2677 }
2678
2679 mComputeShaderLocalSizeDeclared = true;
2680 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002681 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002682 {
2683 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2684 // specification.
2685 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2686 {
2687 error(typeQualifier.line, "Number of views does not match the previous declaration",
2688 "layout");
2689 return;
2690 }
2691
2692 if (layoutQualifier.numViews == -1)
2693 {
2694 error(typeQualifier.line, "No num_views specified", "layout");
2695 return;
2696 }
2697
2698 if (layoutQualifier.numViews > mMaxNumViews)
2699 {
2700 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2701 "layout");
2702 return;
2703 }
2704
2705 mNumViews = layoutQualifier.numViews;
2706 }
Martin Radev802abe02016-08-04 17:48:32 +03002707 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002708 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002709 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002710 {
Martin Radev802abe02016-08-04 17:48:32 +03002711 return;
2712 }
2713
2714 if (typeQualifier.qualifier != EvqUniform)
2715 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002716 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2717 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002718 return;
2719 }
2720
2721 if (mShaderVersion < 300)
2722 {
2723 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2724 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002725 return;
2726 }
2727
Olli Etuaho09b04a22016-12-15 13:30:26 +00002728 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002729
2730 if (layoutQualifier.matrixPacking != EmpUnspecified)
2731 {
2732 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2733 }
2734
2735 if (layoutQualifier.blockStorage != EbsUnspecified)
2736 {
2737 mDefaultBlockStorage = layoutQualifier.blockStorage;
2738 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002739 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002740}
2741
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002742TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2743 const TFunction &function,
2744 const TSourceLoc &location,
2745 bool insertParametersToSymbolTable)
2746{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002747 checkIsNotReserved(location, function.getName());
2748
Olli Etuahofe486322017-03-21 09:30:54 +00002749 TIntermFunctionPrototype *prototype =
2750 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002751 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2752 // point to the data that already exists in the symbol table.
2753 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2754 prototype->setLine(location);
2755
2756 for (size_t i = 0; i < function.getParamCount(); i++)
2757 {
2758 const TConstParameter &param = function.getParam(i);
2759
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002760 TIntermSymbol *symbol = nullptr;
2761
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002762 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2763 // be used for unused args).
2764 if (param.name != nullptr)
2765 {
2766 TVariable *variable = new TVariable(param.name, *param.type);
2767
2768 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002769 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002770 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002771 if (symbolTable.declare(variable))
2772 {
2773 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2774 variable->getType());
2775 }
2776 else
2777 {
2778 error(location, "redefinition", variable->getName().c_str());
2779 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002780 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002781 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002782 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002783 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002784 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2785 // symbol.
2786 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002787 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002788 symbol->setLine(location);
2789 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002790 }
2791 return prototype;
2792}
2793
Olli Etuaho16c745a2017-01-16 17:02:27 +00002794TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2795 const TFunction &parsedFunction,
2796 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002797{
Olli Etuaho476197f2016-10-11 13:59:08 +01002798 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2799 // first declaration. Either way the instance in the symbol table is used to track whether the
2800 // function is declared multiple times.
2801 TFunction *function = static_cast<TFunction *>(
2802 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2803 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002804 {
2805 // ESSL 1.00.17 section 4.2.7.
2806 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2807 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002808 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002809 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002810
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002811 TIntermFunctionPrototype *prototype =
2812 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002813
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002814 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002815
2816 if (!symbolTable.atGlobalLevel())
2817 {
2818 // ESSL 3.00.4 section 4.2.4.
2819 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002820 }
2821
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002822 return prototype;
2823}
2824
Olli Etuaho336b1472016-10-05 16:37:55 +01002825TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002826 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002827 TIntermBlock *functionBody,
2828 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002829{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002830 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002831 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2832 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002833 error(location, "function does not return a value:",
2834 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002835 }
2836
Olli Etuahof51fdd22016-10-03 10:03:40 +01002837 if (functionBody == nullptr)
2838 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002839 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002840 functionBody->setLine(location);
2841 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002842 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002843 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002844 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002845
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002846 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002847 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002848}
2849
Olli Etuaho476197f2016-10-11 13:59:08 +01002850void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2851 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002852 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002853{
Olli Etuaho476197f2016-10-11 13:59:08 +01002854 ASSERT(function);
2855 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002856 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002857 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002858
2859 if (builtIn)
2860 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002861 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002862 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002863 else
Jamie Madill185fb402015-06-12 15:48:48 -04002864 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002865 TFunction *prevDec = static_cast<TFunction *>(
2866 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2867
2868 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2869 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2870 // occurance.
2871 if (*function != prevDec)
2872 {
2873 // Swap the parameters of the previous declaration to the parameters of the function
2874 // definition (parameter names may differ).
2875 prevDec->swapParameters(**function);
2876
2877 // The function definition will share the same symbol as any previous declaration.
2878 *function = prevDec;
2879 }
2880
2881 if ((*function)->isDefined())
2882 {
2883 error(location, "function already has a body", (*function)->getName().c_str());
2884 }
2885
2886 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002887 }
Jamie Madill185fb402015-06-12 15:48:48 -04002888
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002889 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002890 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002891 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002892
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002893 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002894 setLoopNestingLevel(0);
2895}
2896
Jamie Madillb98c3a82015-07-23 14:26:04 -04002897TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002898{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002899 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002900 // We don't know at this point whether this is a function definition or a prototype.
2901 // The definition production code will check for redefinitions.
2902 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002903 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002904 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2905 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002906 //
2907 TFunction *prevDec =
2908 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302909
Martin Radevda6254b2016-12-14 17:00:36 +02002910 if (getShaderVersion() >= 300 &&
2911 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2912 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302913 {
Martin Radevda6254b2016-12-14 17:00:36 +02002914 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302915 // Therefore overloading or redefining builtin functions is an error.
2916 error(location, "Name of a built-in function cannot be redeclared as function",
2917 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302918 }
2919 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002920 {
2921 if (prevDec->getReturnType() != function->getReturnType())
2922 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002923 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002924 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002925 }
2926 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2927 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002928 if (prevDec->getParam(i).type->getQualifier() !=
2929 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002930 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002931 error(location,
2932 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002933 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002934 }
2935 }
2936 }
2937
2938 //
2939 // Check for previously declared variables using the same name.
2940 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002941 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002942 if (prevSym)
2943 {
2944 if (!prevSym->isFunction())
2945 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002946 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002947 }
2948 }
2949 else
2950 {
2951 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002952 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002953 }
2954
2955 // We're at the inner scope level of the function's arguments and body statement.
2956 // Add the function prototype to the surrounding scope instead.
2957 symbolTable.getOuterLevel()->insert(function);
2958
Olli Etuaho78d13742017-01-18 13:06:10 +00002959 // Raise error message if main function takes any parameters or return anything other than void
2960 if (function->getName() == "main")
2961 {
2962 if (function->getParamCount() > 0)
2963 {
2964 error(location, "function cannot take any parameter(s)", "main");
2965 }
2966 if (function->getReturnType().getBasicType() != EbtVoid)
2967 {
2968 error(location, "main function cannot return a value",
2969 function->getReturnType().getBasicString());
2970 }
2971 }
2972
Jamie Madill185fb402015-06-12 15:48:48 -04002973 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002974 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2975 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002976 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2977 //
2978 return function;
2979}
2980
Olli Etuaho9de84a52016-06-14 17:36:01 +03002981TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2982 const TString *name,
2983 const TSourceLoc &location)
2984{
2985 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2986 {
2987 error(location, "no qualifiers allowed for function return",
2988 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002989 }
2990 if (!type.layoutQualifier.isEmpty())
2991 {
2992 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002993 }
jchen10cc2a10e2017-05-03 14:05:12 +08002994 // make sure an opaque type is not involved as well...
2995 std::string reason(getBasicString(type.getBasicType()));
2996 reason += "s can't be function return values";
2997 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002998 if (mShaderVersion < 300)
2999 {
3000 // Array return values are forbidden, but there's also no valid syntax for declaring array
3001 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003002 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003003
3004 if (type.isStructureContainingArrays())
3005 {
3006 // ESSL 1.00.17 section 6.1 Function Definitions
3007 error(location, "structures containing arrays can't be function return values",
3008 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003009 }
3010 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003011
3012 // Add the function as a prototype after parsing it (we do not support recursion)
3013 return new TFunction(name, new TType(type));
3014}
3015
Olli Etuahocce89652017-06-19 16:04:09 +03003016TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3017{
Olli Etuahocce89652017-06-19 16:04:09 +03003018 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3019 return new TFunction(name, returnType);
3020}
3021
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003022TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003023{
Olli Etuahocce89652017-06-19 16:04:09 +03003024 if (mShaderVersion < 300 && publicType.array)
3025 {
3026 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3027 "[]");
3028 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003029 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003030 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003031 error(publicType.getLine(), "constructor can't be a structure definition",
3032 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003033 }
3034
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003035 TType *type = new TType(publicType);
3036 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003037 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003038 error(publicType.getLine(), "cannot construct this type",
3039 getBasicString(publicType.getBasicType()));
3040 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003041 }
3042
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003043 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003044}
3045
Olli Etuahocce89652017-06-19 16:04:09 +03003046TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3047 const TString *name,
3048 const TSourceLoc &nameLoc)
3049{
3050 if (publicType.getBasicType() == EbtVoid)
3051 {
3052 error(nameLoc, "illegal use of type 'void'", name->c_str());
3053 }
3054 checkIsNotReserved(nameLoc, *name);
3055 TType *type = new TType(publicType);
3056 TParameter param = {name, type};
3057 return param;
3058}
3059
3060TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3061 const TSourceLoc &identifierLoc,
3062 TIntermTyped *arraySize,
3063 const TSourceLoc &arrayLoc,
3064 TPublicType *type)
3065{
3066 checkIsValidTypeForArray(arrayLoc, *type);
3067 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3068 type->setArraySize(size);
3069 return parseParameterDeclarator(*type, identifier, identifierLoc);
3070}
3071
Jamie Madillb98c3a82015-07-23 14:26:04 -04003072// This function is used to test for the correctness of the parameters passed to various constructor
3073// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003074//
Olli Etuaho856c4972016-08-08 11:38:39 +03003075// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003076//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003077TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003078 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303079 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080{
Olli Etuaho856c4972016-08-08 11:38:39 +03003081 if (type.isUnsizedArray())
3082 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003083 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003084 {
3085 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3086 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003087 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003088 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003089 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003090 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003091
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003092 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003093 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003094 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003095 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003096
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003097 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003098 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003099
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003100 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3101 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003102 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003103 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003104 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003105 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106}
3107
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003108//
3109// Interface/uniform blocks
3110//
Olli Etuaho13389b62016-10-16 11:48:18 +01003111TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003112 const TTypeQualifierBuilder &typeQualifierBuilder,
3113 const TSourceLoc &nameLine,
3114 const TString &blockName,
3115 TFieldList *fieldList,
3116 const TString *instanceName,
3117 const TSourceLoc &instanceLine,
3118 TIntermTyped *arrayIndex,
3119 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003120{
Olli Etuaho856c4972016-08-08 11:38:39 +03003121 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003122
Olli Etuaho77ba4082016-12-16 12:01:18 +00003123 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003124
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003125 if (typeQualifier.qualifier != EvqUniform)
3126 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003127 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3128 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003129 }
3130
Martin Radev70866b82016-07-22 15:27:42 +03003131 if (typeQualifier.invariant)
3132 {
3133 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3134 }
3135
Olli Etuaho43364892017-02-13 16:00:12 +00003136 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3137
jchen10af713a22017-04-19 09:10:56 +08003138 // add array index
3139 unsigned int arraySize = 0;
3140 if (arrayIndex != nullptr)
3141 {
3142 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3143 }
3144
3145 if (mShaderVersion < 310)
3146 {
3147 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3148 }
3149 else
3150 {
3151 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3152 arraySize);
3153 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003154
Andrei Volykhina5527072017-03-22 16:46:30 +03003155 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3156
Jamie Madill099c0f32013-06-20 11:55:52 -04003157 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003158 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003159
Jamie Madill099c0f32013-06-20 11:55:52 -04003160 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3161 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003162 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003163 }
3164
Jamie Madill1566ef72013-06-20 11:55:54 -04003165 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3166 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003167 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003168 }
3169
Olli Etuaho856c4972016-08-08 11:38:39 +03003170 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003171
Martin Radev2cc85b32016-08-05 16:22:53 +03003172 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3173
Arun Patole7e7e68d2015-05-22 12:02:25 +05303174 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3175 if (!symbolTable.declare(blockNameSymbol))
3176 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003177 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003178 }
3179
Jamie Madill98493dd2013-07-08 14:39:03 -04003180 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303181 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3182 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003183 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303184 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003185 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 {
jchen10cc2a10e2017-05-03 14:05:12 +08003187 std::string reason("unsupported type - ");
3188 reason += fieldType->getBasicString();
3189 reason += " types are not allowed in interface blocks";
3190 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003191 }
3192
Jamie Madill98493dd2013-07-08 14:39:03 -04003193 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003194 switch (qualifier)
3195 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003196 case EvqGlobal:
3197 case EvqUniform:
3198 break;
3199 default:
3200 error(field->line(), "invalid qualifier on interface block member",
3201 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003202 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003203 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003204
Martin Radev70866b82016-07-22 15:27:42 +03003205 if (fieldType->isInvariant())
3206 {
3207 error(field->line(), "invalid qualifier on interface block member", "invariant");
3208 }
3209
Jamie Madilla5efff92013-06-06 11:56:47 -04003210 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003212 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003213 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003214
Jamie Madill98493dd2013-07-08 14:39:03 -04003215 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003216 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003217 error(field->line(), "invalid layout qualifier: cannot be used here",
3218 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003219 }
3220
Jamie Madill98493dd2013-07-08 14:39:03 -04003221 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003222 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003224 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003225 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003226 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003227 warning(field->line(),
3228 "extraneous layout qualifier: only has an effect on matrix types",
3229 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003230 }
3231
Jamie Madill98493dd2013-07-08 14:39:03 -04003232 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003233 }
3234
Jamie Madillb98c3a82015-07-23 14:26:04 -04003235 TInterfaceBlock *interfaceBlock =
3236 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3237 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3238 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003239
3240 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003241 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003242
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003244 {
3245 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003246 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3247 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003248 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303249 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003250
3251 // set parent pointer of the field variable
3252 fieldType->setInterfaceBlock(interfaceBlock);
3253
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003255 fieldVariable->setQualifier(typeQualifier.qualifier);
3256
Arun Patole7e7e68d2015-05-22 12:02:25 +05303257 if (!symbolTable.declare(fieldVariable))
3258 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003259 error(field->line(), "redefinition of an interface block member name",
3260 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003261 }
3262 }
3263 }
3264 else
3265 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003266 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003267
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003268 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303269 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003270 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003271
Arun Patole7e7e68d2015-05-22 12:02:25 +05303272 if (!symbolTable.declare(instanceTypeDef))
3273 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003274 error(instanceLine, "redefinition of an interface block instance name",
3275 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003276 }
3277
Jamie Madillb98c3a82015-07-23 14:26:04 -04003278 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003279 symbolName = instanceTypeDef->getName();
3280 }
3281
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003282 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3283 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003284 TIntermDeclaration *declaration = new TIntermDeclaration();
3285 declaration->appendDeclarator(blockSymbol);
3286 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003287
3288 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003289 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003290}
3291
Olli Etuaho383b7912016-08-05 11:22:59 +03003292void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003293{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003294 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003295
3296 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003297 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303298 if (mStructNestingLevel > 1)
3299 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003300 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003301 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003302}
3303
3304void TParseContext::exitStructDeclaration()
3305{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003306 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003307}
3308
Olli Etuaho8a176262016-08-16 14:23:01 +03003309void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003310{
Jamie Madillacb4b812016-11-07 13:50:29 -05003311 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303312 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003313 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003314 }
3315
Arun Patole7e7e68d2015-05-22 12:02:25 +05303316 if (field.type()->getBasicType() != EbtStruct)
3317 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003318 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003319 }
3320
3321 // We're already inside a structure definition at this point, so add
3322 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303323 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3324 {
Jamie Madill41a49272014-03-18 16:10:13 -04003325 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003326 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3327 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003328 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003329 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003330 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003331 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003332}
3333
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003334//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003335// Parse an array index expression
3336//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003337TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3338 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303339 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003340{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003341 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3342 {
3343 if (baseExpression->getAsSymbolNode())
3344 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303345 error(location, " left of '[' is not of type array, matrix, or vector ",
3346 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003347 }
3348 else
3349 {
3350 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3351 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003352
Olli Etuaho3ec75682017-07-05 17:02:55 +03003353 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003354 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003355
Jamie Madill21c1e452014-12-29 11:33:41 -05003356 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3357
Olli Etuaho36b05142015-11-12 13:10:42 +02003358 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3359 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3360 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3361 // index is a constant expression.
3362 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3363 {
3364 if (baseExpression->isInterfaceBlock())
3365 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003366 error(location,
3367 "array indexes for interface blocks arrays must be constant integral expressions",
3368 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003369 }
3370 else if (baseExpression->getQualifier() == EvqFragmentOut)
3371 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003372 error(location,
3373 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003374 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003375 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3376 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003377 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003378 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003379 }
3380
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003381 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003382 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003383 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3384 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3385 // constant fold expressions that are not constant expressions). The most compatible way to
3386 // handle this case is to report a warning instead of an error and force the index to be in
3387 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003388 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003389 int index = 0;
3390 if (indexConstantUnion->getBasicType() == EbtInt)
3391 {
3392 index = indexConstantUnion->getIConst(0);
3393 }
3394 else if (indexConstantUnion->getBasicType() == EbtUInt)
3395 {
3396 index = static_cast<int>(indexConstantUnion->getUConst(0));
3397 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003398
3399 int safeIndex = -1;
3400
3401 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003402 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003403 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003404 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003405 if (mShaderSpec == SH_WEBGL2_SPEC)
3406 {
3407 // Error has been already generated if index is not const.
3408 if (indexExpression->getQualifier() == EvqConst)
3409 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003410 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003411 }
3412 safeIndex = 0;
3413 }
3414 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3415 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003416 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003417 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003418 "GL_EXT_draw_buffers is disabled",
3419 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003420 safeIndex = 0;
3421 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003422 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003423 // Only do generic out-of-range check if similar error hasn't already been reported.
3424 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003425 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003426 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3427 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003428 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003429 }
3430 }
3431 else if (baseExpression->isMatrix())
3432 {
3433 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003434 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003435 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003436 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003437 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003438 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003439 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3440 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003441 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003442 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003443
3444 ASSERT(safeIndex >= 0);
3445 // Data of constant unions can't be changed, because it may be shared with other
3446 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3447 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003448 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003450 TConstantUnion *safeConstantUnion = new TConstantUnion();
3451 safeConstantUnion->setIConst(safeIndex);
3452 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003453 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003454 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003455
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003456 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3457 node->setLine(location);
3458 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003459 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003460 else
3461 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003462 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3463 node->setLine(location);
3464 // Indirect indexing can never be constant folded.
3465 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003466 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467}
3468
Olli Etuaho90892fb2016-07-14 14:44:51 +03003469int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3470 const TSourceLoc &location,
3471 int index,
3472 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003473 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003474{
3475 if (index >= arraySize || index < 0)
3476 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003477 std::stringstream reasonStream;
3478 reasonStream << reason << " '" << index << "'";
3479 std::string token = reasonStream.str();
3480 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003481 if (index < 0)
3482 {
3483 return 0;
3484 }
3485 else
3486 {
3487 return arraySize - 1;
3488 }
3489 }
3490 return index;
3491}
3492
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3494 const TSourceLoc &dotLocation,
3495 const TString &fieldString,
3496 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003497{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498 if (baseExpression->isArray())
3499 {
3500 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003501 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003502 }
3503
3504 if (baseExpression->isVector())
3505 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003506 TVector<int> fieldOffsets;
3507 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3508 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003509 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003510 fieldOffsets.resize(1);
3511 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003512 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003513 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3514 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003515
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003516 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003517 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 else if (baseExpression->getBasicType() == EbtStruct)
3519 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303520 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003521 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003522 {
3523 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003524 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003525 }
3526 else
3527 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003528 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003529 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003531 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003532 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003533 {
3534 fieldFound = true;
3535 break;
3536 }
3537 }
3538 if (fieldFound)
3539 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003540 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003541 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003542 TIntermBinary *node =
3543 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3544 node->setLine(dotLocation);
3545 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003546 }
3547 else
3548 {
3549 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003550 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003551 }
3552 }
3553 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003554 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003555 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303556 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003557 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003558 {
3559 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003560 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003561 }
3562 else
3563 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003564 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003565 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003566 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003567 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003568 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003569 {
3570 fieldFound = true;
3571 break;
3572 }
3573 }
3574 if (fieldFound)
3575 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003576 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003577 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003578 TIntermBinary *node =
3579 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3580 node->setLine(dotLocation);
3581 // Indexing interface blocks can never be constant folded.
3582 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003583 }
3584 else
3585 {
3586 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003587 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003588 }
3589 }
3590 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003591 else
3592 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003593 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003594 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003595 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303596 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003597 }
3598 else
3599 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303600 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003601 " field selection requires structure, vector, or interface block on left hand "
3602 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303603 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003604 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003605 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003606 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003607}
3608
Jamie Madillb98c3a82015-07-23 14:26:04 -04003609TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3610 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003611{
Martin Radev802abe02016-08-04 17:48:32 +03003612 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003613
3614 if (qualifierType == "shared")
3615 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003616 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003617 {
3618 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3619 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003620 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003621 }
3622 else if (qualifierType == "packed")
3623 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003624 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003625 {
3626 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3627 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003628 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003629 }
3630 else if (qualifierType == "std140")
3631 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003632 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003633 }
3634 else if (qualifierType == "row_major")
3635 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003636 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003637 }
3638 else if (qualifierType == "column_major")
3639 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003640 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003641 }
3642 else if (qualifierType == "location")
3643 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003644 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3645 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003646 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003647 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3648 mShaderType == GL_FRAGMENT_SHADER)
3649 {
3650 qualifier.yuv = true;
3651 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003652 else if (qualifierType == "rgba32f")
3653 {
3654 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3655 qualifier.imageInternalFormat = EiifRGBA32F;
3656 }
3657 else if (qualifierType == "rgba16f")
3658 {
3659 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3660 qualifier.imageInternalFormat = EiifRGBA16F;
3661 }
3662 else if (qualifierType == "r32f")
3663 {
3664 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3665 qualifier.imageInternalFormat = EiifR32F;
3666 }
3667 else if (qualifierType == "rgba8")
3668 {
3669 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3670 qualifier.imageInternalFormat = EiifRGBA8;
3671 }
3672 else if (qualifierType == "rgba8_snorm")
3673 {
3674 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3675 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3676 }
3677 else if (qualifierType == "rgba32i")
3678 {
3679 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3680 qualifier.imageInternalFormat = EiifRGBA32I;
3681 }
3682 else if (qualifierType == "rgba16i")
3683 {
3684 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3685 qualifier.imageInternalFormat = EiifRGBA16I;
3686 }
3687 else if (qualifierType == "rgba8i")
3688 {
3689 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3690 qualifier.imageInternalFormat = EiifRGBA8I;
3691 }
3692 else if (qualifierType == "r32i")
3693 {
3694 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3695 qualifier.imageInternalFormat = EiifR32I;
3696 }
3697 else if (qualifierType == "rgba32ui")
3698 {
3699 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3700 qualifier.imageInternalFormat = EiifRGBA32UI;
3701 }
3702 else if (qualifierType == "rgba16ui")
3703 {
3704 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3705 qualifier.imageInternalFormat = EiifRGBA16UI;
3706 }
3707 else if (qualifierType == "rgba8ui")
3708 {
3709 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3710 qualifier.imageInternalFormat = EiifRGBA8UI;
3711 }
3712 else if (qualifierType == "r32ui")
3713 {
3714 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3715 qualifier.imageInternalFormat = EiifR32UI;
3716 }
3717
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003718 else
3719 {
3720 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003721 }
3722
Jamie Madilla5efff92013-06-06 11:56:47 -04003723 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003724}
3725
Martin Radev802abe02016-08-04 17:48:32 +03003726void TParseContext::parseLocalSize(const TString &qualifierType,
3727 const TSourceLoc &qualifierTypeLine,
3728 int intValue,
3729 const TSourceLoc &intValueLine,
3730 const std::string &intValueString,
3731 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003732 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003733{
Olli Etuaho856c4972016-08-08 11:38:39 +03003734 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003735 if (intValue < 1)
3736 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003737 std::stringstream reasonStream;
3738 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3739 std::string reason = reasonStream.str();
3740 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003741 }
3742 (*localSize)[index] = intValue;
3743}
3744
Olli Etuaho09b04a22016-12-15 13:30:26 +00003745void TParseContext::parseNumViews(int intValue,
3746 const TSourceLoc &intValueLine,
3747 const std::string &intValueString,
3748 int *numViews)
3749{
3750 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3751 // specification.
3752 if (intValue < 1)
3753 {
3754 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3755 }
3756 *numViews = intValue;
3757}
3758
Jamie Madillb98c3a82015-07-23 14:26:04 -04003759TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3760 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003761 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303762 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003763{
Martin Radev802abe02016-08-04 17:48:32 +03003764 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003765
Martin Radev802abe02016-08-04 17:48:32 +03003766 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003767
Martin Radev802abe02016-08-04 17:48:32 +03003768 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003769 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003770 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003771 if (intValue < 0)
3772 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003773 error(intValueLine, "out of range: location must be non-negative",
3774 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003775 }
3776 else
3777 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003778 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003779 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003780 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003781 }
Olli Etuaho43364892017-02-13 16:00:12 +00003782 else if (qualifierType == "binding")
3783 {
3784 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3785 if (intValue < 0)
3786 {
3787 error(intValueLine, "out of range: binding must be non-negative",
3788 intValueString.c_str());
3789 }
3790 else
3791 {
3792 qualifier.binding = intValue;
3793 }
3794 }
jchen104cdac9e2017-05-08 11:01:20 +08003795 else if (qualifierType == "offset")
3796 {
3797 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3798 if (intValue < 0)
3799 {
3800 error(intValueLine, "out of range: offset must be non-negative",
3801 intValueString.c_str());
3802 }
3803 else
3804 {
3805 qualifier.offset = intValue;
3806 }
3807 }
Martin Radev802abe02016-08-04 17:48:32 +03003808 else if (qualifierType == "local_size_x")
3809 {
3810 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3811 &qualifier.localSize);
3812 }
3813 else if (qualifierType == "local_size_y")
3814 {
3815 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3816 &qualifier.localSize);
3817 }
3818 else if (qualifierType == "local_size_z")
3819 {
3820 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3821 &qualifier.localSize);
3822 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003823 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003824 mShaderType == GL_VERTEX_SHADER)
3825 {
3826 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3827 }
Martin Radev802abe02016-08-04 17:48:32 +03003828 else
3829 {
3830 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003831 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003832
Jamie Madilla5efff92013-06-06 11:56:47 -04003833 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003834}
3835
Olli Etuaho613b9592016-09-05 12:05:53 +03003836TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3837{
3838 return new TTypeQualifierBuilder(
3839 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3840 mShaderVersion);
3841}
3842
Olli Etuahocce89652017-06-19 16:04:09 +03003843TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3844 const TSourceLoc &loc)
3845{
3846 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3847 return new TStorageQualifierWrapper(qualifier, loc);
3848}
3849
3850TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3851{
3852 if (getShaderType() == GL_VERTEX_SHADER)
3853 {
3854 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3855 }
3856 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3857}
3858
3859TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3860{
3861 if (declaringFunction())
3862 {
3863 return new TStorageQualifierWrapper(EvqIn, loc);
3864 }
3865 if (getShaderType() == GL_FRAGMENT_SHADER)
3866 {
3867 if (mShaderVersion < 300)
3868 {
3869 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3870 }
3871 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3872 }
3873 if (getShaderType() == GL_VERTEX_SHADER)
3874 {
3875 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3876 {
3877 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3878 }
3879 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3880 }
3881 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3882}
3883
3884TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3885{
3886 if (declaringFunction())
3887 {
3888 return new TStorageQualifierWrapper(EvqOut, loc);
3889 }
3890 if (mShaderVersion < 300)
3891 {
3892 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3893 }
3894 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3895 {
3896 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3897 }
3898 if (getShaderType() == GL_VERTEX_SHADER)
3899 {
3900 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3901 }
3902 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3903}
3904
3905TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3906{
3907 if (!declaringFunction())
3908 {
3909 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3910 }
3911 return new TStorageQualifierWrapper(EvqInOut, loc);
3912}
3913
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003915 TLayoutQualifier rightQualifier,
3916 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003917{
Martin Radevc28888b2016-07-22 15:27:42 +03003918 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003919 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003920}
3921
Olli Etuahocce89652017-06-19 16:04:09 +03003922TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3923{
3924 checkIsNotReserved(loc, *identifier);
3925 TType *type = new TType(EbtVoid, EbpUndefined);
3926 return new TField(type, identifier, loc);
3927}
3928
3929TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3930 const TSourceLoc &loc,
3931 TIntermTyped *arraySize,
3932 const TSourceLoc &arraySizeLoc)
3933{
3934 checkIsNotReserved(loc, *identifier);
3935
3936 TType *type = new TType(EbtVoid, EbpUndefined);
3937 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3938 type->setArraySize(size);
3939
3940 return new TField(type, identifier, loc);
3941}
3942
Olli Etuaho4de340a2016-12-16 09:32:03 +00003943TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3944 const TFieldList *newlyAddedFields,
3945 const TSourceLoc &location)
3946{
3947 for (TField *field : *newlyAddedFields)
3948 {
3949 for (TField *oldField : *processedFields)
3950 {
3951 if (oldField->name() == field->name())
3952 {
3953 error(location, "duplicate field name in structure", field->name().c_str());
3954 }
3955 }
3956 processedFields->push_back(field);
3957 }
3958 return processedFields;
3959}
3960
Martin Radev70866b82016-07-22 15:27:42 +03003961TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3962 const TTypeQualifierBuilder &typeQualifierBuilder,
3963 TPublicType *typeSpecifier,
3964 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003965{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003966 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003967
Martin Radev70866b82016-07-22 15:27:42 +03003968 typeSpecifier->qualifier = typeQualifier.qualifier;
3969 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003970 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003971 typeSpecifier->invariant = typeQualifier.invariant;
3972 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303973 {
Martin Radev70866b82016-07-22 15:27:42 +03003974 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003975 }
Martin Radev70866b82016-07-22 15:27:42 +03003976 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003977}
3978
Jamie Madillb98c3a82015-07-23 14:26:04 -04003979TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3980 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003981{
Martin Radev4a9cd802016-09-01 16:51:51 +03003982 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3983 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003984
Martin Radev4a9cd802016-09-01 16:51:51 +03003985 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003986
Martin Radev4a9cd802016-09-01 16:51:51 +03003987 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003988
Arun Patole7e7e68d2015-05-22 12:02:25 +05303989 for (unsigned int i = 0; i < fieldList->size(); ++i)
3990 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003991 //
3992 // Careful not to replace already known aspects of type, like array-ness
3993 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303994 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003995 type->setBasicType(typeSpecifier.getBasicType());
3996 type->setPrimarySize(typeSpecifier.getPrimarySize());
3997 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003998 type->setPrecision(typeSpecifier.precision);
3999 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004000 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004001 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004002 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004003
4004 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304005 if (type->isArray())
4006 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004007 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004008 }
4009 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004010 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004011 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304012 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004013 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004014 }
4015
Martin Radev4a9cd802016-09-01 16:51:51 +03004016 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017 }
4018
Jamie Madill98493dd2013-07-08 14:39:03 -04004019 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004020}
4021
Martin Radev4a9cd802016-09-01 16:51:51 +03004022TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4023 const TSourceLoc &nameLine,
4024 const TString *structName,
4025 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004026{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304027 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004028 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004029
Jamie Madill9b820842015-02-12 10:40:10 -05004030 // Store a bool in the struct if we're at global scope, to allow us to
4031 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004032 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004033
Jamie Madill98493dd2013-07-08 14:39:03 -04004034 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004035 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004036 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304037 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4038 if (!symbolTable.declare(userTypeDef))
4039 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004040 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004041 }
4042 }
4043
4044 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004045 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004046 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004048 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004049 switch (qualifier)
4050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004051 case EvqGlobal:
4052 case EvqTemporary:
4053 break;
4054 default:
4055 error(field.line(), "invalid qualifier on struct member",
4056 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004057 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004058 }
Martin Radev70866b82016-07-22 15:27:42 +03004059 if (field.type()->isInvariant())
4060 {
4061 error(field.line(), "invalid qualifier on struct member", "invariant");
4062 }
jchen104cdac9e2017-05-08 11:01:20 +08004063 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4064 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004065 {
4066 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4067 }
4068
Olli Etuaho43364892017-02-13 16:00:12 +00004069 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4070
4071 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004072
4073 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004074 }
4075
Martin Radev4a9cd802016-09-01 16:51:51 +03004076 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004077 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004078 exitStructDeclaration();
4079
Martin Radev4a9cd802016-09-01 16:51:51 +03004080 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081}
4082
Jamie Madillb98c3a82015-07-23 14:26:04 -04004083TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004084 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004085 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004086{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004087 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004088 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004089 init->isVector())
4090 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004091 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4092 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004093 return nullptr;
4094 }
4095
Olli Etuahoac5274d2015-02-20 10:19:08 +02004096 if (statementList)
4097 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004098 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004099 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004100 return nullptr;
4101 }
4102 }
4103
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004104 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4105 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004106 return node;
4107}
4108
4109TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4110{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004111 if (mSwitchNestingLevel == 0)
4112 {
4113 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004114 return nullptr;
4115 }
4116 if (condition == nullptr)
4117 {
4118 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004119 return nullptr;
4120 }
4121 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004122 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004123 {
4124 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004125 }
4126 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004127 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4128 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4129 // fold in case labels.
4130 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004131 {
4132 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004133 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004134 TIntermCase *node = new TIntermCase(condition);
4135 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004136 return node;
4137}
4138
4139TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4140{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004141 if (mSwitchNestingLevel == 0)
4142 {
4143 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004144 return nullptr;
4145 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004146 TIntermCase *node = new TIntermCase(nullptr);
4147 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004148 return node;
4149}
4150
Jamie Madillb98c3a82015-07-23 14:26:04 -04004151TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4152 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004153 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004154{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004155 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004156
4157 switch (op)
4158 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004159 case EOpLogicalNot:
4160 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4161 child->isVector())
4162 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004163 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004164 return nullptr;
4165 }
4166 break;
4167 case EOpBitwiseNot:
4168 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4169 child->isMatrix() || child->isArray())
4170 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004171 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004172 return nullptr;
4173 }
4174 break;
4175 case EOpPostIncrement:
4176 case EOpPreIncrement:
4177 case EOpPostDecrement:
4178 case EOpPreDecrement:
4179 case EOpNegative:
4180 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004181 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4182 child->getBasicType() == EbtBool || child->isArray() ||
4183 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004184 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004185 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004186 return nullptr;
4187 }
4188 // Operators for built-ins are already type checked against their prototype.
4189 default:
4190 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004191 }
4192
Olli Etuahof119a262016-08-19 15:54:22 +03004193 TIntermUnary *node = new TIntermUnary(op, child);
4194 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004195
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004196 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004197}
4198
Olli Etuaho09b22472015-02-11 11:47:26 +02004199TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4200{
Olli Etuahocce89652017-06-19 16:04:09 +03004201 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004202 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004203 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004204 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004205 return child;
4206 }
4207 return node;
4208}
4209
Jamie Madillb98c3a82015-07-23 14:26:04 -04004210TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4211 TIntermTyped *child,
4212 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004213{
Olli Etuaho856c4972016-08-08 11:38:39 +03004214 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004215 return addUnaryMath(op, child, loc);
4216}
4217
Jamie Madillb98c3a82015-07-23 14:26:04 -04004218bool TParseContext::binaryOpCommonCheck(TOperator op,
4219 TIntermTyped *left,
4220 TIntermTyped *right,
4221 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004222{
jchen10b4cf5652017-05-05 18:51:17 +08004223 // Check opaque types are not allowed to be operands in expressions other than array indexing
4224 // and structure member selection.
4225 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4226 {
4227 switch (op)
4228 {
4229 case EOpIndexDirect:
4230 case EOpIndexIndirect:
4231 break;
4232 case EOpIndexDirectStruct:
4233 UNREACHABLE();
4234
4235 default:
4236 error(loc, "Invalid operation for variables with an opaque type",
4237 GetOperatorString(op));
4238 return false;
4239 }
4240 }
jchen10cc2a10e2017-05-03 14:05:12 +08004241
Olli Etuaho244be012016-08-18 15:26:02 +03004242 if (left->getType().getStruct() || right->getType().getStruct())
4243 {
4244 switch (op)
4245 {
4246 case EOpIndexDirectStruct:
4247 ASSERT(left->getType().getStruct());
4248 break;
4249 case EOpEqual:
4250 case EOpNotEqual:
4251 case EOpAssign:
4252 case EOpInitialize:
4253 if (left->getType() != right->getType())
4254 {
4255 return false;
4256 }
4257 break;
4258 default:
4259 error(loc, "Invalid operation for structs", GetOperatorString(op));
4260 return false;
4261 }
4262 }
4263
Olli Etuaho94050052017-05-08 14:17:44 +03004264 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4265 {
4266 switch (op)
4267 {
4268 case EOpIndexDirectInterfaceBlock:
4269 ASSERT(left->getType().getInterfaceBlock());
4270 break;
4271 default:
4272 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4273 return false;
4274 }
4275 }
4276
Olli Etuahod6b14282015-03-17 14:31:35 +02004277 if (left->isArray() || right->isArray())
4278 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004279 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004280 {
4281 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4282 return false;
4283 }
4284
4285 if (left->isArray() != right->isArray())
4286 {
4287 error(loc, "array / non-array mismatch", GetOperatorString(op));
4288 return false;
4289 }
4290
4291 switch (op)
4292 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004293 case EOpEqual:
4294 case EOpNotEqual:
4295 case EOpAssign:
4296 case EOpInitialize:
4297 break;
4298 default:
4299 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4300 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004301 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004302 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004303 if (left->getArraySize() != right->getArraySize())
4304 {
4305 error(loc, "array size mismatch", GetOperatorString(op));
4306 return false;
4307 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004308 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004309
4310 // Check ops which require integer / ivec parameters
4311 bool isBitShift = false;
4312 switch (op)
4313 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004314 case EOpBitShiftLeft:
4315 case EOpBitShiftRight:
4316 case EOpBitShiftLeftAssign:
4317 case EOpBitShiftRightAssign:
4318 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4319 // check that the basic type is an integer type.
4320 isBitShift = true;
4321 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4322 {
4323 return false;
4324 }
4325 break;
4326 case EOpBitwiseAnd:
4327 case EOpBitwiseXor:
4328 case EOpBitwiseOr:
4329 case EOpBitwiseAndAssign:
4330 case EOpBitwiseXorAssign:
4331 case EOpBitwiseOrAssign:
4332 // It is enough to check the type of only one operand, since later it
4333 // is checked that the operand types match.
4334 if (!IsInteger(left->getBasicType()))
4335 {
4336 return false;
4337 }
4338 break;
4339 default:
4340 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004341 }
4342
4343 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4344 // So the basic type should usually match.
4345 if (!isBitShift && left->getBasicType() != right->getBasicType())
4346 {
4347 return false;
4348 }
4349
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004350 // Check that:
4351 // 1. Type sizes match exactly on ops that require that.
4352 // 2. Restrictions for structs that contain arrays or samplers are respected.
4353 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004354 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004356 case EOpAssign:
4357 case EOpInitialize:
4358 case EOpEqual:
4359 case EOpNotEqual:
4360 // ESSL 1.00 sections 5.7, 5.8, 5.9
4361 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4362 {
4363 error(loc, "undefined operation for structs containing arrays",
4364 GetOperatorString(op));
4365 return false;
4366 }
4367 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4368 // we interpret the spec so that this extends to structs containing samplers,
4369 // similarly to ESSL 1.00 spec.
4370 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4371 left->getType().isStructureContainingSamplers())
4372 {
4373 error(loc, "undefined operation for structs containing samplers",
4374 GetOperatorString(op));
4375 return false;
4376 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004377
Olli Etuahoe1805592017-01-02 16:41:20 +00004378 if ((left->getNominalSize() != right->getNominalSize()) ||
4379 (left->getSecondarySize() != right->getSecondarySize()))
4380 {
4381 error(loc, "dimension mismatch", GetOperatorString(op));
4382 return false;
4383 }
4384 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004385 case EOpLessThan:
4386 case EOpGreaterThan:
4387 case EOpLessThanEqual:
4388 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004389 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004390 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004391 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004392 return false;
4393 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004394 break;
4395 case EOpAdd:
4396 case EOpSub:
4397 case EOpDiv:
4398 case EOpIMod:
4399 case EOpBitShiftLeft:
4400 case EOpBitShiftRight:
4401 case EOpBitwiseAnd:
4402 case EOpBitwiseXor:
4403 case EOpBitwiseOr:
4404 case EOpAddAssign:
4405 case EOpSubAssign:
4406 case EOpDivAssign:
4407 case EOpIModAssign:
4408 case EOpBitShiftLeftAssign:
4409 case EOpBitShiftRightAssign:
4410 case EOpBitwiseAndAssign:
4411 case EOpBitwiseXorAssign:
4412 case EOpBitwiseOrAssign:
4413 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4414 {
4415 return false;
4416 }
4417
4418 // Are the sizes compatible?
4419 if (left->getNominalSize() != right->getNominalSize() ||
4420 left->getSecondarySize() != right->getSecondarySize())
4421 {
4422 // If the nominal sizes of operands do not match:
4423 // One of them must be a scalar.
4424 if (!left->isScalar() && !right->isScalar())
4425 return false;
4426
4427 // In the case of compound assignment other than multiply-assign,
4428 // the right side needs to be a scalar. Otherwise a vector/matrix
4429 // would be assigned to a scalar. A scalar can't be shifted by a
4430 // vector either.
4431 if (!right->isScalar() &&
4432 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4433 return false;
4434 }
4435 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004436 default:
4437 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004438 }
4439
Olli Etuahod6b14282015-03-17 14:31:35 +02004440 return true;
4441}
4442
Olli Etuaho1dded802016-08-18 18:13:13 +03004443bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4444 const TType &left,
4445 const TType &right)
4446{
4447 switch (op)
4448 {
4449 case EOpMul:
4450 case EOpMulAssign:
4451 return left.getNominalSize() == right.getNominalSize() &&
4452 left.getSecondarySize() == right.getSecondarySize();
4453 case EOpVectorTimesScalar:
4454 return true;
4455 case EOpVectorTimesScalarAssign:
4456 ASSERT(!left.isMatrix() && !right.isMatrix());
4457 return left.isVector() && !right.isVector();
4458 case EOpVectorTimesMatrix:
4459 return left.getNominalSize() == right.getRows();
4460 case EOpVectorTimesMatrixAssign:
4461 ASSERT(!left.isMatrix() && right.isMatrix());
4462 return left.isVector() && left.getNominalSize() == right.getRows() &&
4463 left.getNominalSize() == right.getCols();
4464 case EOpMatrixTimesVector:
4465 return left.getCols() == right.getNominalSize();
4466 case EOpMatrixTimesScalar:
4467 return true;
4468 case EOpMatrixTimesScalarAssign:
4469 ASSERT(left.isMatrix() && !right.isMatrix());
4470 return !right.isVector();
4471 case EOpMatrixTimesMatrix:
4472 return left.getCols() == right.getRows();
4473 case EOpMatrixTimesMatrixAssign:
4474 ASSERT(left.isMatrix() && right.isMatrix());
4475 // We need to check two things:
4476 // 1. The matrix multiplication step is valid.
4477 // 2. The result will have the same number of columns as the lvalue.
4478 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4479
4480 default:
4481 UNREACHABLE();
4482 return false;
4483 }
4484}
4485
Jamie Madillb98c3a82015-07-23 14:26:04 -04004486TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4487 TIntermTyped *left,
4488 TIntermTyped *right,
4489 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004490{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004491 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004492 return nullptr;
4493
Olli Etuahofc1806e2015-03-17 13:03:11 +02004494 switch (op)
4495 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004496 case EOpEqual:
4497 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004498 case EOpLessThan:
4499 case EOpGreaterThan:
4500 case EOpLessThanEqual:
4501 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004502 break;
4503 case EOpLogicalOr:
4504 case EOpLogicalXor:
4505 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004506 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4507 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004508 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004509 {
4510 return nullptr;
4511 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004512 // Basic types matching should have been already checked.
4513 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004514 break;
4515 case EOpAdd:
4516 case EOpSub:
4517 case EOpDiv:
4518 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004519 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4520 !right->getType().getStruct());
4521 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004522 {
4523 return nullptr;
4524 }
4525 break;
4526 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004527 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4528 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004529 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004530 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004531 {
4532 return nullptr;
4533 }
4534 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004535 default:
4536 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004537 }
4538
Olli Etuaho1dded802016-08-18 18:13:13 +03004539 if (op == EOpMul)
4540 {
4541 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4542 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4543 {
4544 return nullptr;
4545 }
4546 }
4547
Olli Etuaho3fdec912016-08-18 15:08:06 +03004548 TIntermBinary *node = new TIntermBinary(op, left, right);
4549 node->setLine(loc);
4550
Olli Etuaho3fdec912016-08-18 15:08:06 +03004551 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004552 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004553}
4554
Jamie Madillb98c3a82015-07-23 14:26:04 -04004555TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4556 TIntermTyped *left,
4557 TIntermTyped *right,
4558 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004559{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004560 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004561 if (node == 0)
4562 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004563 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4564 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004565 return left;
4566 }
4567 return node;
4568}
4569
Jamie Madillb98c3a82015-07-23 14:26:04 -04004570TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4571 TIntermTyped *left,
4572 TIntermTyped *right,
4573 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004574{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004575 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03004576 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004577 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004578 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4579 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03004580 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03004581 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004582 }
4583 return node;
4584}
4585
Olli Etuaho13389b62016-10-16 11:48:18 +01004586TIntermBinary *TParseContext::createAssign(TOperator op,
4587 TIntermTyped *left,
4588 TIntermTyped *right,
4589 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004590{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004591 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004592 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004593 if (op == EOpMulAssign)
4594 {
4595 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4596 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4597 {
4598 return nullptr;
4599 }
4600 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004601 TIntermBinary *node = new TIntermBinary(op, left, right);
4602 node->setLine(loc);
4603
Olli Etuaho3fdec912016-08-18 15:08:06 +03004604 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004605 }
4606 return nullptr;
4607}
4608
Jamie Madillb98c3a82015-07-23 14:26:04 -04004609TIntermTyped *TParseContext::addAssign(TOperator op,
4610 TIntermTyped *left,
4611 TIntermTyped *right,
4612 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004613{
Olli Etuahocce89652017-06-19 16:04:09 +03004614 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004615 TIntermTyped *node = createAssign(op, left, right, loc);
4616 if (node == nullptr)
4617 {
4618 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004619 return left;
4620 }
4621 return node;
4622}
4623
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004624TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4625 TIntermTyped *right,
4626 const TSourceLoc &loc)
4627{
Corentin Wallez0d959252016-07-12 17:26:32 -04004628 // WebGL2 section 5.26, the following results in an error:
4629 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004630 if (mShaderSpec == SH_WEBGL2_SPEC &&
4631 (left->isArray() || left->getBasicType() == EbtVoid ||
4632 left->getType().isStructureContainingArrays() || right->isArray() ||
4633 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004634 {
4635 error(loc,
4636 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4637 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004638 }
4639
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004640 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4641 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4642 commaNode->getTypePointer()->setQualifier(resultQualifier);
4643 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004644}
4645
Olli Etuaho49300862015-02-20 14:54:49 +02004646TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4647{
4648 switch (op)
4649 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004650 case EOpContinue:
4651 if (mLoopNestingLevel <= 0)
4652 {
4653 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004654 }
4655 break;
4656 case EOpBreak:
4657 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4658 {
4659 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660 }
4661 break;
4662 case EOpReturn:
4663 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4664 {
4665 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004666 }
4667 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004668 case EOpKill:
4669 if (mShaderType != GL_FRAGMENT_SHADER)
4670 {
4671 error(loc, "discard supported in fragment shaders only", "discard");
4672 }
4673 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004674 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004675 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004676 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004677 }
Olli Etuahocce89652017-06-19 16:04:09 +03004678 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004679}
4680
Jamie Madillb98c3a82015-07-23 14:26:04 -04004681TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004682 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004684{
Olli Etuahocce89652017-06-19 16:04:09 +03004685 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004686 {
Olli Etuahocce89652017-06-19 16:04:09 +03004687 ASSERT(op == EOpReturn);
4688 mFunctionReturnsValue = true;
4689 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4690 {
4691 error(loc, "void function cannot return a value", "return");
4692 }
4693 else if (*mCurrentFunctionType != expression->getType())
4694 {
4695 error(loc, "function return is not matching type:", "return");
4696 }
Olli Etuaho49300862015-02-20 14:54:49 +02004697 }
Olli Etuahocce89652017-06-19 16:04:09 +03004698 TIntermBranch *node = new TIntermBranch(op, expression);
4699 node->setLine(loc);
4700 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004701}
4702
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004703void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4704{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004705 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004706 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004707 TIntermNode *offset = nullptr;
4708 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004709 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4710 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4711 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004712 {
4713 offset = arguments->back();
4714 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004715 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004716 {
4717 // A bias parameter might follow the offset parameter.
4718 ASSERT(arguments->size() >= 3);
4719 offset = (*arguments)[2];
4720 }
4721 if (offset != nullptr)
4722 {
4723 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4724 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4725 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004726 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004727 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004728 }
4729 else
4730 {
4731 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4732 size_t size = offsetConstantUnion->getType().getObjectSize();
4733 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4734 for (size_t i = 0u; i < size; ++i)
4735 {
4736 int offsetValue = values[i].getIConst();
4737 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4738 {
4739 std::stringstream tokenStream;
4740 tokenStream << offsetValue;
4741 std::string token = tokenStream.str();
4742 error(offset->getLine(), "Texture offset value out of valid range",
4743 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004744 }
4745 }
4746 }
4747 }
4748}
4749
Martin Radev2cc85b32016-08-05 16:22:53 +03004750// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4751void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4752{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004753 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004754 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4755
4756 if (name.compare(0, 5, "image") == 0)
4757 {
4758 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004759 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004760
Olli Etuaho485eefd2017-02-14 17:40:06 +00004761 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004762
4763 if (name.compare(5, 5, "Store") == 0)
4764 {
4765 if (memoryQualifier.readonly)
4766 {
4767 error(imageNode->getLine(),
4768 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004769 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004770 }
4771 }
4772 else if (name.compare(5, 4, "Load") == 0)
4773 {
4774 if (memoryQualifier.writeonly)
4775 {
4776 error(imageNode->getLine(),
4777 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004778 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004779 }
4780 }
4781 }
4782}
4783
4784// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4785void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4786 const TFunction *functionDefinition,
4787 const TIntermAggregate *functionCall)
4788{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004789 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004790
4791 const TIntermSequence &arguments = *functionCall->getSequence();
4792
4793 ASSERT(functionDefinition->getParamCount() == arguments.size());
4794
4795 for (size_t i = 0; i < arguments.size(); ++i)
4796 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004797 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4798 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004799 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4800 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4801
4802 if (IsImage(functionArgumentType.getBasicType()))
4803 {
4804 const TMemoryQualifier &functionArgumentMemoryQualifier =
4805 functionArgumentType.getMemoryQualifier();
4806 const TMemoryQualifier &functionParameterMemoryQualifier =
4807 functionParameterType.getMemoryQualifier();
4808 if (functionArgumentMemoryQualifier.readonly &&
4809 !functionParameterMemoryQualifier.readonly)
4810 {
4811 error(functionCall->getLine(),
4812 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004813 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004814 }
4815
4816 if (functionArgumentMemoryQualifier.writeonly &&
4817 !functionParameterMemoryQualifier.writeonly)
4818 {
4819 error(functionCall->getLine(),
4820 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004821 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004822 }
Martin Radev049edfa2016-11-11 14:35:37 +02004823
4824 if (functionArgumentMemoryQualifier.coherent &&
4825 !functionParameterMemoryQualifier.coherent)
4826 {
4827 error(functionCall->getLine(),
4828 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004829 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004830 }
4831
4832 if (functionArgumentMemoryQualifier.volatileQualifier &&
4833 !functionParameterMemoryQualifier.volatileQualifier)
4834 {
4835 error(functionCall->getLine(),
4836 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004837 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004838 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004839 }
4840 }
4841}
4842
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004843TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004844{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004845 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004846}
4847
4848TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004849 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004850 TIntermNode *thisNode,
4851 const TSourceLoc &loc)
4852{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004853 if (thisNode != nullptr)
4854 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004855 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004856 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004857
4858 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004859 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004860 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004861 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004862 }
4863 else
4864 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004865 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004866 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4867 }
4868}
4869
4870TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4871 TIntermSequence *arguments,
4872 TIntermNode *thisNode,
4873 const TSourceLoc &loc)
4874{
4875 TConstantUnion *unionArray = new TConstantUnion[1];
4876 int arraySize = 0;
4877 TIntermTyped *typedThis = thisNode->getAsTyped();
4878 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4879 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4880 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4881 // So accessing fnCall->getName() below is safe.
4882 if (fnCall->getName() != "length")
4883 {
4884 error(loc, "invalid method", fnCall->getName().c_str());
4885 }
4886 else if (!arguments->empty())
4887 {
4888 error(loc, "method takes no parameters", "length");
4889 }
4890 else if (typedThis == nullptr || !typedThis->isArray())
4891 {
4892 error(loc, "length can only be called on arrays", "length");
4893 }
4894 else
4895 {
4896 arraySize = typedThis->getArraySize();
4897 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004898 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004899 // This code path can be hit with expressions like these:
4900 // (a = b).length()
4901 // (func()).length()
4902 // (int[3](0, 1, 2)).length()
4903 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4904 // expression.
4905 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4906 // spec section 5.9 which allows "An array, vector or matrix expression with the
4907 // length method applied".
4908 error(loc, "length can only be called on array names, not on array expressions",
4909 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004910 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004911 }
4912 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03004913 TIntermConstantUnion *node =
4914 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
4915 node->setLine(loc);
4916 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004917}
4918
4919TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4920 TIntermSequence *arguments,
4921 const TSourceLoc &loc)
4922{
4923 // First find by unmangled name to check whether the function name has been
4924 // hidden by a variable name or struct typename.
4925 // If a function is found, check for one with a matching argument list.
4926 bool builtIn;
4927 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4928 if (symbol != nullptr && !symbol->isFunction())
4929 {
4930 error(loc, "function name expected", fnCall->getName().c_str());
4931 }
4932 else
4933 {
4934 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4935 mShaderVersion, &builtIn);
4936 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004937 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004938 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4939 }
4940 else
4941 {
4942 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004943 //
4944 // A declared function.
4945 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004946 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004947 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004948 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004949 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004950 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004951 if (builtIn && op != EOpNull)
4952 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004953 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004954 if (fnCandidate->getParamCount() == 1)
4955 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004956 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004957 TIntermNode *unaryParamNode = arguments->front();
4958 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004959 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004960 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004961 }
4962 else
4963 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004964 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004965 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004966 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004967
4968 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004969 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304970
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004971 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05304972 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004973 // See if we can constant fold a built-in. Note that this may be possible
4974 // even if it is not const-qualified.
4975 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304976 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004977 else
4978 {
4979 return callNode;
4980 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004981 }
4982 }
4983 else
4984 {
4985 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004986 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004987
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004988 // If builtIn == false, the function is user defined - could be an overloaded
4989 // built-in as well.
4990 // if builtIn == true, it's a builtIn function with no op associated with it.
4991 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004992 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004993 {
Olli Etuahofe486322017-03-21 09:30:54 +00004994 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004995 checkTextureOffsetConst(callNode);
4996 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004997 }
4998 else
4999 {
Olli Etuahofe486322017-03-21 09:30:54 +00005000 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005001 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005002 }
5003
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005004 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005005
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005006 callNode->setLine(loc);
5007
5008 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005009 }
5010 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005011 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005012
5013 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005014 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005015}
5016
Jamie Madillb98c3a82015-07-23 14:26:04 -04005017TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005018 TIntermTyped *trueExpression,
5019 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005020 const TSourceLoc &loc)
5021{
Olli Etuaho56229f12017-07-10 14:16:33 +03005022 if (!checkIsScalarBool(loc, cond))
5023 {
5024 return falseExpression;
5025 }
Olli Etuaho52901742015-04-15 13:42:45 +03005026
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005027 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005028 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005029 std::stringstream reasonStream;
5030 reasonStream << "mismatching ternary operator operand types '"
5031 << trueExpression->getCompleteString() << " and '"
5032 << falseExpression->getCompleteString() << "'";
5033 std::string reason = reasonStream.str();
5034 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005035 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005036 }
Olli Etuahode318b22016-10-25 16:18:25 +01005037 if (IsOpaqueType(trueExpression->getBasicType()))
5038 {
5039 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005040 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005041 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5042 // Note that structs containing opaque types don't need to be checked as structs are
5043 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005044 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005045 return falseExpression;
5046 }
5047
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005048 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005049 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005050 // ESSL 3.00.6 section 5.7:
5051 // Ternary operator support is optional for arrays. No certainty that it works across all
5052 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5053 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005054 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005055 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005056 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005057 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005058 }
Olli Etuaho94050052017-05-08 14:17:44 +03005059 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5060 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005061 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005062 return falseExpression;
5063 }
5064
Corentin Wallez0d959252016-07-12 17:26:32 -04005065 // WebGL2 section 5.26, the following results in an error:
5066 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005067 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005068 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005069 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005070 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005071 }
5072
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005073 // Note that the node resulting from here can be a constant union without being qualified as
5074 // constant.
5075 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5076 node->setLine(loc);
5077
5078 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005079}
Olli Etuaho49300862015-02-20 14:54:49 +02005080
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005081//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005082// Parse an array of strings using yyparse.
5083//
5084// Returns 0 for success.
5085//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005086int PaParseStrings(size_t count,
5087 const char *const string[],
5088 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305089 TParseContext *context)
5090{
Yunchao He4f285442017-04-21 12:15:49 +08005091 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005092 return 1;
5093
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005094 if (glslang_initialize(context))
5095 return 1;
5096
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005097 int error = glslang_scan(count, string, length, context);
5098 if (!error)
5099 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005100
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005101 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005102
alokp@chromium.org6b495712012-06-29 00:06:58 +00005103 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005104}
Jamie Madill45bcc782016-11-07 13:58:48 -05005105
5106} // namespace sh