blob: 8b04ae2268f58ee1129710f18b12bde0d8241345 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030015#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080016#include "compiler/translator/ValidateSwitch.h"
17#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030018#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000019
Jamie Madill45bcc782016-11-07 13:58:48 -050020namespace sh
21{
22
alokp@chromium.org8b851c62012-06-15 16:25:11 +000023///////////////////////////////////////////////////////////////////////
24//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025// Sub- vector and matrix fields
26//
27////////////////////////////////////////////////////////////////////////
28
Martin Radev2cc85b32016-08-05 16:22:53 +030029namespace
30{
31
32const int kWebGLMaxStructNesting = 4;
33
34bool ContainsSampler(const TType &type)
35{
36 if (IsSampler(type.getBasicType()))
37 return true;
38
jchen10cc2a10e2017-05-03 14:05:12 +080039 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030040 {
41 const TFieldList &fields = type.getStruct()->fields();
42 for (unsigned int i = 0; i < fields.size(); ++i)
43 {
44 if (ContainsSampler(*fields[i]->type()))
45 return true;
46 }
47 }
48
49 return false;
50}
51
Olli Etuaho485eefd2017-02-14 17:40:06 +000052// Get a token from an image argument to use as an error message token.
53const char *GetImageArgumentToken(TIntermTyped *imageNode)
54{
55 ASSERT(IsImage(imageNode->getBasicType()));
56 while (imageNode->getAsBinaryNode() &&
57 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
58 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
59 {
60 imageNode = imageNode->getAsBinaryNode()->getLeft();
61 }
62 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
63 if (imageSymbol)
64 {
65 return imageSymbol->getSymbol().c_str();
66 }
67 return "image";
68}
69
Olli Etuahocce89652017-06-19 16:04:09 +030070bool CanSetDefaultPrecisionOnType(const TPublicType &type)
71{
72 if (!SupportsPrecision(type.getBasicType()))
73 {
74 return false;
75 }
76 if (type.getBasicType() == EbtUInt)
77 {
78 // ESSL 3.00.4 section 4.5.4
79 return false;
80 }
81 if (type.isAggregate())
82 {
83 // Not allowed to set for aggregate types
84 return false;
85 }
86 return true;
87}
88
Martin Radev2cc85b32016-08-05 16:22:53 +030089} // namespace
90
jchen104cdac9e2017-05-08 11:01:20 +080091// This tracks each binding point's current default offset for inheritance of subsequent
92// variables using the same binding, and keeps offsets unique and non overlapping.
93// See GLSL ES 3.1, section 4.4.6.
94class TParseContext::AtomicCounterBindingState
95{
96 public:
97 AtomicCounterBindingState() : mDefaultOffset(0) {}
98 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
99 // newly inserted span.
100 int insertSpan(int start, size_t length)
101 {
102 gl::RangeI newSpan(start, start + static_cast<int>(length));
103 for (const auto &span : mSpans)
104 {
105 if (newSpan.intersects(span))
106 {
107 return -1;
108 }
109 }
110 mSpans.push_back(newSpan);
111 mDefaultOffset = newSpan.high();
112 return start;
113 }
114 // Inserts a new span starting from the default offset.
115 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
116 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
117
118 private:
119 int mDefaultOffset;
120 std::vector<gl::RangeI> mSpans;
121};
122
Jamie Madillacb4b812016-11-07 13:50:29 -0500123TParseContext::TParseContext(TSymbolTable &symt,
124 TExtensionBehavior &ext,
125 sh::GLenum type,
126 ShShaderSpec spec,
127 ShCompileOptions options,
128 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000129 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500130 const ShBuiltInResources &resources)
131 : intermediate(),
132 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 Etuaho856c4972016-08-08 11:38:39 +0300767void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530769 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
770 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000771 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530772 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773}
774
Jamie Madillb98c3a82015-07-23 14:26:04 -0400775// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300776// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300777void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778{
Martin Radev4a9cd802016-09-01 16:51:51 +0300779 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530780 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000781 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530782 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
jchen10cc2a10e2017-05-03 14:05:12 +0800785bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
786 const TTypeSpecifierNonArray &pType,
787 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000788{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530789 if (pType.type == EbtStruct)
790 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300791 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530792 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000793 std::stringstream reasonStream;
794 reasonStream << reason << " (structure contains a sampler)";
795 std::string reasonStr = reasonStream.str();
796 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300797 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000798 }
jchen10cc2a10e2017-05-03 14:05:12 +0800799 // only samplers need to be checked from structs, since other opaque types can't be struct
800 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300801 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530802 }
jchen10cc2a10e2017-05-03 14:05:12 +0800803 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530804 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000805 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300806 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
Olli Etuaho8a176262016-08-16 14:23:01 +0300809 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810}
811
Olli Etuaho856c4972016-08-08 11:38:39 +0300812void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
813 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400814{
815 if (pType.layoutQualifier.location != -1)
816 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400817 error(line, "location must only be specified for a single input or output variable",
818 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400819 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400820}
821
Olli Etuaho856c4972016-08-08 11:38:39 +0300822void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
823 const TLayoutQualifier &layoutQualifier)
824{
825 if (layoutQualifier.location != -1)
826 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000827 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
828 if (mShaderVersion >= 310)
829 {
830 errorMsg =
831 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
832 }
833 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300834 }
835}
836
Martin Radev2cc85b32016-08-05 16:22:53 +0300837void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
838 TQualifier qualifier,
839 const TType &type)
840{
Martin Radev2cc85b32016-08-05 16:22:53 +0300841 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800842 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530843 {
jchen10cc2a10e2017-05-03 14:05:12 +0800844 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846}
847
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300849unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530851 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000852
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200853 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
854 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
855 // fold as array size.
856 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000857 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000858 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300859 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861
Olli Etuaho856c4972016-08-08 11:38:39 +0300862 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400863
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000864 if (constant->getBasicType() == EbtUInt)
865 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300866 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000867 }
868 else
869 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300870 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000871
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000873 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400874 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300875 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000876 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400877
Olli Etuaho856c4972016-08-08 11:38:39 +0300878 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400879 }
880
Olli Etuaho856c4972016-08-08 11:38:39 +0300881 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400882 {
883 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300884 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400885 }
886
887 // The size of arrays is restricted here to prevent issues further down the
888 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
889 // 4096 registers so this should be reasonable even for aggressively optimizable code.
890 const unsigned int sizeLimit = 65536;
891
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400893 {
894 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300895 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300897
898 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300902bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
903 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904{
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 if ((elementQualifier.qualifier == EvqAttribute) ||
906 (elementQualifier.qualifier == EvqVertexIn) ||
907 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400909 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 TType(elementQualifier).getQualifierString());
911 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000912 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
Olli Etuaho8a176262016-08-16 14:23:01 +0300914 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915}
916
Olli Etuaho8a176262016-08-16 14:23:01 +0300917// See if this element type can be formed into an array.
918bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000920 //
921 // Can the type be an array?
922 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300923 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400924 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300925 error(line, "cannot declare arrays of arrays",
926 TType(elementType).getCompleteString().c_str());
927 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300929 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
930 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
931 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300932 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300934 {
935 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 TType(elementType).getCompleteString().c_str());
937 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939
Olli Etuaho8a176262016-08-16 14:23:01 +0300940 return true;
941}
942
943// Check if this qualified element type can be formed into an array.
944bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
945 const TPublicType &elementType)
946{
947 if (checkIsValidTypeForArray(indexLocation, elementType))
948 {
949 return checkIsValidQualifierForArray(indexLocation, elementType);
950 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000951 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300955void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
956 const TString &identifier,
957 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958{
Olli Etuaho3739d232015-04-08 12:23:44 +0300959 ASSERT(type != nullptr);
960 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000961 {
962 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300963 type->qualifier = EvqTemporary;
964
965 // Generate informative error messages for ESSL1.
966 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400967 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000968 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530969 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400970 "structures containing arrays may not be declared constant since they cannot be "
971 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530972 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000973 }
974 else
975 {
976 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
977 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300978 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000979 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300980 if (type->isUnsizedArray())
981 {
982 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984}
985
Olli Etuaho2935c582015-04-08 14:32:06 +0300986// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987// and update the symbol table.
988//
Olli Etuaho2935c582015-04-08 14:32:06 +0300989// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400991bool TParseContext::declareVariable(const TSourceLoc &line,
992 const TString &identifier,
993 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300994 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995{
Olli Etuaho2935c582015-04-08 14:32:06 +0300996 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997
Olli Etuaho43364892017-02-13 16:00:12 +0000998 checkBindingIsValid(line, type);
999
Olli Etuaho856c4972016-08-08 11:38:39 +03001000 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001
Olli Etuaho2935c582015-04-08 14:32:06 +03001002 // gl_LastFragData may be redeclared with a new precision qualifier
1003 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001005 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1006 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001007 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001008 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001009 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001011 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001012 }
1013 }
1014 else
1015 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001016 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1017 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 return false;
1019 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001020 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021
Olli Etuaho8a176262016-08-16 14:23:01 +03001022 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001023 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001024
Olli Etuaho2935c582015-04-08 14:32:06 +03001025 (*variable) = new TVariable(&identifier, type);
1026 if (!symbolTable.declare(*variable))
1027 {
1028 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001029 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001030 return false;
1031 }
1032
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 return false;
1035
1036 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037}
1038
Martin Radev70866b82016-07-22 15:27:42 +03001039void TParseContext::checkIsParameterQualifierValid(
1040 const TSourceLoc &line,
1041 const TTypeQualifierBuilder &typeQualifierBuilder,
1042 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301043{
Olli Etuahocce89652017-06-19 16:04:09 +03001044 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001045 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001046
1047 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301048 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001049 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1050 }
1051
1052 if (!IsImage(type->getBasicType()))
1053 {
Olli Etuaho43364892017-02-13 16:00:12 +00001054 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001055 }
1056 else
1057 {
1058 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001059 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060
Martin Radev70866b82016-07-22 15:27:42 +03001061 type->setQualifier(typeQualifier.qualifier);
1062
1063 if (typeQualifier.precision != EbpUndefined)
1064 {
1065 type->setPrecision(typeQualifier.precision);
1066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067}
1068
Olli Etuaho856c4972016-08-08 11:38:39 +03001069bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001070{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001071 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001072 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301073 if (iter == extBehavior.end())
1074 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001075 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001076 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001077 }
zmo@google.comf5450912011-09-09 01:37:19 +00001078 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301079 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1080 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001081 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1082 // associated with more than one extension.
1083 if (extension == "GL_OVR_multiview")
1084 {
1085 return checkCanUseExtension(line, "GL_OVR_multiview2");
1086 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001087 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001088 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001089 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 if (iter->second == EBhWarn)
1091 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001092 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001093 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001094 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001095
Olli Etuaho8a176262016-08-16 14:23:01 +03001096 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097}
1098
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001099// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1100// compile-time or link-time errors are the same whether or not the declaration is empty".
1101// This function implements all the checks that are done on qualifiers regardless of if the
1102// declaration is empty.
1103void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1104 const sh::TLayoutQualifier &layoutQualifier,
1105 const TSourceLoc &location)
1106{
1107 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1108 {
1109 error(location, "Shared memory declarations cannot have layout specified", "layout");
1110 }
1111
1112 if (layoutQualifier.matrixPacking != EmpUnspecified)
1113 {
1114 error(location, "layout qualifier only valid for interface blocks",
1115 getMatrixPackingString(layoutQualifier.matrixPacking));
1116 return;
1117 }
1118
1119 if (layoutQualifier.blockStorage != EbsUnspecified)
1120 {
1121 error(location, "layout qualifier only valid for interface blocks",
1122 getBlockStorageString(layoutQualifier.blockStorage));
1123 return;
1124 }
1125
1126 if (qualifier == EvqFragmentOut)
1127 {
1128 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1129 {
1130 error(location, "invalid layout qualifier combination", "yuv");
1131 return;
1132 }
1133 }
1134 else
1135 {
1136 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1137 }
1138
Olli Etuaho95468d12017-05-04 11:14:34 +03001139 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1140 // parsing steps. So it needs to be checked here.
1141 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1142 {
1143 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1144 }
1145
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001146 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1147 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1148 {
1149 canHaveLocation = true;
1150 // We're not checking whether the uniform location is in range here since that depends on
1151 // the type of the variable.
1152 // The type can only be fully determined for non-empty declarations.
1153 }
1154 if (!canHaveLocation)
1155 {
1156 checkLocationIsNotSpecified(location, layoutQualifier);
1157 }
1158}
1159
jchen104cdac9e2017-05-08 11:01:20 +08001160void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1161 const TSourceLoc &location)
1162{
1163 if (publicType.precision != EbpHigh)
1164 {
1165 error(location, "Can only be highp", "atomic counter");
1166 }
1167 // dEQP enforces compile error if location is specified. See uniform_location.test.
1168 if (publicType.layoutQualifier.location != -1)
1169 {
1170 error(location, "location must not be set for atomic_uint", "layout");
1171 }
1172 if (publicType.layoutQualifier.binding == -1)
1173 {
1174 error(location, "no binding specified", "atomic counter");
1175 }
1176}
1177
Martin Radevb8b01222016-11-20 23:25:53 +02001178void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1179 const TSourceLoc &location)
1180{
1181 if (publicType.isUnsizedArray())
1182 {
1183 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1184 // error. It is assumed that this applies to empty declarations as well.
1185 error(location, "empty array declaration needs to specify a size", "");
1186 }
Martin Radevb8b01222016-11-20 23:25:53 +02001187}
1188
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001189// These checks are done for all declarations that are non-empty. They're done for non-empty
1190// declarations starting a declarator list, and declarators that follow an empty declaration.
1191void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1192 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001193{
Olli Etuahofa33d582015-04-09 14:33:12 +03001194 switch (publicType.qualifier)
1195 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001196 case EvqVaryingIn:
1197 case EvqVaryingOut:
1198 case EvqAttribute:
1199 case EvqVertexIn:
1200 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001201 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001202 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001203 {
1204 error(identifierLocation, "cannot be used with a structure",
1205 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001206 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001207 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001208
Jamie Madillb98c3a82015-07-23 14:26:04 -04001209 default:
1210 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001211 }
jchen10cc2a10e2017-05-03 14:05:12 +08001212 std::string reason(getBasicString(publicType.getBasicType()));
1213 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001214 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001215 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001216 {
1217 return;
1218 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001219
Andrei Volykhina5527072017-03-22 16:46:30 +03001220 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1221 publicType.qualifier != EvqConst) &&
1222 publicType.getBasicType() == EbtYuvCscStandardEXT)
1223 {
1224 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1225 getQualifierString(publicType.qualifier));
1226 return;
1227 }
1228
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001229 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1230 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001231 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1232 // But invalid shaders may still reach here with an unsized array declaration.
1233 if (!publicType.isUnsizedArray())
1234 {
1235 TType type(publicType);
1236 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1237 publicType.layoutQualifier);
1238 }
1239 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001240
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001241 // check for layout qualifier issues
1242 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001243
Martin Radev2cc85b32016-08-05 16:22:53 +03001244 if (IsImage(publicType.getBasicType()))
1245 {
1246
1247 switch (layoutQualifier.imageInternalFormat)
1248 {
1249 case EiifRGBA32F:
1250 case EiifRGBA16F:
1251 case EiifR32F:
1252 case EiifRGBA8:
1253 case EiifRGBA8_SNORM:
1254 if (!IsFloatImage(publicType.getBasicType()))
1255 {
1256 error(identifierLocation,
1257 "internal image format requires a floating image type",
1258 getBasicString(publicType.getBasicType()));
1259 return;
1260 }
1261 break;
1262 case EiifRGBA32I:
1263 case EiifRGBA16I:
1264 case EiifRGBA8I:
1265 case EiifR32I:
1266 if (!IsIntegerImage(publicType.getBasicType()))
1267 {
1268 error(identifierLocation,
1269 "internal image format requires an integer image type",
1270 getBasicString(publicType.getBasicType()));
1271 return;
1272 }
1273 break;
1274 case EiifRGBA32UI:
1275 case EiifRGBA16UI:
1276 case EiifRGBA8UI:
1277 case EiifR32UI:
1278 if (!IsUnsignedImage(publicType.getBasicType()))
1279 {
1280 error(identifierLocation,
1281 "internal image format requires an unsigned image type",
1282 getBasicString(publicType.getBasicType()));
1283 return;
1284 }
1285 break;
1286 case EiifUnspecified:
1287 error(identifierLocation, "layout qualifier", "No image internal format specified");
1288 return;
1289 default:
1290 error(identifierLocation, "layout qualifier", "unrecognized token");
1291 return;
1292 }
1293
1294 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1295 switch (layoutQualifier.imageInternalFormat)
1296 {
1297 case EiifR32F:
1298 case EiifR32I:
1299 case EiifR32UI:
1300 break;
1301 default:
1302 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1303 {
1304 error(identifierLocation, "layout qualifier",
1305 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1306 "image variables must be qualified readonly and/or writeonly");
1307 return;
1308 }
1309 break;
1310 }
1311 }
1312 else
1313 {
Olli Etuaho43364892017-02-13 16:00:12 +00001314 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001315 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1316 }
jchen104cdac9e2017-05-08 11:01:20 +08001317
1318 if (IsAtomicCounter(publicType.getBasicType()))
1319 {
1320 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1321 }
1322 else
1323 {
1324 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1325 }
Olli Etuaho43364892017-02-13 16:00:12 +00001326}
Martin Radev2cc85b32016-08-05 16:22:53 +03001327
Olli Etuaho43364892017-02-13 16:00:12 +00001328void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1329{
1330 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1331 int arraySize = type.isArray() ? type.getArraySize() : 1;
1332 if (IsImage(type.getBasicType()))
1333 {
1334 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1335 }
1336 else if (IsSampler(type.getBasicType()))
1337 {
1338 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1339 }
jchen104cdac9e2017-05-08 11:01:20 +08001340 else if (IsAtomicCounter(type.getBasicType()))
1341 {
1342 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1343 }
Olli Etuaho43364892017-02-13 16:00:12 +00001344 else
1345 {
1346 ASSERT(!IsOpaqueType(type.getBasicType()));
1347 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001348 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001349}
1350
Olli Etuaho856c4972016-08-08 11:38:39 +03001351void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1352 const TString &layoutQualifierName,
1353 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001354{
1355
1356 if (mShaderVersion < versionRequired)
1357 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001358 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001359 }
1360}
1361
Olli Etuaho856c4972016-08-08 11:38:39 +03001362bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1363 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001364{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001365 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001366 for (size_t i = 0u; i < localSize.size(); ++i)
1367 {
1368 if (localSize[i] != -1)
1369 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001370 error(location,
1371 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1372 "global layout declaration",
1373 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001374 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001375 }
1376 }
1377
Olli Etuaho8a176262016-08-16 14:23:01 +03001378 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001379}
1380
Olli Etuaho43364892017-02-13 16:00:12 +00001381void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001382 TLayoutImageInternalFormat internalFormat)
1383{
1384 if (internalFormat != EiifUnspecified)
1385 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001386 error(location, "invalid layout qualifier: only valid when used with images",
1387 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001388 }
Olli Etuaho43364892017-02-13 16:00:12 +00001389}
1390
1391void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1392{
1393 if (binding != -1)
1394 {
1395 error(location,
1396 "invalid layout qualifier: only valid when used with opaque types or blocks",
1397 "binding");
1398 }
1399}
1400
jchen104cdac9e2017-05-08 11:01:20 +08001401void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1402{
1403 if (offset != -1)
1404 {
1405 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1406 "offset");
1407 }
1408}
1409
Olli Etuaho43364892017-02-13 16:00:12 +00001410void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1411{
1412 // Expects arraySize to be 1 when setting binding for only a single variable.
1413 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1414 {
1415 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1416 }
1417}
1418
1419void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1420 int binding,
1421 int arraySize)
1422{
1423 // Expects arraySize to be 1 when setting binding for only a single variable.
1424 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1425 {
1426 error(location, "sampler binding greater than maximum texture units", "binding");
1427 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001428}
1429
jchen10af713a22017-04-19 09:10:56 +08001430void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1431{
1432 int size = (arraySize == 0 ? 1 : arraySize);
1433 if (binding + size > mMaxUniformBufferBindings)
1434 {
1435 error(location, "interface block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1436 "binding");
1437 }
1438}
jchen104cdac9e2017-05-08 11:01:20 +08001439void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1440{
1441 if (binding >= mMaxAtomicCounterBindings)
1442 {
1443 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1444 "binding");
1445 }
1446}
jchen10af713a22017-04-19 09:10:56 +08001447
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001448void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1449 int objectLocationCount,
1450 const TLayoutQualifier &layoutQualifier)
1451{
1452 int loc = layoutQualifier.location;
1453 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1454 {
1455 error(location, "Uniform location out of range", "location");
1456 }
1457}
1458
Andrei Volykhina5527072017-03-22 16:46:30 +03001459void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1460{
1461 if (yuv != false)
1462 {
1463 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1464 }
1465}
1466
Olli Etuaho383b7912016-08-05 11:22:59 +03001467void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001468 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001469{
1470 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1471 {
1472 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1473 if (qual == EvqOut || qual == EvqInOut)
1474 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001475 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001476 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001477 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001478 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001479 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001480 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001481 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001482 }
1483 }
1484 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001485}
1486
Martin Radev70866b82016-07-22 15:27:42 +03001487void TParseContext::checkInvariantVariableQualifier(bool invariant,
1488 const TQualifier qualifier,
1489 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001490{
Martin Radev70866b82016-07-22 15:27:42 +03001491 if (!invariant)
1492 return;
1493
1494 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001495 {
Martin Radev70866b82016-07-22 15:27:42 +03001496 // input variables in the fragment shader can be also qualified as invariant
1497 if (!sh::CanBeInvariantESSL1(qualifier))
1498 {
1499 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1500 }
1501 }
1502 else
1503 {
1504 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1505 {
1506 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1507 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001508 }
1509}
1510
Arun Patole7e7e68d2015-05-22 12:02:25 +05301511bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001514 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1515 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001516}
1517
Arun Patole7e7e68d2015-05-22 12:02:25 +05301518bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001519{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001520 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001521}
1522
Jamie Madillb98c3a82015-07-23 14:26:04 -04001523void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1524 const char *extName,
1525 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001526{
1527 pp::SourceLocation srcLoc;
1528 srcLoc.file = loc.first_file;
1529 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001530 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001531}
1532
Jamie Madillb98c3a82015-07-23 14:26:04 -04001533void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1534 const char *name,
1535 const char *value,
1536 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001537{
1538 pp::SourceLocation srcLoc;
1539 srcLoc.file = loc.first_file;
1540 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001541 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001542}
1543
Martin Radev4c4c8e72016-08-04 12:25:34 +03001544sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001545{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001546 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001547 for (size_t i = 0u; i < result.size(); ++i)
1548 {
1549 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1550 {
1551 result[i] = 1;
1552 }
1553 else
1554 {
1555 result[i] = mComputeShaderLocalSize[i];
1556 }
1557 }
1558 return result;
1559}
1560
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001561/////////////////////////////////////////////////////////////////////////////////
1562//
1563// Non-Errors.
1564//
1565/////////////////////////////////////////////////////////////////////////////////
1566
Jamie Madill5c097022014-08-20 16:38:32 -04001567const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1568 const TString *name,
1569 const TSymbol *symbol)
1570{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001571 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001572
1573 if (!symbol)
1574 {
1575 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001576 }
1577 else if (!symbol->isVariable())
1578 {
1579 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001580 }
1581 else
1582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001583 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001584
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001585 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001586 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001587 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001588 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001589 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001590
1591 // Reject shaders using both gl_FragData and gl_FragColor
1592 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001593 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001594 {
1595 mUsesFragData = true;
1596 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001597 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001598 {
1599 mUsesFragColor = true;
1600 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001601 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1602 {
1603 mUsesSecondaryOutputs = true;
1604 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001605
1606 // This validation is not quite correct - it's only an error to write to
1607 // both FragData and FragColor. For simplicity, and because users shouldn't
1608 // be rewarded for reading from undefined varaibles, return an error
1609 // if they are both referenced, rather than assigned.
1610 if (mUsesFragData && mUsesFragColor)
1611 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001612 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1613 if (mUsesSecondaryOutputs)
1614 {
1615 errorMessage =
1616 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1617 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1618 }
1619 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001620 }
Martin Radevb0883602016-08-04 17:48:58 +03001621
1622 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1623 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1624 qualifier == EvqWorkGroupSize)
1625 {
1626 error(location,
1627 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1628 "gl_WorkGroupSize");
1629 }
Jamie Madill5c097022014-08-20 16:38:32 -04001630 }
1631
1632 if (!variable)
1633 {
1634 TType type(EbtFloat, EbpUndefined);
1635 TVariable *fakeVariable = new TVariable(name, type);
1636 symbolTable.declare(fakeVariable);
1637 variable = fakeVariable;
1638 }
1639
1640 return variable;
1641}
1642
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001643TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1644 const TString *name,
1645 const TSymbol *symbol)
1646{
1647 const TVariable *variable = getNamedVariable(location, name, symbol);
1648
Olli Etuaho09b04a22016-12-15 13:30:26 +00001649 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1650 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1651 {
1652 // WEBGL_multiview spec
1653 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1654 "gl_ViewID_OVR");
1655 }
1656
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001657 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001658 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001659 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001660 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001661 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001662 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1663 mComputeShaderLocalSizeDeclared)
1664 {
1665 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1666 // needs to be added to the AST as a constant and not as a symbol.
1667 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1668 TConstantUnion *constArray = new TConstantUnion[3];
1669 for (size_t i = 0; i < 3; ++i)
1670 {
1671 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1672 }
1673
1674 ASSERT(variable->getType().getBasicType() == EbtUInt);
1675 ASSERT(variable->getType().getObjectSize() == 3);
1676
1677 TType type(variable->getType());
1678 type.setQualifier(EvqConst);
1679 return intermediate.addConstantUnion(constArray, type, location);
1680 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001681 else
1682 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001683 TIntermSymbol *symbolNode =
1684 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1685 symbolNode->setLine(location);
1686 return symbolNode;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001687 }
1688}
1689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690// Initializers show up in several places in the grammar. Have one set of
1691// code to handle them here.
1692//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001693// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001694bool TParseContext::executeInitializer(const TSourceLoc &line,
1695 const TString &identifier,
1696 const TPublicType &pType,
1697 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001698 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699{
Olli Etuaho13389b62016-10-16 11:48:18 +01001700 ASSERT(initNode != nullptr);
1701 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001702 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001703
Olli Etuaho2935c582015-04-08 14:32:06 +03001704 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001705 if (type.isUnsizedArray())
1706 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001707 // We have not checked yet whether the initializer actually is an array or not.
1708 if (initializer->isArray())
1709 {
1710 type.setArraySize(initializer->getArraySize());
1711 }
1712 else
1713 {
1714 // Having a non-array initializer for an unsized array will result in an error later,
1715 // so we don't generate an error message here.
1716 type.setArraySize(1u);
1717 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001718 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001719 if (!declareVariable(line, identifier, type, &variable))
1720 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001721 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
Olli Etuahob0c645e2015-05-12 14:25:36 +03001724 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001725 if (symbolTable.atGlobalLevel() &&
1726 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001727 {
1728 // Error message does not completely match behavior with ESSL 1.00, but
1729 // we want to steer developers towards only using constant expressions.
1730 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001731 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001732 }
1733 if (globalInitWarning)
1734 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001735 warning(
1736 line,
1737 "global variable initializers should be constant expressions "
1738 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1739 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001740 }
1741
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001742 //
1743 // identifier must be of type constant, a global, or a temporary
1744 //
1745 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301746 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1747 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 error(line, " cannot initialize this type of qualifier ",
1749 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001750 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 }
1752 //
1753 // test for and propagate constant
1754 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755
Arun Patole7e7e68d2015-05-22 12:02:25 +05301756 if (qualifier == EvqConst)
1757 {
1758 if (qualifier != initializer->getType().getQualifier())
1759 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001760 std::stringstream reasonStream;
1761 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1762 << "'";
1763 std::string reason = reasonStream.str();
1764 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001765 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001766 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001767 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301768 if (type != initializer->getType())
1769 {
1770 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001772 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001773 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001774 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001775
1776 // Save the constant folded value to the variable if possible. For example array
1777 // initializers are not folded, since that way copying the array literal to multiple places
1778 // in the shader is avoided.
1779 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1780 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301781 if (initializer->getAsConstantUnion())
1782 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001783 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001784 ASSERT(*initNode == nullptr);
1785 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301786 }
1787 else if (initializer->getAsSymbolNode())
1788 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789 const TSymbol *symbol =
1790 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1791 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001793 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001794 if (constArray)
1795 {
1796 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001797 ASSERT(*initNode == nullptr);
1798 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001799 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001800 }
1801 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001802
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001803 TIntermSymbol *intermSymbol =
1804 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1805 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001806 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1807 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001808 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001809 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001810 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001812
Olli Etuaho914b79a2017-06-19 16:03:19 +03001813 return true;
1814}
1815
1816TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1817 const TString &identifier,
1818 TIntermTyped *initializer,
1819 const TSourceLoc &loc)
1820{
1821 checkIsScalarBool(loc, pType);
1822 TIntermBinary *initNode = nullptr;
1823 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1824 {
1825 // The initializer is valid. The init condition needs to have a node - either the
1826 // initializer node, or a constant node in case the initialized variable is const and won't
1827 // be recorded in the AST.
1828 if (initNode == nullptr)
1829 {
1830 return initializer;
1831 }
1832 else
1833 {
1834 TIntermDeclaration *declaration = new TIntermDeclaration();
1835 declaration->appendDeclarator(initNode);
1836 return declaration;
1837 }
1838 }
1839 return nullptr;
1840}
1841
1842TIntermNode *TParseContext::addLoop(TLoopType type,
1843 TIntermNode *init,
1844 TIntermNode *cond,
1845 TIntermTyped *expr,
1846 TIntermNode *body,
1847 const TSourceLoc &line)
1848{
1849 TIntermNode *node = nullptr;
1850 TIntermTyped *typedCond = nullptr;
1851 if (cond)
1852 {
1853 typedCond = cond->getAsTyped();
1854 }
1855 if (cond == nullptr || typedCond)
1856 {
Olli Etuahocce89652017-06-19 16:04:09 +03001857 if (type == ELoopDoWhile)
1858 {
1859 checkIsScalarBool(line, typedCond);
1860 }
1861 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1862 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1863 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1864 !typedCond->isVector()));
1865
Olli Etuaho914b79a2017-06-19 16:03:19 +03001866 node = new TIntermLoop(type, init, typedCond, expr, TIntermediate::EnsureBlock(body));
1867 node->setLine(line);
1868 return node;
1869 }
1870
Olli Etuahocce89652017-06-19 16:04:09 +03001871 ASSERT(type != ELoopDoWhile);
1872
Olli Etuaho914b79a2017-06-19 16:03:19 +03001873 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1874 ASSERT(declaration);
1875 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1876 ASSERT(declarator->getLeft()->getAsSymbolNode());
1877
1878 // The condition is a declaration. In the AST representation we don't support declarations as
1879 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1880 // the loop.
1881 TIntermBlock *block = new TIntermBlock();
1882
1883 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1884 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1885 block->appendStatement(declareCondition);
1886
1887 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1888 declarator->getRight()->deepCopy());
1889 TIntermLoop *loop =
1890 new TIntermLoop(type, init, conditionInit, expr, TIntermediate::EnsureBlock(body));
1891 block->appendStatement(loop);
1892 loop->setLine(line);
1893 block->setLine(line);
1894 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895}
1896
Olli Etuahocce89652017-06-19 16:04:09 +03001897TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1898 TIntermNodePair code,
1899 const TSourceLoc &loc)
1900{
1901 checkIsScalarBool(loc, cond);
1902
1903 // For compile time constant conditions, prune the code now.
1904 if (cond->getAsConstantUnion())
1905 {
1906 if (cond->getAsConstantUnion()->getBConst(0) == true)
1907 {
1908 return TIntermediate::EnsureBlock(code.node1);
1909 }
1910 else
1911 {
1912 return TIntermediate::EnsureBlock(code.node2);
1913 }
1914 }
1915
1916 TIntermIfElse *node = new TIntermIfElse(cond, TIntermediate::EnsureBlock(code.node1),
1917 TIntermediate::EnsureBlock(code.node2));
1918 node->setLine(loc);
1919
1920 return node;
1921}
1922
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001923void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1924{
1925 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1926 typeSpecifier->getBasicType());
1927
1928 if (mShaderVersion < 300 && typeSpecifier->array)
1929 {
1930 error(typeSpecifier->getLine(), "not supported", "first-class array");
1931 typeSpecifier->clearArrayness();
1932 }
1933}
1934
Martin Radev70866b82016-07-22 15:27:42 +03001935TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301936 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001937{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001938 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001939
Martin Radev70866b82016-07-22 15:27:42 +03001940 TPublicType returnType = typeSpecifier;
1941 returnType.qualifier = typeQualifier.qualifier;
1942 returnType.invariant = typeQualifier.invariant;
1943 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001944 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001945 returnType.precision = typeSpecifier.precision;
1946
1947 if (typeQualifier.precision != EbpUndefined)
1948 {
1949 returnType.precision = typeQualifier.precision;
1950 }
1951
Martin Radev4a9cd802016-09-01 16:51:51 +03001952 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1953 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001954
Martin Radev4a9cd802016-09-01 16:51:51 +03001955 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1956 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001957
Martin Radev4a9cd802016-09-01 16:51:51 +03001958 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001959
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001960 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001961 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001962 if (typeSpecifier.array)
1963 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001964 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001965 returnType.clearArrayness();
1966 }
1967
Martin Radev70866b82016-07-22 15:27:42 +03001968 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001969 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001970 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001971 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001972 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001973 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001974
Martin Radev70866b82016-07-22 15:27:42 +03001975 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001976 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001977 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001978 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001979 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001980 }
1981 }
1982 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001983 {
Martin Radev70866b82016-07-22 15:27:42 +03001984 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001985 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001986 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001987 }
Martin Radev70866b82016-07-22 15:27:42 +03001988 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1989 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001990 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001991 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1992 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001993 }
Martin Radev70866b82016-07-22 15:27:42 +03001994 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001995 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001996 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001997 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001998 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001999 }
2000
2001 return returnType;
2002}
2003
Olli Etuaho856c4972016-08-08 11:38:39 +03002004void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2005 const TPublicType &type,
2006 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002007{
2008 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002009 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002010 {
2011 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002012 }
2013
2014 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2015 switch (qualifier)
2016 {
2017 case EvqVertexIn:
2018 // ESSL 3.00 section 4.3.4
2019 if (type.array)
2020 {
2021 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002022 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002023 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002024 return;
2025 case EvqFragmentOut:
2026 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002027 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002028 {
2029 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002030 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002031 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002032 return;
2033 default:
2034 break;
2035 }
2036
2037 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2038 // restrictions.
2039 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002040 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2041 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002042 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2043 {
2044 error(qualifierLocation, "must use 'flat' interpolation here",
2045 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002046 }
2047
Martin Radev4a9cd802016-09-01 16:51:51 +03002048 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002049 {
2050 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2051 // These restrictions are only implied by the ESSL 3.00 spec, but
2052 // the ESSL 3.10 spec lists these restrictions explicitly.
2053 if (type.array)
2054 {
2055 error(qualifierLocation, "cannot be an array of structures",
2056 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002057 }
2058 if (type.isStructureContainingArrays())
2059 {
2060 error(qualifierLocation, "cannot be a structure containing an array",
2061 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002062 }
2063 if (type.isStructureContainingType(EbtStruct))
2064 {
2065 error(qualifierLocation, "cannot be a structure containing a structure",
2066 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002067 }
2068 if (type.isStructureContainingType(EbtBool))
2069 {
2070 error(qualifierLocation, "cannot be a structure containing a bool",
2071 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002072 }
2073 }
2074}
2075
Martin Radev2cc85b32016-08-05 16:22:53 +03002076void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2077{
2078 if (qualifier.getType() == QtStorage)
2079 {
2080 const TStorageQualifierWrapper &storageQualifier =
2081 static_cast<const TStorageQualifierWrapper &>(qualifier);
2082 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2083 !symbolTable.atGlobalLevel())
2084 {
2085 error(storageQualifier.getLine(),
2086 "Local variables can only use the const storage qualifier.",
2087 storageQualifier.getQualifierString().c_str());
2088 }
2089 }
2090}
2091
Olli Etuaho43364892017-02-13 16:00:12 +00002092void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002093 const TSourceLoc &location)
2094{
2095 if (memoryQualifier.readonly)
2096 {
2097 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002098 }
2099 if (memoryQualifier.writeonly)
2100 {
2101 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002102 }
Martin Radev049edfa2016-11-11 14:35:37 +02002103 if (memoryQualifier.coherent)
2104 {
2105 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002106 }
2107 if (memoryQualifier.restrictQualifier)
2108 {
2109 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002110 }
2111 if (memoryQualifier.volatileQualifier)
2112 {
2113 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002114 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002115}
2116
jchen104cdac9e2017-05-08 11:01:20 +08002117// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2118// intermediate tree.
2119void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2120 size_t size,
2121 bool forceAppend,
2122 const TSourceLoc &loc,
2123 TType &type)
2124{
2125 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2126 int offset;
2127 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2128 {
2129 offset = bindingState.appendSpan(size);
2130 }
2131 else
2132 {
2133 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2134 }
2135 if (offset == -1)
2136 {
2137 error(loc, "Offset overlapping", "atomic counter");
2138 return;
2139 }
2140 TLayoutQualifier qualifier = type.getLayoutQualifier();
2141 qualifier.offset = offset;
2142 type.setLayoutQualifier(qualifier);
2143}
2144
Olli Etuaho13389b62016-10-16 11:48:18 +01002145TIntermDeclaration *TParseContext::parseSingleDeclaration(
2146 TPublicType &publicType,
2147 const TSourceLoc &identifierOrTypeLocation,
2148 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002149{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002150 TType type(publicType);
2151 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2152 mDirectiveHandler.pragma().stdgl.invariantAll)
2153 {
2154 TQualifier qualifier = type.getQualifier();
2155
2156 // The directive handler has already taken care of rejecting invalid uses of this pragma
2157 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2158 // affected variable declarations:
2159 //
2160 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2161 // elsewhere, in TranslatorGLSL.)
2162 //
2163 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2164 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2165 // the way this is currently implemented we have to enable this compiler option before
2166 // parsing the shader and determining the shading language version it uses. If this were
2167 // implemented as a post-pass, the workaround could be more targeted.
2168 //
2169 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2170 // the specification, but there are desktop OpenGL drivers that expect that this is the
2171 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2172 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2173 {
2174 type.setInvariant(true);
2175 }
2176 }
2177
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002178 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2179 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002180
Olli Etuahobab4c082015-04-24 16:38:49 +03002181 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002182 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002183
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002184 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002185 if (emptyDeclaration)
2186 {
Martin Radevb8b01222016-11-20 23:25:53 +02002187 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002188 // In most cases we don't need to create a symbol node for an empty declaration.
2189 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2190 if (type.getBasicType() == EbtStruct)
2191 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002192 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002193 }
jchen104cdac9e2017-05-08 11:01:20 +08002194 else if (IsAtomicCounter(publicType.getBasicType()))
2195 {
2196 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2197 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002198 }
2199 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002200 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002201 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002202
Olli Etuaho856c4972016-08-08 11:38:39 +03002203 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002204
jchen104cdac9e2017-05-08 11:01:20 +08002205 if (IsAtomicCounter(publicType.getBasicType()))
2206 {
2207
2208 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2209 identifierOrTypeLocation, type);
2210 }
2211
Olli Etuaho2935c582015-04-08 14:32:06 +03002212 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002213 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002214
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002215 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002216 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002217 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002218 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002219 }
2220
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002221 TIntermDeclaration *declaration = new TIntermDeclaration();
2222 declaration->setLine(identifierOrTypeLocation);
2223 if (symbol)
2224 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002225 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002226 declaration->appendDeclarator(symbol);
2227 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002228 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002229}
2230
Olli Etuaho13389b62016-10-16 11:48:18 +01002231TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2232 const TSourceLoc &identifierLocation,
2233 const TString &identifier,
2234 const TSourceLoc &indexLocation,
2235 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002236{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002237 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002238
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002239 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2240 identifierLocation);
2241
2242 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002243
Olli Etuaho856c4972016-08-08 11:38:39 +03002244 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002245
Olli Etuaho8a176262016-08-16 14:23:01 +03002246 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002247
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002248 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002249
Olli Etuaho856c4972016-08-08 11:38:39 +03002250 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002251 // Make the type an array even if size check failed.
2252 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2253 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002254
jchen104cdac9e2017-05-08 11:01:20 +08002255 if (IsAtomicCounter(publicType.getBasicType()))
2256 {
2257 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2258 identifierLocation, arrayType);
2259 }
2260
Olli Etuaho2935c582015-04-08 14:32:06 +03002261 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002262 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002263
Olli Etuaho13389b62016-10-16 11:48:18 +01002264 TIntermDeclaration *declaration = new TIntermDeclaration();
2265 declaration->setLine(identifierLocation);
2266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002267 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002268 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002269 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2270 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002271 declaration->appendDeclarator(symbol);
2272 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002273
Olli Etuaho13389b62016-10-16 11:48:18 +01002274 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002275}
2276
Olli Etuaho13389b62016-10-16 11:48:18 +01002277TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2278 const TSourceLoc &identifierLocation,
2279 const TString &identifier,
2280 const TSourceLoc &initLocation,
2281 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002282{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002283 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002284
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002285 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2286 identifierLocation);
2287
2288 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002289
Olli Etuaho13389b62016-10-16 11:48:18 +01002290 TIntermDeclaration *declaration = new TIntermDeclaration();
2291 declaration->setLine(identifierLocation);
2292
2293 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002294 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002295 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002296 if (initNode)
2297 {
2298 declaration->appendDeclarator(initNode);
2299 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002300 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002301 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002302}
2303
Olli Etuaho13389b62016-10-16 11:48:18 +01002304TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002305 TPublicType &publicType,
2306 const TSourceLoc &identifierLocation,
2307 const TString &identifier,
2308 const TSourceLoc &indexLocation,
2309 TIntermTyped *indexExpression,
2310 const TSourceLoc &initLocation,
2311 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002312{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002314
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002315 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2316 identifierLocation);
2317
2318 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002319
Olli Etuaho8a176262016-08-16 14:23:01 +03002320 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002321
2322 TPublicType arrayType(publicType);
2323
Olli Etuaho856c4972016-08-08 11:38:39 +03002324 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002325 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2326 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002327 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002328 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002329 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002330 }
2331 // Make the type an array even if size check failed.
2332 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2333 arrayType.setArraySize(size);
2334
Olli Etuaho13389b62016-10-16 11:48:18 +01002335 TIntermDeclaration *declaration = new TIntermDeclaration();
2336 declaration->setLine(identifierLocation);
2337
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002338 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002339 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002340 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002341 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002342 if (initNode)
2343 {
2344 declaration->appendDeclarator(initNode);
2345 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002346 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002347
2348 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002349}
2350
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002351TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002352 const TTypeQualifierBuilder &typeQualifierBuilder,
2353 const TSourceLoc &identifierLoc,
2354 const TString *identifier,
2355 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002356{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002357 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002358
Martin Radev70866b82016-07-22 15:27:42 +03002359 if (!typeQualifier.invariant)
2360 {
2361 error(identifierLoc, "Expected invariant", identifier->c_str());
2362 return nullptr;
2363 }
2364 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2365 {
2366 return nullptr;
2367 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002368 if (!symbol)
2369 {
2370 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002371 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002372 }
Martin Radev70866b82016-07-22 15:27:42 +03002373 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002374 {
Martin Radev70866b82016-07-22 15:27:42 +03002375 error(identifierLoc, "invariant declaration specifies qualifier",
2376 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002377 }
Martin Radev70866b82016-07-22 15:27:42 +03002378 if (typeQualifier.precision != EbpUndefined)
2379 {
2380 error(identifierLoc, "invariant declaration specifies precision",
2381 getPrecisionString(typeQualifier.precision));
2382 }
2383 if (!typeQualifier.layoutQualifier.isEmpty())
2384 {
2385 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2386 }
2387
2388 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2389 ASSERT(variable);
2390 const TType &type = variable->getType();
2391
2392 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2393 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002394 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002395
2396 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2397
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002398 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2399 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002400
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002401 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002402}
2403
Olli Etuaho13389b62016-10-16 11:48:18 +01002404void TParseContext::parseDeclarator(TPublicType &publicType,
2405 const TSourceLoc &identifierLocation,
2406 const TString &identifier,
2407 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002408{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002409 // If the declaration starting this declarator list was empty (example: int,), some checks were
2410 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002411 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002412 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002413 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2414 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002415 }
2416
Olli Etuaho856c4972016-08-08 11:38:39 +03002417 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002418
Olli Etuaho856c4972016-08-08 11:38:39 +03002419 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002420
Olli Etuaho2935c582015-04-08 14:32:06 +03002421 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002422 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002423 if (IsAtomicCounter(publicType.getBasicType()))
2424 {
2425 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2426 identifierLocation, type);
2427 }
Olli Etuaho43364892017-02-13 16:00:12 +00002428 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002429
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002430 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002431 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002432 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2433 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002434 declarationOut->appendDeclarator(symbol);
2435 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002436}
2437
Olli Etuaho13389b62016-10-16 11:48:18 +01002438void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2439 const TSourceLoc &identifierLocation,
2440 const TString &identifier,
2441 const TSourceLoc &arrayLocation,
2442 TIntermTyped *indexExpression,
2443 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002444{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002445 // If the declaration starting this declarator list was empty (example: int,), some checks were
2446 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002448 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002449 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2450 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002451 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002452
Olli Etuaho856c4972016-08-08 11:38:39 +03002453 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002454
Olli Etuaho856c4972016-08-08 11:38:39 +03002455 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002456
Olli Etuaho8a176262016-08-16 14:23:01 +03002457 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002458 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002459 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002460 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002461 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002462
jchen104cdac9e2017-05-08 11:01:20 +08002463 if (IsAtomicCounter(publicType.getBasicType()))
2464 {
2465 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2466 true, identifierLocation, arrayType);
2467 }
2468
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002469 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002470 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002471
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002472 TIntermSymbol *symbol = new TIntermSymbol(0, identifier, arrayType);
2473 symbol->setLine(identifierLocation);
2474 if (variable)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002475 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002476
Olli Etuaho13389b62016-10-16 11:48:18 +01002477 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002478 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002479}
2480
Olli Etuaho13389b62016-10-16 11:48:18 +01002481void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2482 const TSourceLoc &identifierLocation,
2483 const TString &identifier,
2484 const TSourceLoc &initLocation,
2485 TIntermTyped *initializer,
2486 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002487{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002488 // If the declaration starting this declarator list was empty (example: int,), some checks were
2489 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002490 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002491 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002492 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2493 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002494 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002495
Olli Etuaho856c4972016-08-08 11:38:39 +03002496 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002497
Olli Etuaho13389b62016-10-16 11:48:18 +01002498 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002499 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002500 {
2501 //
2502 // build the intermediate representation
2503 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002504 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002505 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002507 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002508 }
2509}
2510
Olli Etuaho13389b62016-10-16 11:48:18 +01002511void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2512 const TSourceLoc &identifierLocation,
2513 const TString &identifier,
2514 const TSourceLoc &indexLocation,
2515 TIntermTyped *indexExpression,
2516 const TSourceLoc &initLocation,
2517 TIntermTyped *initializer,
2518 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002519{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002520 // If the declaration starting this declarator list was empty (example: int,), some checks were
2521 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002522 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002523 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002524 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2525 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002526 }
2527
Olli Etuaho856c4972016-08-08 11:38:39 +03002528 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002529
Olli Etuaho8a176262016-08-16 14:23:01 +03002530 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002531
2532 TPublicType arrayType(publicType);
2533
Olli Etuaho856c4972016-08-08 11:38:39 +03002534 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002535 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2536 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002537 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002538 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002539 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002540 }
2541 // Make the type an array even if size check failed.
2542 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2543 arrayType.setArraySize(size);
2544
2545 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002546 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002547 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002548 {
2549 if (initNode)
2550 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002551 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002552 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553 }
2554}
2555
jchen104cdac9e2017-05-08 11:01:20 +08002556void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2557 const TSourceLoc &location)
2558{
2559 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2560 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2561 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2562 {
2563 error(location, "Requires both binding and offset", "layout");
2564 return;
2565 }
2566 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2567}
2568
Olli Etuahocce89652017-06-19 16:04:09 +03002569void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2570 const TPublicType &type,
2571 const TSourceLoc &loc)
2572{
2573 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2574 !getFragmentPrecisionHigh())
2575 {
2576 error(loc, "precision is not supported in fragment shader", "highp");
2577 }
2578
2579 if (!CanSetDefaultPrecisionOnType(type))
2580 {
2581 error(loc, "illegal type argument for default precision qualifier",
2582 getBasicString(type.getBasicType()));
2583 return;
2584 }
2585 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2586}
2587
Martin Radev70866b82016-07-22 15:27:42 +03002588void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002589{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002590 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002591 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002592
Martin Radev70866b82016-07-22 15:27:42 +03002593 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2594 typeQualifier.line);
2595
Jamie Madillc2128ff2016-07-04 10:26:17 -04002596 // It should never be the case, but some strange parser errors can send us here.
2597 if (layoutQualifier.isEmpty())
2598 {
2599 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002600 return;
2601 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002602
Martin Radev802abe02016-08-04 17:48:32 +03002603 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002604 {
Olli Etuaho43364892017-02-13 16:00:12 +00002605 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002606 return;
2607 }
2608
Olli Etuaho43364892017-02-13 16:00:12 +00002609 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2610
2611 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002612
2613 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2614
Andrei Volykhina5527072017-03-22 16:46:30 +03002615 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2616
jchen104cdac9e2017-05-08 11:01:20 +08002617 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2618
Martin Radev802abe02016-08-04 17:48:32 +03002619 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002620 {
Martin Radev802abe02016-08-04 17:48:32 +03002621 if (mComputeShaderLocalSizeDeclared &&
2622 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2623 {
2624 error(typeQualifier.line, "Work group size does not match the previous declaration",
2625 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002626 return;
2627 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002628
Martin Radev802abe02016-08-04 17:48:32 +03002629 if (mShaderVersion < 310)
2630 {
2631 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002632 return;
2633 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002634
Martin Radev4c4c8e72016-08-04 12:25:34 +03002635 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002636 {
2637 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002638 return;
2639 }
2640
2641 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2642 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2643
2644 const TConstantUnion *maxComputeWorkGroupSizeData =
2645 maxComputeWorkGroupSize->getConstPointer();
2646
2647 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2648 {
2649 if (layoutQualifier.localSize[i] != -1)
2650 {
2651 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2652 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2653 if (mComputeShaderLocalSize[i] < 1 ||
2654 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2655 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002656 std::stringstream reasonStream;
2657 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2658 << maxComputeWorkGroupSizeValue;
2659 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002660
Olli Etuaho4de340a2016-12-16 09:32:03 +00002661 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002662 return;
2663 }
2664 }
2665 }
2666
2667 mComputeShaderLocalSizeDeclared = true;
2668 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002669 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002670 {
2671 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2672 // specification.
2673 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2674 {
2675 error(typeQualifier.line, "Number of views does not match the previous declaration",
2676 "layout");
2677 return;
2678 }
2679
2680 if (layoutQualifier.numViews == -1)
2681 {
2682 error(typeQualifier.line, "No num_views specified", "layout");
2683 return;
2684 }
2685
2686 if (layoutQualifier.numViews > mMaxNumViews)
2687 {
2688 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2689 "layout");
2690 return;
2691 }
2692
2693 mNumViews = layoutQualifier.numViews;
2694 }
Martin Radev802abe02016-08-04 17:48:32 +03002695 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002696 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002697 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002698 {
Martin Radev802abe02016-08-04 17:48:32 +03002699 return;
2700 }
2701
2702 if (typeQualifier.qualifier != EvqUniform)
2703 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002704 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2705 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002706 return;
2707 }
2708
2709 if (mShaderVersion < 300)
2710 {
2711 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2712 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002713 return;
2714 }
2715
Olli Etuaho09b04a22016-12-15 13:30:26 +00002716 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002717
2718 if (layoutQualifier.matrixPacking != EmpUnspecified)
2719 {
2720 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2721 }
2722
2723 if (layoutQualifier.blockStorage != EbsUnspecified)
2724 {
2725 mDefaultBlockStorage = layoutQualifier.blockStorage;
2726 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002727 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002728}
2729
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002730TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2731 const TFunction &function,
2732 const TSourceLoc &location,
2733 bool insertParametersToSymbolTable)
2734{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03002735 checkIsNotReserved(location, function.getName());
2736
Olli Etuahofe486322017-03-21 09:30:54 +00002737 TIntermFunctionPrototype *prototype =
2738 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002739 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2740 // point to the data that already exists in the symbol table.
2741 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2742 prototype->setLine(location);
2743
2744 for (size_t i = 0; i < function.getParamCount(); i++)
2745 {
2746 const TConstParameter &param = function.getParam(i);
2747
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002748 TIntermSymbol *symbol = nullptr;
2749
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002750 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2751 // be used for unused args).
2752 if (param.name != nullptr)
2753 {
2754 TVariable *variable = new TVariable(param.name, *param.type);
2755
2756 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002757 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002758 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002759 if (symbolTable.declare(variable))
2760 {
2761 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
2762 variable->getType());
2763 }
2764 else
2765 {
2766 error(location, "redefinition", variable->getName().c_str());
2767 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002768 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002769 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002770 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002771 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002772 // The parameter had no name or declaring the symbol failed - either way, add a nameless
2773 // symbol.
2774 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002775 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002776 symbol->setLine(location);
2777 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002778 }
2779 return prototype;
2780}
2781
Olli Etuaho16c745a2017-01-16 17:02:27 +00002782TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2783 const TFunction &parsedFunction,
2784 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002785{
Olli Etuaho476197f2016-10-11 13:59:08 +01002786 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2787 // first declaration. Either way the instance in the symbol table is used to track whether the
2788 // function is declared multiple times.
2789 TFunction *function = static_cast<TFunction *>(
2790 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2791 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002792 {
2793 // ESSL 1.00.17 section 4.2.7.
2794 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2795 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002796 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002797 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002798
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002799 TIntermFunctionPrototype *prototype =
2800 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002801
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002802 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002803
2804 if (!symbolTable.atGlobalLevel())
2805 {
2806 // ESSL 3.00.4 section 4.2.4.
2807 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002808 }
2809
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002810 return prototype;
2811}
2812
Olli Etuaho336b1472016-10-05 16:37:55 +01002813TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002814 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002815 TIntermBlock *functionBody,
2816 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002817{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002818 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002819 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2820 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002821 error(location, "function does not return a value:",
2822 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002823 }
2824
Olli Etuahof51fdd22016-10-03 10:03:40 +01002825 if (functionBody == nullptr)
2826 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002827 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002828 functionBody->setLine(location);
2829 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002830 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002831 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002832 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002833
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002834 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002835 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002836}
2837
Olli Etuaho476197f2016-10-11 13:59:08 +01002838void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2839 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002840 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002841{
Olli Etuaho476197f2016-10-11 13:59:08 +01002842 ASSERT(function);
2843 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002844 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002845 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002846
2847 if (builtIn)
2848 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002849 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002850 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002851 else
Jamie Madill185fb402015-06-12 15:48:48 -04002852 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002853 TFunction *prevDec = static_cast<TFunction *>(
2854 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2855
2856 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2857 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2858 // occurance.
2859 if (*function != prevDec)
2860 {
2861 // Swap the parameters of the previous declaration to the parameters of the function
2862 // definition (parameter names may differ).
2863 prevDec->swapParameters(**function);
2864
2865 // The function definition will share the same symbol as any previous declaration.
2866 *function = prevDec;
2867 }
2868
2869 if ((*function)->isDefined())
2870 {
2871 error(location, "function already has a body", (*function)->getName().c_str());
2872 }
2873
2874 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002875 }
Jamie Madill185fb402015-06-12 15:48:48 -04002876
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002877 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002878 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002879 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002880
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002881 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002882 setLoopNestingLevel(0);
2883}
2884
Jamie Madillb98c3a82015-07-23 14:26:04 -04002885TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002886{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002887 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002888 // We don't know at this point whether this is a function definition or a prototype.
2889 // The definition production code will check for redefinitions.
2890 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002891 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002892 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2893 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002894 //
2895 TFunction *prevDec =
2896 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302897
Martin Radevda6254b2016-12-14 17:00:36 +02002898 if (getShaderVersion() >= 300 &&
2899 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2900 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302901 {
Martin Radevda6254b2016-12-14 17:00:36 +02002902 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302903 // Therefore overloading or redefining builtin functions is an error.
2904 error(location, "Name of a built-in function cannot be redeclared as function",
2905 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302906 }
2907 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002908 {
2909 if (prevDec->getReturnType() != function->getReturnType())
2910 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002911 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002912 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002913 }
2914 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002916 if (prevDec->getParam(i).type->getQualifier() !=
2917 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002918 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002919 error(location,
2920 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002921 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002922 }
2923 }
2924 }
2925
2926 //
2927 // Check for previously declared variables using the same name.
2928 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002929 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002930 if (prevSym)
2931 {
2932 if (!prevSym->isFunction())
2933 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002934 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002935 }
2936 }
2937 else
2938 {
2939 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002940 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002941 }
2942
2943 // We're at the inner scope level of the function's arguments and body statement.
2944 // Add the function prototype to the surrounding scope instead.
2945 symbolTable.getOuterLevel()->insert(function);
2946
Olli Etuaho78d13742017-01-18 13:06:10 +00002947 // Raise error message if main function takes any parameters or return anything other than void
2948 if (function->getName() == "main")
2949 {
2950 if (function->getParamCount() > 0)
2951 {
2952 error(location, "function cannot take any parameter(s)", "main");
2953 }
2954 if (function->getReturnType().getBasicType() != EbtVoid)
2955 {
2956 error(location, "main function cannot return a value",
2957 function->getReturnType().getBasicString());
2958 }
2959 }
2960
Jamie Madill185fb402015-06-12 15:48:48 -04002961 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002962 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2963 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002964 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2965 //
2966 return function;
2967}
2968
Olli Etuaho9de84a52016-06-14 17:36:01 +03002969TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2970 const TString *name,
2971 const TSourceLoc &location)
2972{
2973 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2974 {
2975 error(location, "no qualifiers allowed for function return",
2976 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002977 }
2978 if (!type.layoutQualifier.isEmpty())
2979 {
2980 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002981 }
jchen10cc2a10e2017-05-03 14:05:12 +08002982 // make sure an opaque type is not involved as well...
2983 std::string reason(getBasicString(type.getBasicType()));
2984 reason += "s can't be function return values";
2985 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002986 if (mShaderVersion < 300)
2987 {
2988 // Array return values are forbidden, but there's also no valid syntax for declaring array
2989 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002990 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002991
2992 if (type.isStructureContainingArrays())
2993 {
2994 // ESSL 1.00.17 section 6.1 Function Definitions
2995 error(location, "structures containing arrays can't be function return values",
2996 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002997 }
2998 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002999
3000 // Add the function as a prototype after parsing it (we do not support recursion)
3001 return new TFunction(name, new TType(type));
3002}
3003
Olli Etuahocce89652017-06-19 16:04:09 +03003004TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3005{
Olli Etuahocce89652017-06-19 16:04:09 +03003006 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3007 return new TFunction(name, returnType);
3008}
3009
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003010TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003011{
Olli Etuahocce89652017-06-19 16:04:09 +03003012 if (mShaderVersion < 300 && publicType.array)
3013 {
3014 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3015 "[]");
3016 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003017 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003018 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003019 error(publicType.getLine(), "constructor can't be a structure definition",
3020 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003021 }
3022
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003023 TType *type = new TType(publicType);
3024 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003025 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003026 error(publicType.getLine(), "cannot construct this type",
3027 getBasicString(publicType.getBasicType()));
3028 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003029 }
3030
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003031 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003032}
3033
Olli Etuahocce89652017-06-19 16:04:09 +03003034TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3035 const TString *name,
3036 const TSourceLoc &nameLoc)
3037{
3038 if (publicType.getBasicType() == EbtVoid)
3039 {
3040 error(nameLoc, "illegal use of type 'void'", name->c_str());
3041 }
3042 checkIsNotReserved(nameLoc, *name);
3043 TType *type = new TType(publicType);
3044 TParameter param = {name, type};
3045 return param;
3046}
3047
3048TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3049 const TSourceLoc &identifierLoc,
3050 TIntermTyped *arraySize,
3051 const TSourceLoc &arrayLoc,
3052 TPublicType *type)
3053{
3054 checkIsValidTypeForArray(arrayLoc, *type);
3055 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3056 type->setArraySize(size);
3057 return parseParameterDeclarator(*type, identifier, identifierLoc);
3058}
3059
Jamie Madillb98c3a82015-07-23 14:26:04 -04003060// This function is used to test for the correctness of the parameters passed to various constructor
3061// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003062//
Olli Etuaho856c4972016-08-08 11:38:39 +03003063// 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 +00003064//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003065TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003066 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303067 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003068{
Olli Etuaho856c4972016-08-08 11:38:39 +03003069 if (type.isUnsizedArray())
3070 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003071 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003072 {
3073 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3074 type.setArraySize(1u);
3075 return TIntermTyped::CreateZero(type);
3076 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003077 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003078 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003079
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003080 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003081 {
Olli Etuaho72d10202017-01-19 15:58:30 +00003082 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003083 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003084
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003085 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003086 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003087
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003088 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3089 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003090 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003091 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003092 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003093 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003094}
3095
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003096//
3097// Interface/uniform blocks
3098//
Olli Etuaho13389b62016-10-16 11:48:18 +01003099TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003100 const TTypeQualifierBuilder &typeQualifierBuilder,
3101 const TSourceLoc &nameLine,
3102 const TString &blockName,
3103 TFieldList *fieldList,
3104 const TString *instanceName,
3105 const TSourceLoc &instanceLine,
3106 TIntermTyped *arrayIndex,
3107 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003108{
Olli Etuaho856c4972016-08-08 11:38:39 +03003109 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003110
Olli Etuaho77ba4082016-12-16 12:01:18 +00003111 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003112
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003113 if (typeQualifier.qualifier != EvqUniform)
3114 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003115 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3116 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003117 }
3118
Martin Radev70866b82016-07-22 15:27:42 +03003119 if (typeQualifier.invariant)
3120 {
3121 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3122 }
3123
Olli Etuaho43364892017-02-13 16:00:12 +00003124 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3125
jchen10af713a22017-04-19 09:10:56 +08003126 // add array index
3127 unsigned int arraySize = 0;
3128 if (arrayIndex != nullptr)
3129 {
3130 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3131 }
3132
3133 if (mShaderVersion < 310)
3134 {
3135 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3136 }
3137 else
3138 {
3139 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3140 arraySize);
3141 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003142
Andrei Volykhina5527072017-03-22 16:46:30 +03003143 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3144
Jamie Madill099c0f32013-06-20 11:55:52 -04003145 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003146 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003147
Jamie Madill099c0f32013-06-20 11:55:52 -04003148 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3149 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003150 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003151 }
3152
Jamie Madill1566ef72013-06-20 11:55:54 -04003153 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3154 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003155 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003156 }
3157
Olli Etuaho856c4972016-08-08 11:38:39 +03003158 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003159
Martin Radev2cc85b32016-08-05 16:22:53 +03003160 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3161
Arun Patole7e7e68d2015-05-22 12:02:25 +05303162 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3163 if (!symbolTable.declare(blockNameSymbol))
3164 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003165 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003166 }
3167
Jamie Madill98493dd2013-07-08 14:39:03 -04003168 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303169 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3170 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003171 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303172 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003173 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303174 {
jchen10cc2a10e2017-05-03 14:05:12 +08003175 std::string reason("unsupported type - ");
3176 reason += fieldType->getBasicString();
3177 reason += " types are not allowed in interface blocks";
3178 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003179 }
3180
Jamie Madill98493dd2013-07-08 14:39:03 -04003181 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003182 switch (qualifier)
3183 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003184 case EvqGlobal:
3185 case EvqUniform:
3186 break;
3187 default:
3188 error(field->line(), "invalid qualifier on interface block member",
3189 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003190 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003191 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003192
Martin Radev70866b82016-07-22 15:27:42 +03003193 if (fieldType->isInvariant())
3194 {
3195 error(field->line(), "invalid qualifier on interface block member", "invariant");
3196 }
3197
Jamie Madilla5efff92013-06-06 11:56:47 -04003198 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003199 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003200 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003201 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003202
Jamie Madill98493dd2013-07-08 14:39:03 -04003203 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003204 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003205 error(field->line(), "invalid layout qualifier: cannot be used here",
3206 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003207 }
3208
Jamie Madill98493dd2013-07-08 14:39:03 -04003209 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003210 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003211 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003212 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003213 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003214 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003215 warning(field->line(),
3216 "extraneous layout qualifier: only has an effect on matrix types",
3217 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003218 }
3219
Jamie Madill98493dd2013-07-08 14:39:03 -04003220 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003221 }
3222
Jamie Madillb98c3a82015-07-23 14:26:04 -04003223 TInterfaceBlock *interfaceBlock =
3224 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3225 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3226 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003227
3228 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003229 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003230
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003232 {
3233 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003234 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3235 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003236 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303237 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003238
3239 // set parent pointer of the field variable
3240 fieldType->setInterfaceBlock(interfaceBlock);
3241
Arun Patole7e7e68d2015-05-22 12:02:25 +05303242 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 fieldVariable->setQualifier(typeQualifier.qualifier);
3244
Arun Patole7e7e68d2015-05-22 12:02:25 +05303245 if (!symbolTable.declare(fieldVariable))
3246 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003247 error(field->line(), "redefinition of an interface block member name",
3248 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003249 }
3250 }
3251 }
3252 else
3253 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003254 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003255
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003256 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303257 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003258 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003259
Arun Patole7e7e68d2015-05-22 12:02:25 +05303260 if (!symbolTable.declare(instanceTypeDef))
3261 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003262 error(instanceLine, "redefinition of an interface block instance name",
3263 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003264 }
3265
Jamie Madillb98c3a82015-07-23 14:26:04 -04003266 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003267 symbolName = instanceTypeDef->getName();
3268 }
3269
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003270 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3271 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003272 TIntermDeclaration *declaration = new TIntermDeclaration();
3273 declaration->appendDeclarator(blockSymbol);
3274 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003275
3276 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003277 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003278}
3279
Olli Etuaho383b7912016-08-05 11:22:59 +03003280void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003281{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003282 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003283
3284 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003285 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303286 if (mStructNestingLevel > 1)
3287 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003288 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003289 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003290}
3291
3292void TParseContext::exitStructDeclaration()
3293{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003294 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003295}
3296
Olli Etuaho8a176262016-08-16 14:23:01 +03003297void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003298{
Jamie Madillacb4b812016-11-07 13:50:29 -05003299 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303300 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003301 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003302 }
3303
Arun Patole7e7e68d2015-05-22 12:02:25 +05303304 if (field.type()->getBasicType() != EbtStruct)
3305 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003306 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003307 }
3308
3309 // We're already inside a structure definition at this point, so add
3310 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303311 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3312 {
Jamie Madill41a49272014-03-18 16:10:13 -04003313 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003314 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3315 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003316 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003317 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003318 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003319 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003320}
3321
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003322//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003323// Parse an array index expression
3324//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003325TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3326 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303327 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003328{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003329 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3330 {
3331 if (baseExpression->getAsSymbolNode())
3332 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303333 error(location, " left of '[' is not of type array, matrix, or vector ",
3334 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003335 }
3336 else
3337 {
3338 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3339 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003340
3341 TConstantUnion *unionArray = new TConstantUnion[1];
3342 unionArray->setFConst(0.0f);
3343 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3344 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003345 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003346
Jamie Madill21c1e452014-12-29 11:33:41 -05003347 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3348
Olli Etuaho36b05142015-11-12 13:10:42 +02003349 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3350 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3351 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3352 // index is a constant expression.
3353 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3354 {
3355 if (baseExpression->isInterfaceBlock())
3356 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003357 error(location,
3358 "array indexes for interface blocks arrays must be constant integral expressions",
3359 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003360 }
3361 else if (baseExpression->getQualifier() == EvqFragmentOut)
3362 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003363 error(location,
3364 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003365 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003366 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3367 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003368 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003369 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003370 }
3371
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003372 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003373 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003374 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3375 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3376 // constant fold expressions that are not constant expressions). The most compatible way to
3377 // handle this case is to report a warning instead of an error and force the index to be in
3378 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003379 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003380 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003381
3382 int safeIndex = -1;
3383
3384 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003385 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003386 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003387 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003388 if (mShaderSpec == SH_WEBGL2_SPEC)
3389 {
3390 // Error has been already generated if index is not const.
3391 if (indexExpression->getQualifier() == EvqConst)
3392 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003393 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003394 }
3395 safeIndex = 0;
3396 }
3397 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3398 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003399 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003400 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003401 "GL_EXT_draw_buffers is disabled",
3402 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003403 safeIndex = 0;
3404 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003405 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003406 // Only do generic out-of-range check if similar error hasn't already been reported.
3407 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003408 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003409 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3410 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003411 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003412 }
3413 }
3414 else if (baseExpression->isMatrix())
3415 {
3416 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003417 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003418 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003419 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003420 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003421 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003422 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3423 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003424 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003425 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003426
3427 ASSERT(safeIndex >= 0);
3428 // Data of constant unions can't be changed, because it may be shared with other
3429 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3430 // sanitized object.
3431 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003433 TConstantUnion *safeConstantUnion = new TConstantUnion();
3434 safeConstantUnion->setIConst(safeIndex);
3435 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003436 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003437
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003438 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3439 node->setLine(location);
3440 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003441 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003442 else
3443 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003444 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3445 node->setLine(location);
3446 // Indirect indexing can never be constant folded.
3447 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003448 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449}
3450
Olli Etuaho90892fb2016-07-14 14:44:51 +03003451int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3452 const TSourceLoc &location,
3453 int index,
3454 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003455 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003456{
3457 if (index >= arraySize || index < 0)
3458 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003459 std::stringstream reasonStream;
3460 reasonStream << reason << " '" << index << "'";
3461 std::string token = reasonStream.str();
3462 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003463 if (index < 0)
3464 {
3465 return 0;
3466 }
3467 else
3468 {
3469 return arraySize - 1;
3470 }
3471 }
3472 return index;
3473}
3474
Jamie Madillb98c3a82015-07-23 14:26:04 -04003475TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3476 const TSourceLoc &dotLocation,
3477 const TString &fieldString,
3478 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003479{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003480 if (baseExpression->isArray())
3481 {
3482 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003483 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003484 }
3485
3486 if (baseExpression->isVector())
3487 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003488 TVector<int> fieldOffsets;
3489 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3490 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003491 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003492 fieldOffsets.resize(1);
3493 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003494 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003495 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3496 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003497
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003498 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003499 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003500 else if (baseExpression->getBasicType() == EbtStruct)
3501 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303502 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003503 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003504 {
3505 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003506 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003507 }
3508 else
3509 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003510 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003511 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003512 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003513 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003514 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003515 {
3516 fieldFound = true;
3517 break;
3518 }
3519 }
3520 if (fieldFound)
3521 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003522 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3523 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003524 TIntermBinary *node =
3525 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3526 node->setLine(dotLocation);
3527 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003528 }
3529 else
3530 {
3531 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003532 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003533 }
3534 }
3535 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003536 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003537 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303538 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003539 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003540 {
3541 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003542 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003543 }
3544 else
3545 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003546 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003547 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003548 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003549 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003550 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003551 {
3552 fieldFound = true;
3553 break;
3554 }
3555 }
3556 if (fieldFound)
3557 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003558 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3559 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003560 TIntermBinary *node =
3561 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3562 node->setLine(dotLocation);
3563 // Indexing interface blocks can never be constant folded.
3564 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003565 }
3566 else
3567 {
3568 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003569 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003570 }
3571 }
3572 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003573 else
3574 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003575 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003576 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003577 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303578 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003579 }
3580 else
3581 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303582 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003583 " field selection requires structure, vector, or interface block on left hand "
3584 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303585 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003586 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003587 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003588 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003589}
3590
Jamie Madillb98c3a82015-07-23 14:26:04 -04003591TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3592 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003593{
Martin Radev802abe02016-08-04 17:48:32 +03003594 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003595
3596 if (qualifierType == "shared")
3597 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003598 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003599 {
3600 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3601 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003602 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003603 }
3604 else if (qualifierType == "packed")
3605 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003606 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003607 {
3608 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3609 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003610 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003611 }
3612 else if (qualifierType == "std140")
3613 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003614 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003615 }
3616 else if (qualifierType == "row_major")
3617 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003618 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003619 }
3620 else if (qualifierType == "column_major")
3621 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003622 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003623 }
3624 else if (qualifierType == "location")
3625 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003626 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3627 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003628 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003629 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3630 mShaderType == GL_FRAGMENT_SHADER)
3631 {
3632 qualifier.yuv = true;
3633 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003634 else if (qualifierType == "rgba32f")
3635 {
3636 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3637 qualifier.imageInternalFormat = EiifRGBA32F;
3638 }
3639 else if (qualifierType == "rgba16f")
3640 {
3641 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3642 qualifier.imageInternalFormat = EiifRGBA16F;
3643 }
3644 else if (qualifierType == "r32f")
3645 {
3646 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3647 qualifier.imageInternalFormat = EiifR32F;
3648 }
3649 else if (qualifierType == "rgba8")
3650 {
3651 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3652 qualifier.imageInternalFormat = EiifRGBA8;
3653 }
3654 else if (qualifierType == "rgba8_snorm")
3655 {
3656 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3657 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3658 }
3659 else if (qualifierType == "rgba32i")
3660 {
3661 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3662 qualifier.imageInternalFormat = EiifRGBA32I;
3663 }
3664 else if (qualifierType == "rgba16i")
3665 {
3666 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3667 qualifier.imageInternalFormat = EiifRGBA16I;
3668 }
3669 else if (qualifierType == "rgba8i")
3670 {
3671 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3672 qualifier.imageInternalFormat = EiifRGBA8I;
3673 }
3674 else if (qualifierType == "r32i")
3675 {
3676 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3677 qualifier.imageInternalFormat = EiifR32I;
3678 }
3679 else if (qualifierType == "rgba32ui")
3680 {
3681 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3682 qualifier.imageInternalFormat = EiifRGBA32UI;
3683 }
3684 else if (qualifierType == "rgba16ui")
3685 {
3686 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3687 qualifier.imageInternalFormat = EiifRGBA16UI;
3688 }
3689 else if (qualifierType == "rgba8ui")
3690 {
3691 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3692 qualifier.imageInternalFormat = EiifRGBA8UI;
3693 }
3694 else if (qualifierType == "r32ui")
3695 {
3696 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3697 qualifier.imageInternalFormat = EiifR32UI;
3698 }
3699
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003700 else
3701 {
3702 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003703 }
3704
Jamie Madilla5efff92013-06-06 11:56:47 -04003705 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003706}
3707
Martin Radev802abe02016-08-04 17:48:32 +03003708void TParseContext::parseLocalSize(const TString &qualifierType,
3709 const TSourceLoc &qualifierTypeLine,
3710 int intValue,
3711 const TSourceLoc &intValueLine,
3712 const std::string &intValueString,
3713 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003714 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003715{
Olli Etuaho856c4972016-08-08 11:38:39 +03003716 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003717 if (intValue < 1)
3718 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003719 std::stringstream reasonStream;
3720 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3721 std::string reason = reasonStream.str();
3722 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003723 }
3724 (*localSize)[index] = intValue;
3725}
3726
Olli Etuaho09b04a22016-12-15 13:30:26 +00003727void TParseContext::parseNumViews(int intValue,
3728 const TSourceLoc &intValueLine,
3729 const std::string &intValueString,
3730 int *numViews)
3731{
3732 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3733 // specification.
3734 if (intValue < 1)
3735 {
3736 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3737 }
3738 *numViews = intValue;
3739}
3740
Jamie Madillb98c3a82015-07-23 14:26:04 -04003741TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3742 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303744 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003745{
Martin Radev802abe02016-08-04 17:48:32 +03003746 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003747
Martin Radev802abe02016-08-04 17:48:32 +03003748 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003749
Martin Radev802abe02016-08-04 17:48:32 +03003750 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003751 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003752 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003753 if (intValue < 0)
3754 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003755 error(intValueLine, "out of range: location must be non-negative",
3756 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003757 }
3758 else
3759 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003760 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003761 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003762 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003763 }
Olli Etuaho43364892017-02-13 16:00:12 +00003764 else if (qualifierType == "binding")
3765 {
3766 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3767 if (intValue < 0)
3768 {
3769 error(intValueLine, "out of range: binding must be non-negative",
3770 intValueString.c_str());
3771 }
3772 else
3773 {
3774 qualifier.binding = intValue;
3775 }
3776 }
jchen104cdac9e2017-05-08 11:01:20 +08003777 else if (qualifierType == "offset")
3778 {
3779 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3780 if (intValue < 0)
3781 {
3782 error(intValueLine, "out of range: offset must be non-negative",
3783 intValueString.c_str());
3784 }
3785 else
3786 {
3787 qualifier.offset = intValue;
3788 }
3789 }
Martin Radev802abe02016-08-04 17:48:32 +03003790 else if (qualifierType == "local_size_x")
3791 {
3792 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3793 &qualifier.localSize);
3794 }
3795 else if (qualifierType == "local_size_y")
3796 {
3797 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3798 &qualifier.localSize);
3799 }
3800 else if (qualifierType == "local_size_z")
3801 {
3802 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3803 &qualifier.localSize);
3804 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003805 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003806 mShaderType == GL_VERTEX_SHADER)
3807 {
3808 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3809 }
Martin Radev802abe02016-08-04 17:48:32 +03003810 else
3811 {
3812 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003813 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003814
Jamie Madilla5efff92013-06-06 11:56:47 -04003815 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003816}
3817
Olli Etuaho613b9592016-09-05 12:05:53 +03003818TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3819{
3820 return new TTypeQualifierBuilder(
3821 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3822 mShaderVersion);
3823}
3824
Olli Etuahocce89652017-06-19 16:04:09 +03003825TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3826 const TSourceLoc &loc)
3827{
3828 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3829 return new TStorageQualifierWrapper(qualifier, loc);
3830}
3831
3832TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3833{
3834 if (getShaderType() == GL_VERTEX_SHADER)
3835 {
3836 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3837 }
3838 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3839}
3840
3841TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3842{
3843 if (declaringFunction())
3844 {
3845 return new TStorageQualifierWrapper(EvqIn, loc);
3846 }
3847 if (getShaderType() == GL_FRAGMENT_SHADER)
3848 {
3849 if (mShaderVersion < 300)
3850 {
3851 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3852 }
3853 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3854 }
3855 if (getShaderType() == GL_VERTEX_SHADER)
3856 {
3857 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3858 {
3859 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3860 }
3861 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3862 }
3863 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3864}
3865
3866TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3867{
3868 if (declaringFunction())
3869 {
3870 return new TStorageQualifierWrapper(EvqOut, loc);
3871 }
3872 if (mShaderVersion < 300)
3873 {
3874 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3875 }
3876 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3877 {
3878 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3879 }
3880 if (getShaderType() == GL_VERTEX_SHADER)
3881 {
3882 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3883 }
3884 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3885}
3886
3887TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3888{
3889 if (!declaringFunction())
3890 {
3891 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3892 }
3893 return new TStorageQualifierWrapper(EvqInOut, loc);
3894}
3895
Jamie Madillb98c3a82015-07-23 14:26:04 -04003896TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003897 TLayoutQualifier rightQualifier,
3898 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003899{
Martin Radevc28888b2016-07-22 15:27:42 +03003900 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003901 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003902}
3903
Olli Etuahocce89652017-06-19 16:04:09 +03003904TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3905{
3906 checkIsNotReserved(loc, *identifier);
3907 TType *type = new TType(EbtVoid, EbpUndefined);
3908 return new TField(type, identifier, loc);
3909}
3910
3911TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3912 const TSourceLoc &loc,
3913 TIntermTyped *arraySize,
3914 const TSourceLoc &arraySizeLoc)
3915{
3916 checkIsNotReserved(loc, *identifier);
3917
3918 TType *type = new TType(EbtVoid, EbpUndefined);
3919 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3920 type->setArraySize(size);
3921
3922 return new TField(type, identifier, loc);
3923}
3924
Olli Etuaho4de340a2016-12-16 09:32:03 +00003925TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3926 const TFieldList *newlyAddedFields,
3927 const TSourceLoc &location)
3928{
3929 for (TField *field : *newlyAddedFields)
3930 {
3931 for (TField *oldField : *processedFields)
3932 {
3933 if (oldField->name() == field->name())
3934 {
3935 error(location, "duplicate field name in structure", field->name().c_str());
3936 }
3937 }
3938 processedFields->push_back(field);
3939 }
3940 return processedFields;
3941}
3942
Martin Radev70866b82016-07-22 15:27:42 +03003943TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3944 const TTypeQualifierBuilder &typeQualifierBuilder,
3945 TPublicType *typeSpecifier,
3946 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003947{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003948 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003949
Martin Radev70866b82016-07-22 15:27:42 +03003950 typeSpecifier->qualifier = typeQualifier.qualifier;
3951 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003952 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003953 typeSpecifier->invariant = typeQualifier.invariant;
3954 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303955 {
Martin Radev70866b82016-07-22 15:27:42 +03003956 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003957 }
Martin Radev70866b82016-07-22 15:27:42 +03003958 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003959}
3960
Jamie Madillb98c3a82015-07-23 14:26:04 -04003961TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3962 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003963{
Martin Radev4a9cd802016-09-01 16:51:51 +03003964 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3965 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003966
Martin Radev4a9cd802016-09-01 16:51:51 +03003967 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003968
Martin Radev4a9cd802016-09-01 16:51:51 +03003969 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003970
Arun Patole7e7e68d2015-05-22 12:02:25 +05303971 for (unsigned int i = 0; i < fieldList->size(); ++i)
3972 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003973 //
3974 // Careful not to replace already known aspects of type, like array-ness
3975 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303976 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003977 type->setBasicType(typeSpecifier.getBasicType());
3978 type->setPrimarySize(typeSpecifier.getPrimarySize());
3979 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003980 type->setPrecision(typeSpecifier.precision);
3981 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003982 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003983 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003984 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003985
3986 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303987 if (type->isArray())
3988 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003989 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003990 }
3991 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003992 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003993 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303994 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003995 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003996 }
3997
Martin Radev4a9cd802016-09-01 16:51:51 +03003998 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003999 }
4000
Jamie Madill98493dd2013-07-08 14:39:03 -04004001 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004002}
4003
Martin Radev4a9cd802016-09-01 16:51:51 +03004004TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4005 const TSourceLoc &nameLine,
4006 const TString *structName,
4007 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004008{
Arun Patole7e7e68d2015-05-22 12:02:25 +05304009 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004010 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004011
Jamie Madill9b820842015-02-12 10:40:10 -05004012 // Store a bool in the struct if we're at global scope, to allow us to
4013 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004014 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004015
Jamie Madill98493dd2013-07-08 14:39:03 -04004016 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004018 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304019 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4020 if (!symbolTable.declare(userTypeDef))
4021 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004022 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004023 }
4024 }
4025
4026 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004027 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004028 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004029 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004030 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004031 switch (qualifier)
4032 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004033 case EvqGlobal:
4034 case EvqTemporary:
4035 break;
4036 default:
4037 error(field.line(), "invalid qualifier on struct member",
4038 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004039 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004040 }
Martin Radev70866b82016-07-22 15:27:42 +03004041 if (field.type()->isInvariant())
4042 {
4043 error(field.line(), "invalid qualifier on struct member", "invariant");
4044 }
jchen104cdac9e2017-05-08 11:01:20 +08004045 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4046 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004047 {
4048 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4049 }
4050
Olli Etuaho43364892017-02-13 16:00:12 +00004051 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4052
4053 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004054
4055 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004056 }
4057
Martin Radev4a9cd802016-09-01 16:51:51 +03004058 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004059 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004060 exitStructDeclaration();
4061
Martin Radev4a9cd802016-09-01 16:51:51 +03004062 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004063}
4064
Jamie Madillb98c3a82015-07-23 14:26:04 -04004065TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004066 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004067 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004068{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004069 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004070 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004071 init->isVector())
4072 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004073 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4074 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004075 return nullptr;
4076 }
4077
Olli Etuahoac5274d2015-02-20 10:19:08 +02004078 if (statementList)
4079 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004080 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004081 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004082 return nullptr;
4083 }
4084 }
4085
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004086 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4087 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004088 return node;
4089}
4090
4091TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4092{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004093 if (mSwitchNestingLevel == 0)
4094 {
4095 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004096 return nullptr;
4097 }
4098 if (condition == nullptr)
4099 {
4100 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004101 return nullptr;
4102 }
4103 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004104 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004105 {
4106 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004107 }
4108 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004109 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4110 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4111 // fold in case labels.
4112 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004113 {
4114 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004115 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004116 TIntermCase *node = new TIntermCase(condition);
4117 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004118 return node;
4119}
4120
4121TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4122{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004123 if (mSwitchNestingLevel == 0)
4124 {
4125 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004126 return nullptr;
4127 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004128 TIntermCase *node = new TIntermCase(nullptr);
4129 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004130 return node;
4131}
4132
Jamie Madillb98c3a82015-07-23 14:26:04 -04004133TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4134 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004135 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004136{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004137 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004138
4139 switch (op)
4140 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004141 case EOpLogicalNot:
4142 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4143 child->isVector())
4144 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004145 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004146 return nullptr;
4147 }
4148 break;
4149 case EOpBitwiseNot:
4150 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4151 child->isMatrix() || child->isArray())
4152 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004153 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004154 return nullptr;
4155 }
4156 break;
4157 case EOpPostIncrement:
4158 case EOpPreIncrement:
4159 case EOpPostDecrement:
4160 case EOpPreDecrement:
4161 case EOpNegative:
4162 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004163 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4164 child->getBasicType() == EbtBool || child->isArray() ||
4165 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004167 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004168 return nullptr;
4169 }
4170 // Operators for built-ins are already type checked against their prototype.
4171 default:
4172 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004173 }
4174
Olli Etuahof119a262016-08-19 15:54:22 +03004175 TIntermUnary *node = new TIntermUnary(op, child);
4176 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004177
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004178 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004179}
4180
Olli Etuaho09b22472015-02-11 11:47:26 +02004181TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4182{
Olli Etuahocce89652017-06-19 16:04:09 +03004183 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004184 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004185 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004186 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004187 return child;
4188 }
4189 return node;
4190}
4191
Jamie Madillb98c3a82015-07-23 14:26:04 -04004192TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4193 TIntermTyped *child,
4194 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004195{
Olli Etuaho856c4972016-08-08 11:38:39 +03004196 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004197 return addUnaryMath(op, child, loc);
4198}
4199
Jamie Madillb98c3a82015-07-23 14:26:04 -04004200bool TParseContext::binaryOpCommonCheck(TOperator op,
4201 TIntermTyped *left,
4202 TIntermTyped *right,
4203 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004204{
jchen10b4cf5652017-05-05 18:51:17 +08004205 // Check opaque types are not allowed to be operands in expressions other than array indexing
4206 // and structure member selection.
4207 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4208 {
4209 switch (op)
4210 {
4211 case EOpIndexDirect:
4212 case EOpIndexIndirect:
4213 break;
4214 case EOpIndexDirectStruct:
4215 UNREACHABLE();
4216
4217 default:
4218 error(loc, "Invalid operation for variables with an opaque type",
4219 GetOperatorString(op));
4220 return false;
4221 }
4222 }
jchen10cc2a10e2017-05-03 14:05:12 +08004223
Olli Etuaho244be012016-08-18 15:26:02 +03004224 if (left->getType().getStruct() || right->getType().getStruct())
4225 {
4226 switch (op)
4227 {
4228 case EOpIndexDirectStruct:
4229 ASSERT(left->getType().getStruct());
4230 break;
4231 case EOpEqual:
4232 case EOpNotEqual:
4233 case EOpAssign:
4234 case EOpInitialize:
4235 if (left->getType() != right->getType())
4236 {
4237 return false;
4238 }
4239 break;
4240 default:
4241 error(loc, "Invalid operation for structs", GetOperatorString(op));
4242 return false;
4243 }
4244 }
4245
Olli Etuaho94050052017-05-08 14:17:44 +03004246 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4247 {
4248 switch (op)
4249 {
4250 case EOpIndexDirectInterfaceBlock:
4251 ASSERT(left->getType().getInterfaceBlock());
4252 break;
4253 default:
4254 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4255 return false;
4256 }
4257 }
4258
Olli Etuahod6b14282015-03-17 14:31:35 +02004259 if (left->isArray() || right->isArray())
4260 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004261 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004262 {
4263 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4264 return false;
4265 }
4266
4267 if (left->isArray() != right->isArray())
4268 {
4269 error(loc, "array / non-array mismatch", GetOperatorString(op));
4270 return false;
4271 }
4272
4273 switch (op)
4274 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004275 case EOpEqual:
4276 case EOpNotEqual:
4277 case EOpAssign:
4278 case EOpInitialize:
4279 break;
4280 default:
4281 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4282 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004283 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004284 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004285 if (left->getArraySize() != right->getArraySize())
4286 {
4287 error(loc, "array size mismatch", GetOperatorString(op));
4288 return false;
4289 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004290 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004291
4292 // Check ops which require integer / ivec parameters
4293 bool isBitShift = false;
4294 switch (op)
4295 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004296 case EOpBitShiftLeft:
4297 case EOpBitShiftRight:
4298 case EOpBitShiftLeftAssign:
4299 case EOpBitShiftRightAssign:
4300 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4301 // check that the basic type is an integer type.
4302 isBitShift = true;
4303 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4304 {
4305 return false;
4306 }
4307 break;
4308 case EOpBitwiseAnd:
4309 case EOpBitwiseXor:
4310 case EOpBitwiseOr:
4311 case EOpBitwiseAndAssign:
4312 case EOpBitwiseXorAssign:
4313 case EOpBitwiseOrAssign:
4314 // It is enough to check the type of only one operand, since later it
4315 // is checked that the operand types match.
4316 if (!IsInteger(left->getBasicType()))
4317 {
4318 return false;
4319 }
4320 break;
4321 default:
4322 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004323 }
4324
4325 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4326 // So the basic type should usually match.
4327 if (!isBitShift && left->getBasicType() != right->getBasicType())
4328 {
4329 return false;
4330 }
4331
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004332 // Check that:
4333 // 1. Type sizes match exactly on ops that require that.
4334 // 2. Restrictions for structs that contain arrays or samplers are respected.
4335 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004336 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004338 case EOpAssign:
4339 case EOpInitialize:
4340 case EOpEqual:
4341 case EOpNotEqual:
4342 // ESSL 1.00 sections 5.7, 5.8, 5.9
4343 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4344 {
4345 error(loc, "undefined operation for structs containing arrays",
4346 GetOperatorString(op));
4347 return false;
4348 }
4349 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4350 // we interpret the spec so that this extends to structs containing samplers,
4351 // similarly to ESSL 1.00 spec.
4352 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4353 left->getType().isStructureContainingSamplers())
4354 {
4355 error(loc, "undefined operation for structs containing samplers",
4356 GetOperatorString(op));
4357 return false;
4358 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004359
Olli Etuahoe1805592017-01-02 16:41:20 +00004360 if ((left->getNominalSize() != right->getNominalSize()) ||
4361 (left->getSecondarySize() != right->getSecondarySize()))
4362 {
4363 error(loc, "dimension mismatch", GetOperatorString(op));
4364 return false;
4365 }
4366 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004367 case EOpLessThan:
4368 case EOpGreaterThan:
4369 case EOpLessThanEqual:
4370 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004371 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004372 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004373 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004374 return false;
4375 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004376 break;
4377 case EOpAdd:
4378 case EOpSub:
4379 case EOpDiv:
4380 case EOpIMod:
4381 case EOpBitShiftLeft:
4382 case EOpBitShiftRight:
4383 case EOpBitwiseAnd:
4384 case EOpBitwiseXor:
4385 case EOpBitwiseOr:
4386 case EOpAddAssign:
4387 case EOpSubAssign:
4388 case EOpDivAssign:
4389 case EOpIModAssign:
4390 case EOpBitShiftLeftAssign:
4391 case EOpBitShiftRightAssign:
4392 case EOpBitwiseAndAssign:
4393 case EOpBitwiseXorAssign:
4394 case EOpBitwiseOrAssign:
4395 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4396 {
4397 return false;
4398 }
4399
4400 // Are the sizes compatible?
4401 if (left->getNominalSize() != right->getNominalSize() ||
4402 left->getSecondarySize() != right->getSecondarySize())
4403 {
4404 // If the nominal sizes of operands do not match:
4405 // One of them must be a scalar.
4406 if (!left->isScalar() && !right->isScalar())
4407 return false;
4408
4409 // In the case of compound assignment other than multiply-assign,
4410 // the right side needs to be a scalar. Otherwise a vector/matrix
4411 // would be assigned to a scalar. A scalar can't be shifted by a
4412 // vector either.
4413 if (!right->isScalar() &&
4414 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4415 return false;
4416 }
4417 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004418 default:
4419 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004420 }
4421
Olli Etuahod6b14282015-03-17 14:31:35 +02004422 return true;
4423}
4424
Olli Etuaho1dded802016-08-18 18:13:13 +03004425bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4426 const TType &left,
4427 const TType &right)
4428{
4429 switch (op)
4430 {
4431 case EOpMul:
4432 case EOpMulAssign:
4433 return left.getNominalSize() == right.getNominalSize() &&
4434 left.getSecondarySize() == right.getSecondarySize();
4435 case EOpVectorTimesScalar:
4436 return true;
4437 case EOpVectorTimesScalarAssign:
4438 ASSERT(!left.isMatrix() && !right.isMatrix());
4439 return left.isVector() && !right.isVector();
4440 case EOpVectorTimesMatrix:
4441 return left.getNominalSize() == right.getRows();
4442 case EOpVectorTimesMatrixAssign:
4443 ASSERT(!left.isMatrix() && right.isMatrix());
4444 return left.isVector() && left.getNominalSize() == right.getRows() &&
4445 left.getNominalSize() == right.getCols();
4446 case EOpMatrixTimesVector:
4447 return left.getCols() == right.getNominalSize();
4448 case EOpMatrixTimesScalar:
4449 return true;
4450 case EOpMatrixTimesScalarAssign:
4451 ASSERT(left.isMatrix() && !right.isMatrix());
4452 return !right.isVector();
4453 case EOpMatrixTimesMatrix:
4454 return left.getCols() == right.getRows();
4455 case EOpMatrixTimesMatrixAssign:
4456 ASSERT(left.isMatrix() && right.isMatrix());
4457 // We need to check two things:
4458 // 1. The matrix multiplication step is valid.
4459 // 2. The result will have the same number of columns as the lvalue.
4460 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4461
4462 default:
4463 UNREACHABLE();
4464 return false;
4465 }
4466}
4467
Jamie Madillb98c3a82015-07-23 14:26:04 -04004468TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4469 TIntermTyped *left,
4470 TIntermTyped *right,
4471 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004472{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004473 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004474 return nullptr;
4475
Olli Etuahofc1806e2015-03-17 13:03:11 +02004476 switch (op)
4477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004478 case EOpEqual:
4479 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004480 case EOpLessThan:
4481 case EOpGreaterThan:
4482 case EOpLessThanEqual:
4483 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004484 break;
4485 case EOpLogicalOr:
4486 case EOpLogicalXor:
4487 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004488 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4489 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004490 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004491 {
4492 return nullptr;
4493 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004494 // Basic types matching should have been already checked.
4495 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004496 break;
4497 case EOpAdd:
4498 case EOpSub:
4499 case EOpDiv:
4500 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004501 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4502 !right->getType().getStruct());
4503 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004504 {
4505 return nullptr;
4506 }
4507 break;
4508 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004509 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4510 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004511 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004512 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004513 {
4514 return nullptr;
4515 }
4516 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004517 default:
4518 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004519 }
4520
Olli Etuaho1dded802016-08-18 18:13:13 +03004521 if (op == EOpMul)
4522 {
4523 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4524 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4525 {
4526 return nullptr;
4527 }
4528 }
4529
Olli Etuaho3fdec912016-08-18 15:08:06 +03004530 TIntermBinary *node = new TIntermBinary(op, left, right);
4531 node->setLine(loc);
4532
Olli Etuaho3fdec912016-08-18 15:08:06 +03004533 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004534 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02004535}
4536
Jamie Madillb98c3a82015-07-23 14:26:04 -04004537TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4538 TIntermTyped *left,
4539 TIntermTyped *right,
4540 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004541{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004542 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004543 if (node == 0)
4544 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004545 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4546 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004547 return left;
4548 }
4549 return node;
4550}
4551
Jamie Madillb98c3a82015-07-23 14:26:04 -04004552TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4553 TIntermTyped *left,
4554 TIntermTyped *right,
4555 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004556{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004557 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004558 if (node == 0)
4559 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004560 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4561 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004562 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004563 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004564 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4565 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004566 }
4567 return node;
4568}
4569
Olli Etuaho13389b62016-10-16 11:48:18 +01004570TIntermBinary *TParseContext::createAssign(TOperator op,
4571 TIntermTyped *left,
4572 TIntermTyped *right,
4573 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004574{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004575 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004576 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004577 if (op == EOpMulAssign)
4578 {
4579 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4580 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4581 {
4582 return nullptr;
4583 }
4584 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004585 TIntermBinary *node = new TIntermBinary(op, left, right);
4586 node->setLine(loc);
4587
Olli Etuaho3fdec912016-08-18 15:08:06 +03004588 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004589 }
4590 return nullptr;
4591}
4592
Jamie Madillb98c3a82015-07-23 14:26:04 -04004593TIntermTyped *TParseContext::addAssign(TOperator op,
4594 TIntermTyped *left,
4595 TIntermTyped *right,
4596 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004597{
Olli Etuahocce89652017-06-19 16:04:09 +03004598 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004599 TIntermTyped *node = createAssign(op, left, right, loc);
4600 if (node == nullptr)
4601 {
4602 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004603 return left;
4604 }
4605 return node;
4606}
4607
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004608TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4609 TIntermTyped *right,
4610 const TSourceLoc &loc)
4611{
Corentin Wallez0d959252016-07-12 17:26:32 -04004612 // WebGL2 section 5.26, the following results in an error:
4613 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004614 if (mShaderSpec == SH_WEBGL2_SPEC &&
4615 (left->isArray() || left->getBasicType() == EbtVoid ||
4616 left->getType().isStructureContainingArrays() || right->isArray() ||
4617 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004618 {
4619 error(loc,
4620 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4621 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004622 }
4623
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004624 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
4625 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
4626 commaNode->getTypePointer()->setQualifier(resultQualifier);
4627 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004628}
4629
Olli Etuaho49300862015-02-20 14:54:49 +02004630TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4631{
4632 switch (op)
4633 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004634 case EOpContinue:
4635 if (mLoopNestingLevel <= 0)
4636 {
4637 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004638 }
4639 break;
4640 case EOpBreak:
4641 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4642 {
4643 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004644 }
4645 break;
4646 case EOpReturn:
4647 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4648 {
4649 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004650 }
4651 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004652 case EOpKill:
4653 if (mShaderType != GL_FRAGMENT_SHADER)
4654 {
4655 error(loc, "discard supported in fragment shaders only", "discard");
4656 }
4657 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004658 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004659 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004660 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004661 }
Olli Etuahocce89652017-06-19 16:04:09 +03004662 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004663}
4664
Jamie Madillb98c3a82015-07-23 14:26:04 -04004665TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004666 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004667 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004668{
Olli Etuahocce89652017-06-19 16:04:09 +03004669 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004670 {
Olli Etuahocce89652017-06-19 16:04:09 +03004671 ASSERT(op == EOpReturn);
4672 mFunctionReturnsValue = true;
4673 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4674 {
4675 error(loc, "void function cannot return a value", "return");
4676 }
4677 else if (*mCurrentFunctionType != expression->getType())
4678 {
4679 error(loc, "function return is not matching type:", "return");
4680 }
Olli Etuaho49300862015-02-20 14:54:49 +02004681 }
Olli Etuahocce89652017-06-19 16:04:09 +03004682 TIntermBranch *node = new TIntermBranch(op, expression);
4683 node->setLine(loc);
4684 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004685}
4686
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004687void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4688{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004689 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004690 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004691 TIntermNode *offset = nullptr;
4692 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004693 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4694 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4695 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004696 {
4697 offset = arguments->back();
4698 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004699 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004700 {
4701 // A bias parameter might follow the offset parameter.
4702 ASSERT(arguments->size() >= 3);
4703 offset = (*arguments)[2];
4704 }
4705 if (offset != nullptr)
4706 {
4707 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4708 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4709 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004710 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004711 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004712 }
4713 else
4714 {
4715 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4716 size_t size = offsetConstantUnion->getType().getObjectSize();
4717 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4718 for (size_t i = 0u; i < size; ++i)
4719 {
4720 int offsetValue = values[i].getIConst();
4721 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4722 {
4723 std::stringstream tokenStream;
4724 tokenStream << offsetValue;
4725 std::string token = tokenStream.str();
4726 error(offset->getLine(), "Texture offset value out of valid range",
4727 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004728 }
4729 }
4730 }
4731 }
4732}
4733
Martin Radev2cc85b32016-08-05 16:22:53 +03004734// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4735void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4736{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004737 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004738 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4739
4740 if (name.compare(0, 5, "image") == 0)
4741 {
4742 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004743 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004744
Olli Etuaho485eefd2017-02-14 17:40:06 +00004745 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004746
4747 if (name.compare(5, 5, "Store") == 0)
4748 {
4749 if (memoryQualifier.readonly)
4750 {
4751 error(imageNode->getLine(),
4752 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004753 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004754 }
4755 }
4756 else if (name.compare(5, 4, "Load") == 0)
4757 {
4758 if (memoryQualifier.writeonly)
4759 {
4760 error(imageNode->getLine(),
4761 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004762 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004763 }
4764 }
4765 }
4766}
4767
4768// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4769void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4770 const TFunction *functionDefinition,
4771 const TIntermAggregate *functionCall)
4772{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004773 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004774
4775 const TIntermSequence &arguments = *functionCall->getSequence();
4776
4777 ASSERT(functionDefinition->getParamCount() == arguments.size());
4778
4779 for (size_t i = 0; i < arguments.size(); ++i)
4780 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004781 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4782 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004783 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4784 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4785
4786 if (IsImage(functionArgumentType.getBasicType()))
4787 {
4788 const TMemoryQualifier &functionArgumentMemoryQualifier =
4789 functionArgumentType.getMemoryQualifier();
4790 const TMemoryQualifier &functionParameterMemoryQualifier =
4791 functionParameterType.getMemoryQualifier();
4792 if (functionArgumentMemoryQualifier.readonly &&
4793 !functionParameterMemoryQualifier.readonly)
4794 {
4795 error(functionCall->getLine(),
4796 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004797 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004798 }
4799
4800 if (functionArgumentMemoryQualifier.writeonly &&
4801 !functionParameterMemoryQualifier.writeonly)
4802 {
4803 error(functionCall->getLine(),
4804 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004805 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004806 }
Martin Radev049edfa2016-11-11 14:35:37 +02004807
4808 if (functionArgumentMemoryQualifier.coherent &&
4809 !functionParameterMemoryQualifier.coherent)
4810 {
4811 error(functionCall->getLine(),
4812 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004813 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004814 }
4815
4816 if (functionArgumentMemoryQualifier.volatileQualifier &&
4817 !functionParameterMemoryQualifier.volatileQualifier)
4818 {
4819 error(functionCall->getLine(),
4820 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004821 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004822 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004823 }
4824 }
4825}
4826
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004827TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004828{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004829 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004830}
4831
4832TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004833 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004834 TIntermNode *thisNode,
4835 const TSourceLoc &loc)
4836{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004837 if (thisNode != nullptr)
4838 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004839 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004840 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004841
4842 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004843 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004844 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004845 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004846 }
4847 else
4848 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004849 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004850 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4851 }
4852}
4853
4854TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4855 TIntermSequence *arguments,
4856 TIntermNode *thisNode,
4857 const TSourceLoc &loc)
4858{
4859 TConstantUnion *unionArray = new TConstantUnion[1];
4860 int arraySize = 0;
4861 TIntermTyped *typedThis = thisNode->getAsTyped();
4862 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4863 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4864 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4865 // So accessing fnCall->getName() below is safe.
4866 if (fnCall->getName() != "length")
4867 {
4868 error(loc, "invalid method", fnCall->getName().c_str());
4869 }
4870 else if (!arguments->empty())
4871 {
4872 error(loc, "method takes no parameters", "length");
4873 }
4874 else if (typedThis == nullptr || !typedThis->isArray())
4875 {
4876 error(loc, "length can only be called on arrays", "length");
4877 }
4878 else
4879 {
4880 arraySize = typedThis->getArraySize();
4881 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004882 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004883 // This code path can be hit with expressions like these:
4884 // (a = b).length()
4885 // (func()).length()
4886 // (int[3](0, 1, 2)).length()
4887 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4888 // expression.
4889 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4890 // spec section 5.9 which allows "An array, vector or matrix expression with the
4891 // length method applied".
4892 error(loc, "length can only be called on array names, not on array expressions",
4893 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004894 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004895 }
4896 unionArray->setIConst(arraySize);
4897 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4898}
4899
4900TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4901 TIntermSequence *arguments,
4902 const TSourceLoc &loc)
4903{
4904 // First find by unmangled name to check whether the function name has been
4905 // hidden by a variable name or struct typename.
4906 // If a function is found, check for one with a matching argument list.
4907 bool builtIn;
4908 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4909 if (symbol != nullptr && !symbol->isFunction())
4910 {
4911 error(loc, "function name expected", fnCall->getName().c_str());
4912 }
4913 else
4914 {
4915 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4916 mShaderVersion, &builtIn);
4917 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004918 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004919 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4920 }
4921 else
4922 {
4923 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004924 //
4925 // A declared function.
4926 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004927 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004928 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004929 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004930 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004931 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004932 if (builtIn && op != EOpNull)
4933 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004934 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004935 if (fnCandidate->getParamCount() == 1)
4936 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004937 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004938 TIntermNode *unaryParamNode = arguments->front();
4939 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004940 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004941 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004942 }
4943 else
4944 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004945 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004946 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004947 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004948
4949 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004950 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304951
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004952 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05304953 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004954 // See if we can constant fold a built-in. Note that this may be possible
4955 // even if it is not const-qualified.
4956 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304957 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004958 else
4959 {
4960 return callNode;
4961 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004962 }
4963 }
4964 else
4965 {
4966 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004967 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004968
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004969 // If builtIn == false, the function is user defined - could be an overloaded
4970 // built-in as well.
4971 // if builtIn == true, it's a builtIn function with no op associated with it.
4972 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004973 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004974 {
Olli Etuahofe486322017-03-21 09:30:54 +00004975 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004976 checkTextureOffsetConst(callNode);
4977 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004978 }
4979 else
4980 {
Olli Etuahofe486322017-03-21 09:30:54 +00004981 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004982 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004983 }
4984
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004985 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004986
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004987 callNode->setLine(loc);
4988
4989 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004990 }
4991 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004992 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004993
4994 // Error message was already written. Put on a dummy node for error recovery.
4995 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004996}
4997
Jamie Madillb98c3a82015-07-23 14:26:04 -04004998TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004999 TIntermTyped *trueExpression,
5000 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005001 const TSourceLoc &loc)
5002{
Olli Etuaho856c4972016-08-08 11:38:39 +03005003 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03005004
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005005 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005006 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005007 std::stringstream reasonStream;
5008 reasonStream << "mismatching ternary operator operand types '"
5009 << trueExpression->getCompleteString() << " and '"
5010 << falseExpression->getCompleteString() << "'";
5011 std::string reason = reasonStream.str();
5012 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005013 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005014 }
Olli Etuahode318b22016-10-25 16:18:25 +01005015 if (IsOpaqueType(trueExpression->getBasicType()))
5016 {
5017 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005018 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005019 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5020 // Note that structs containing opaque types don't need to be checked as structs are
5021 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005022 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005023 return falseExpression;
5024 }
5025
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005026 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005027 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005028 // ESSL 3.00.6 section 5.7:
5029 // Ternary operator support is optional for arrays. No certainty that it works across all
5030 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5031 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005032 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005033 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005034 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005035 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005036 }
Olli Etuaho94050052017-05-08 14:17:44 +03005037 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5038 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005039 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005040 return falseExpression;
5041 }
5042
Corentin Wallez0d959252016-07-12 17:26:32 -04005043 // WebGL2 section 5.26, the following results in an error:
5044 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005045 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005046 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005047 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005048 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005049 }
5050
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005051 // Note that the node resulting from here can be a constant union without being qualified as
5052 // constant.
5053 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5054 node->setLine(loc);
5055
5056 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005057}
Olli Etuaho49300862015-02-20 14:54:49 +02005058
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005059//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005060// Parse an array of strings using yyparse.
5061//
5062// Returns 0 for success.
5063//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005064int PaParseStrings(size_t count,
5065 const char *const string[],
5066 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305067 TParseContext *context)
5068{
Yunchao He4f285442017-04-21 12:15:49 +08005069 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005070 return 1;
5071
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005072 if (glslang_initialize(context))
5073 return 1;
5074
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005075 int error = glslang_scan(count, string, length, context);
5076 if (!error)
5077 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005078
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005079 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005080
alokp@chromium.org6b495712012-06-29 00:06:58 +00005081 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005082}
Jamie Madill45bcc782016-11-07 13:58:48 -05005083
5084} // namespace sh