blob: 975fade4d92a42e8a9c4463e41fda85fa74e181f [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179//
180// Look at a '.' field selector string and change it into offsets
181// for a vector.
182//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400183bool TParseContext::parseVectorFields(const TString &compString,
184 int vecSize,
185 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530186 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400188 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530189 if (fields.num > 4)
190 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000191 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000192 return false;
193 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000194
Jamie Madillb98c3a82015-07-23 14:26:04 -0400195 enum
196 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000197 exyzw,
198 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000199 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000200 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201
Arun Patole7e7e68d2015-05-22 12:02:25 +0530202 for (int i = 0; i < fields.num; ++i)
203 {
204 switch (compString[i])
205 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400206 case 'x':
207 fields.offsets[i] = 0;
208 fieldSet[i] = exyzw;
209 break;
210 case 'r':
211 fields.offsets[i] = 0;
212 fieldSet[i] = ergba;
213 break;
214 case 's':
215 fields.offsets[i] = 0;
216 fieldSet[i] = estpq;
217 break;
218 case 'y':
219 fields.offsets[i] = 1;
220 fieldSet[i] = exyzw;
221 break;
222 case 'g':
223 fields.offsets[i] = 1;
224 fieldSet[i] = ergba;
225 break;
226 case 't':
227 fields.offsets[i] = 1;
228 fieldSet[i] = estpq;
229 break;
230 case 'z':
231 fields.offsets[i] = 2;
232 fieldSet[i] = exyzw;
233 break;
234 case 'b':
235 fields.offsets[i] = 2;
236 fieldSet[i] = ergba;
237 break;
238 case 'p':
239 fields.offsets[i] = 2;
240 fieldSet[i] = estpq;
241 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530242
Jamie Madillb98c3a82015-07-23 14:26:04 -0400243 case 'w':
244 fields.offsets[i] = 3;
245 fieldSet[i] = exyzw;
246 break;
247 case 'a':
248 fields.offsets[i] = 3;
249 fieldSet[i] = ergba;
250 break;
251 case 'q':
252 fields.offsets[i] = 3;
253 fieldSet[i] = estpq;
254 break;
255 default:
256 error(line, "illegal vector field selection", compString.c_str());
257 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000258 }
259 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260
Arun Patole7e7e68d2015-05-22 12:02:25 +0530261 for (int i = 0; i < fields.num; ++i)
262 {
263 if (fields.offsets[i] >= vecSize)
264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400265 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000266 return false;
267 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268
Arun Patole7e7e68d2015-05-22 12:02:25 +0530269 if (i > 0)
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530272 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 error(line, "illegal - vector component fields not from the same set",
274 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000275 return false;
276 }
277 }
278 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000280 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281}
282
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283///////////////////////////////////////////////////////////////////////
284//
285// Errors
286//
287////////////////////////////////////////////////////////////////////////
288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000289//
290// Used by flex/bison to output all syntax and parsing errors.
291//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000292void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000293{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000294 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295}
296
Olli Etuaho4de340a2016-12-16 09:32:03 +0000297void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530298{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000299 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000300}
301
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200302void TParseContext::outOfRangeError(bool isError,
303 const TSourceLoc &loc,
304 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000305 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200306{
307 if (isError)
308 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000309 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200310 }
311 else
312 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000313 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200314 }
315}
316
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000317//
318// Same error message for all places assignments don't work.
319//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530320void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000322 std::stringstream reasonStream;
323 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
324 std::string reason = reasonStream.str();
325 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326}
327
328//
329// Same error message for all places unary operations don't work.
330//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530331void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000333 std::stringstream reasonStream;
334 reasonStream << "wrong operand type - no operation '" << op
335 << "' exists that takes an operand of type " << operand
336 << " (or there is no acceptable conversion)";
337 std::string reason = reasonStream.str();
338 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339}
340
341//
342// Same error message for all binary operations don't work.
343//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400344void TParseContext::binaryOpError(const TSourceLoc &line,
345 const char *op,
346 TString left,
347 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000349 std::stringstream reasonStream;
350 reasonStream << "wrong operand types - no operation '" << op
351 << "' exists that takes a left-hand operand of type '" << left
352 << "' and a right operand of type '" << right
353 << "' (or there is no acceptable conversion)";
354 std::string reason = reasonStream.str();
355 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356}
357
Olli Etuaho856c4972016-08-08 11:38:39 +0300358void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
359 TPrecision precision,
360 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530361{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400362 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300363 return;
Martin Radev70866b82016-07-22 15:27:42 +0300364
365 if (precision != EbpUndefined && !SupportsPrecision(type))
366 {
367 error(line, "illegal type for precision qualifier", getBasicString(type));
368 }
369
Olli Etuaho183d7e22015-11-20 15:59:09 +0200370 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530371 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200372 switch (type)
373 {
374 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400375 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300376 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200377 case EbtInt:
378 case EbtUInt:
379 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400380 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300381 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200382 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800383 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200384 {
jchen10cc2a10e2017-05-03 14:05:12 +0800385 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300386 return;
387 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200388 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000389 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000390}
391
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392// Both test and if necessary, spit out an error, to see if the node is really
393// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300394bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500396 TIntermSymbol *symNode = node->getAsSymbolNode();
397 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100398 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
399
400 if (swizzleNode)
401 {
402 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
403 if (ok && swizzleNode->hasDuplicateOffsets())
404 {
405 error(line, " l-value of swizzle cannot have duplicate components", op);
406 return false;
407 }
408 return ok;
409 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410
Arun Patole7e7e68d2015-05-22 12:02:25 +0530411 if (binaryNode)
412 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400413 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530414 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400415 case EOpIndexDirect:
416 case EOpIndexIndirect:
417 case EOpIndexDirectStruct:
418 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300419 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400420 default:
421 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000422 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000423 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300424 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000425 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426
jchen10cc2a10e2017-05-03 14:05:12 +0800427 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428 switch (node->getQualifier())
429 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400430 case EvqConst:
431 message = "can't modify a const";
432 break;
433 case EvqConstReadOnly:
434 message = "can't modify a const";
435 break;
436 case EvqAttribute:
437 message = "can't modify an attribute";
438 break;
439 case EvqFragmentIn:
440 message = "can't modify an input";
441 break;
442 case EvqVertexIn:
443 message = "can't modify an input";
444 break;
445 case EvqUniform:
446 message = "can't modify a uniform";
447 break;
448 case EvqVaryingIn:
449 message = "can't modify a varying";
450 break;
451 case EvqFragCoord:
452 message = "can't modify gl_FragCoord";
453 break;
454 case EvqFrontFacing:
455 message = "can't modify gl_FrontFacing";
456 break;
457 case EvqPointCoord:
458 message = "can't modify gl_PointCoord";
459 break;
Martin Radevb0883602016-08-04 17:48:58 +0300460 case EvqNumWorkGroups:
461 message = "can't modify gl_NumWorkGroups";
462 break;
463 case EvqWorkGroupSize:
464 message = "can't modify gl_WorkGroupSize";
465 break;
466 case EvqWorkGroupID:
467 message = "can't modify gl_WorkGroupID";
468 break;
469 case EvqLocalInvocationID:
470 message = "can't modify gl_LocalInvocationID";
471 break;
472 case EvqGlobalInvocationID:
473 message = "can't modify gl_GlobalInvocationID";
474 break;
475 case EvqLocalInvocationIndex:
476 message = "can't modify gl_LocalInvocationIndex";
477 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300478 case EvqViewIDOVR:
479 message = "can't modify gl_ViewID_OVR";
480 break;
Martin Radev802abe02016-08-04 17:48:32 +0300481 case EvqComputeIn:
482 message = "can't modify work group size variable";
483 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400484 default:
485 //
486 // Type that can't be written to?
487 //
488 if (node->getBasicType() == EbtVoid)
489 {
490 message = "can't modify void";
491 }
jchen10cc2a10e2017-05-03 14:05:12 +0800492 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400493 {
jchen10cc2a10e2017-05-03 14:05:12 +0800494 message = "can't modify a variable with type ";
495 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300496 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498
jchen10cc2a10e2017-05-03 14:05:12 +0800499 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000501 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
Olli Etuaho8a176262016-08-16 14:23:01 +0300503 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 //
507 // Everything else is okay, no error.
508 //
jchen10cc2a10e2017-05-03 14:05:12 +0800509 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300510 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000511
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000512 //
513 // If we get here, we have an error and a message.
514 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530515 if (symNode)
516 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000517 const char *symbol = symNode->getSymbol().c_str();
518 std::stringstream reasonStream;
519 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
520 std::string reason = reasonStream.str();
521 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000522 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530523 else
524 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000525 std::stringstream reasonStream;
526 reasonStream << "l-value required (" << message << ")";
527 std::string reason = reasonStream.str();
528 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
Olli Etuaho8a176262016-08-16 14:23:01 +0300531 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532}
533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534// Both test, and if necessary spit out an error, to see if the node is really
535// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300536void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537{
Olli Etuaho383b7912016-08-05 11:22:59 +0300538 if (node->getQualifier() != EvqConst)
539 {
540 error(node->getLine(), "constant expression required", "");
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542}
543
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544// Both test, and if necessary spit out an error, to see if the node is really
545// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300546void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547{
Olli Etuaho383b7912016-08-05 11:22:59 +0300548 if (!node->isScalarInt())
549 {
550 error(node->getLine(), "integer expression required", token);
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552}
553
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554// Both test, and if necessary spit out an error, to see if we are currently
555// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800556bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557{
Olli Etuaho856c4972016-08-08 11:38:39 +0300558 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300559 {
560 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800561 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300562 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800563 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564}
565
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566// For now, keep it simple: if it starts "gl_", it's reserved, independent
567// of scope. Except, if the symbol table is at the built-in push-level,
568// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000569// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
570// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300571bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530573 static const char *reservedErrMsg = "reserved built-in name";
574 if (!symbolTable.atBuiltInLevel())
575 {
576 if (identifier.compare(0, 3, "gl_") == 0)
577 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000578 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300579 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500581 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 {
583 if (identifier.compare(0, 6, "webgl_") == 0)
584 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000585 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300586 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000587 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530588 if (identifier.compare(0, 7, "_webgl_") == 0)
589 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000590 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300591 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000592 }
593 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530594 if (identifier.find("__") != TString::npos)
595 {
596 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400597 "identifiers containing two consecutive underscores (__) are reserved as "
598 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530599 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300600 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 }
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
Olli Etuaho8a176262016-08-16 14:23:01 +0300604 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605}
606
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300607// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300608bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800609 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300610 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800612 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200614 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300615 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200617
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300618 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530619 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300620 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200621 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300622 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200623 {
jchen10cc2a10e2017-05-03 14:05:12 +0800624 std::string reason("cannot convert a variable with type ");
625 reason += getBasicString(argTyped->getBasicType());
626 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300627 return false;
628 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200629 if (argTyped->getBasicType() == EbtVoid)
630 {
631 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300632 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200633 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
Olli Etuaho856c4972016-08-08 11:38:39 +0300636 if (type.isArray())
637 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300638 // The size of an unsized constructor should already have been determined.
639 ASSERT(!type.isUnsizedArray());
640 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
641 {
642 error(line, "array constructor needs one argument per array element", "constructor");
643 return false;
644 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300645 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
646 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800647 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300648 {
649 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500650 if (argType.isArray())
651 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300652 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500653 return false;
654 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300655 if (!argType.sameElementType(type))
656 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000657 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300658 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300659 }
660 }
661 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300662 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300663 {
664 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300665 if (fields.size() != arguments->size())
666 {
667 error(line,
668 "Number of constructor parameters does not match the number of structure fields",
669 "constructor");
670 return false;
671 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300672
673 for (size_t i = 0; i < fields.size(); i++)
674 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800675 if (i >= arguments->size() ||
676 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300677 {
678 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000679 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300680 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300681 }
682 }
683 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300684 else
685 {
686 // We're constructing a scalar, vector, or matrix.
687
688 // Note: It's okay to have too many components available, but not okay to have unused
689 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
690 // there is an extra argument, so 'overFull' will become true.
691
692 size_t size = 0;
693 bool full = false;
694 bool overFull = false;
695 bool matrixArg = false;
696 for (TIntermNode *arg : *arguments)
697 {
698 const TIntermTyped *argTyped = arg->getAsTyped();
699 ASSERT(argTyped != nullptr);
700
Olli Etuaho487b63a2017-05-23 15:55:09 +0300701 if (argTyped->getBasicType() == EbtStruct)
702 {
703 error(line, "a struct cannot be used as a constructor argument for this type",
704 "constructor");
705 return false;
706 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 if (argTyped->getType().isArray())
708 {
709 error(line, "constructing from a non-dereferenced array", "constructor");
710 return false;
711 }
712 if (argTyped->getType().isMatrix())
713 {
714 matrixArg = true;
715 }
716
717 size += argTyped->getType().getObjectSize();
718 if (full)
719 {
720 overFull = true;
721 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300722 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300723 {
724 full = true;
725 }
726 }
727
728 if (type.isMatrix() && matrixArg)
729 {
730 if (arguments->size() != 1)
731 {
732 error(line, "constructing matrix from matrix can only take one argument",
733 "constructor");
734 return false;
735 }
736 }
737 else
738 {
739 if (size != 1 && size < type.getObjectSize())
740 {
741 error(line, "not enough data provided for construction", "constructor");
742 return false;
743 }
744 if (overFull)
745 {
746 error(line, "too many arguments", "constructor");
747 return false;
748 }
749 }
750 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300751
Olli Etuaho8a176262016-08-16 14:23:01 +0300752 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753}
754
Jamie Madillb98c3a82015-07-23 14:26:04 -0400755// This function checks to see if a void variable has been declared and raise an error message for
756// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757//
758// returns true in case of an error
759//
Olli Etuaho856c4972016-08-08 11:38:39 +0300760bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400761 const TString &identifier,
762 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300764 if (type == EbtVoid)
765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300767 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769
Olli Etuaho8a176262016-08-16 14:23:01 +0300770 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771}
772
Jamie Madillb98c3a82015-07-23 14:26:04 -0400773// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300774// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300775void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530777 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
778 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000779 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781}
782
Jamie Madillb98c3a82015-07-23 14:26:04 -0400783// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300784// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300785void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786{
Martin Radev4a9cd802016-09-01 16:51:51 +0300787 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530788 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000789 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530790 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000791}
792
jchen10cc2a10e2017-05-03 14:05:12 +0800793bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
794 const TTypeSpecifierNonArray &pType,
795 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530797 if (pType.type == EbtStruct)
798 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300799 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530800 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000801 std::stringstream reasonStream;
802 reasonStream << reason << " (structure contains a sampler)";
803 std::string reasonStr = reasonStream.str();
804 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300805 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000806 }
jchen10cc2a10e2017-05-03 14:05:12 +0800807 // only samplers need to be checked from structs, since other opaque types can't be struct
808 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300809 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530810 }
jchen10cc2a10e2017-05-03 14:05:12 +0800811 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530812 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000813 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300814 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816
Olli Etuaho8a176262016-08-16 14:23:01 +0300817 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818}
819
Olli Etuaho856c4972016-08-08 11:38:39 +0300820void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
821 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400822{
823 if (pType.layoutQualifier.location != -1)
824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400825 error(line, "location must only be specified for a single input or output variable",
826 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400827 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400828}
829
Olli Etuaho856c4972016-08-08 11:38:39 +0300830void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
831 const TLayoutQualifier &layoutQualifier)
832{
833 if (layoutQualifier.location != -1)
834 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000835 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
836 if (mShaderVersion >= 310)
837 {
838 errorMsg =
839 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
840 }
841 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300842 }
843}
844
Martin Radev2cc85b32016-08-05 16:22:53 +0300845void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
846 TQualifier qualifier,
847 const TType &type)
848{
Martin Radev2cc85b32016-08-05 16:22:53 +0300849 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800850 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530851 {
jchen10cc2a10e2017-05-03 14:05:12 +0800852 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300857unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530859 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000860
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200861 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
862 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
863 // fold as array size.
864 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000865 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000866 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300867 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000868 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869
Olli Etuaho856c4972016-08-08 11:38:39 +0300870 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400871
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000872 if (constant->getBasicType() == EbtUInt)
873 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300874 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000875 }
876 else
877 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300878 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000879
Olli Etuaho856c4972016-08-08 11:38:39 +0300880 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000881 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400882 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000884 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400885
Olli Etuaho856c4972016-08-08 11:38:39 +0300886 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 }
888
Olli Etuaho856c4972016-08-08 11:38:39 +0300889 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400890 {
891 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300892 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400893 }
894
895 // The size of arrays is restricted here to prevent issues further down the
896 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
897 // 4096 registers so this should be reasonable even for aggressively optimizable code.
898 const unsigned int sizeLimit = 65536;
899
Olli Etuaho856c4972016-08-08 11:38:39 +0300900 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400901 {
902 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300905
906 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907}
908
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300910bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
911 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912{
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 if ((elementQualifier.qualifier == EvqAttribute) ||
914 (elementQualifier.qualifier == EvqVertexIn) ||
915 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300916 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400917 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300918 TType(elementQualifier).getQualifierString());
919 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000920 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
Olli Etuaho8a176262016-08-16 14:23:01 +0300922 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923}
924
Olli Etuaho8a176262016-08-16 14:23:01 +0300925// See if this element type can be formed into an array.
926bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000928 //
929 // Can the type be an array?
930 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300931 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400932 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300933 error(line, "cannot declare arrays of arrays",
934 TType(elementType).getCompleteString().c_str());
935 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000936 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300937 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
938 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
939 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300940 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300941 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300942 {
943 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300944 TType(elementType).getCompleteString().c_str());
945 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300946 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000947
Olli Etuaho8a176262016-08-16 14:23:01 +0300948 return true;
949}
950
951// Check if this qualified element type can be formed into an array.
952bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
953 const TPublicType &elementType)
954{
955 if (checkIsValidTypeForArray(indexLocation, elementType))
956 {
957 return checkIsValidQualifierForArray(indexLocation, elementType);
958 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300963void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
964 const TString &identifier,
965 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966{
Olli Etuaho3739d232015-04-08 12:23:44 +0300967 ASSERT(type != nullptr);
968 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000969 {
970 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300971 type->qualifier = EvqTemporary;
972
973 // Generate informative error messages for ESSL1.
974 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400975 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000976 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530977 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400978 "structures containing arrays may not be declared constant since they cannot be "
979 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000981 }
982 else
983 {
984 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
985 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300986 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000987 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300988 if (type->isUnsizedArray())
989 {
990 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300991 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992}
993
Olli Etuaho2935c582015-04-08 14:32:06 +0300994// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995// and update the symbol table.
996//
Olli Etuaho2935c582015-04-08 14:32:06 +0300997// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999bool TParseContext::declareVariable(const TSourceLoc &line,
1000 const TString &identifier,
1001 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001002 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003{
Olli Etuaho2935c582015-04-08 14:32:06 +03001004 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005
Olli Etuaho43364892017-02-13 16:00:12 +00001006 checkBindingIsValid(line, type);
1007
Olli Etuaho856c4972016-08-08 11:38:39 +03001008 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
Olli Etuaho2935c582015-04-08 14:32:06 +03001010 // gl_LastFragData may be redeclared with a new precision qualifier
1011 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1012 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1014 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001015 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001016 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001017 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 }
1021 }
1022 else
1023 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1025 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001026 return false;
1027 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001028 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029
Olli Etuaho8a176262016-08-16 14:23:01 +03001030 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001031 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho2935c582015-04-08 14:32:06 +03001033 (*variable) = new TVariable(&identifier, type);
1034 if (!symbolTable.declare(*variable))
1035 {
1036 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001037 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001038 return false;
1039 }
1040
Olli Etuaho8a176262016-08-16 14:23:01 +03001041 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001042 return false;
1043
1044 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045}
1046
Martin Radev70866b82016-07-22 15:27:42 +03001047void TParseContext::checkIsParameterQualifierValid(
1048 const TSourceLoc &line,
1049 const TTypeQualifierBuilder &typeQualifierBuilder,
1050 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301051{
Olli Etuahocce89652017-06-19 16:04:09 +03001052 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001053 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001054
1055 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301056 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001057 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1058 }
1059
1060 if (!IsImage(type->getBasicType()))
1061 {
Olli Etuaho43364892017-02-13 16:00:12 +00001062 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001063 }
1064 else
1065 {
1066 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001067 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
Martin Radev70866b82016-07-22 15:27:42 +03001069 type->setQualifier(typeQualifier.qualifier);
1070
1071 if (typeQualifier.precision != EbpUndefined)
1072 {
1073 type->setPrecision(typeQualifier.precision);
1074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075}
1076
Olli Etuaho856c4972016-08-08 11:38:39 +03001077bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001078{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001079 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001080 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301081 if (iter == extBehavior.end())
1082 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001083 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001084 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001085 }
zmo@google.comf5450912011-09-09 01:37:19 +00001086 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1088 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001089 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1090 // associated with more than one extension.
1091 if (extension == "GL_OVR_multiview")
1092 {
1093 return checkCanUseExtension(line, "GL_OVR_multiview2");
1094 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001095 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001096 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001097 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301098 if (iter->second == EBhWarn)
1099 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001100 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001101 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
Olli Etuaho8a176262016-08-16 14:23:01 +03001104 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105}
1106
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001107// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1108// compile-time or link-time errors are the same whether or not the declaration is empty".
1109// This function implements all the checks that are done on qualifiers regardless of if the
1110// declaration is empty.
1111void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1112 const sh::TLayoutQualifier &layoutQualifier,
1113 const TSourceLoc &location)
1114{
1115 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1116 {
1117 error(location, "Shared memory declarations cannot have layout specified", "layout");
1118 }
1119
1120 if (layoutQualifier.matrixPacking != EmpUnspecified)
1121 {
1122 error(location, "layout qualifier only valid for interface blocks",
1123 getMatrixPackingString(layoutQualifier.matrixPacking));
1124 return;
1125 }
1126
1127 if (layoutQualifier.blockStorage != EbsUnspecified)
1128 {
1129 error(location, "layout qualifier only valid for interface blocks",
1130 getBlockStorageString(layoutQualifier.blockStorage));
1131 return;
1132 }
1133
1134 if (qualifier == EvqFragmentOut)
1135 {
1136 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1137 {
1138 error(location, "invalid layout qualifier combination", "yuv");
1139 return;
1140 }
1141 }
1142 else
1143 {
1144 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1145 }
1146
Olli Etuaho95468d12017-05-04 11:14:34 +03001147 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1148 // parsing steps. So it needs to be checked here.
1149 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1150 {
1151 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1152 }
1153
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001154 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1155 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1156 {
1157 canHaveLocation = true;
1158 // We're not checking whether the uniform location is in range here since that depends on
1159 // the type of the variable.
1160 // The type can only be fully determined for non-empty declarations.
1161 }
1162 if (!canHaveLocation)
1163 {
1164 checkLocationIsNotSpecified(location, layoutQualifier);
1165 }
1166}
1167
jchen104cdac9e2017-05-08 11:01:20 +08001168void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1169 const TSourceLoc &location)
1170{
1171 if (publicType.precision != EbpHigh)
1172 {
1173 error(location, "Can only be highp", "atomic counter");
1174 }
1175 // dEQP enforces compile error if location is specified. See uniform_location.test.
1176 if (publicType.layoutQualifier.location != -1)
1177 {
1178 error(location, "location must not be set for atomic_uint", "layout");
1179 }
1180 if (publicType.layoutQualifier.binding == -1)
1181 {
1182 error(location, "no binding specified", "atomic counter");
1183 }
1184}
1185
Martin Radevb8b01222016-11-20 23:25:53 +02001186void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1187 const TSourceLoc &location)
1188{
1189 if (publicType.isUnsizedArray())
1190 {
1191 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1192 // error. It is assumed that this applies to empty declarations as well.
1193 error(location, "empty array declaration needs to specify a size", "");
1194 }
Martin Radevb8b01222016-11-20 23:25:53 +02001195}
1196
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001197// These checks are done for all declarations that are non-empty. They're done for non-empty
1198// declarations starting a declarator list, and declarators that follow an empty declaration.
1199void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1200 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001201{
Olli Etuahofa33d582015-04-09 14:33:12 +03001202 switch (publicType.qualifier)
1203 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001204 case EvqVaryingIn:
1205 case EvqVaryingOut:
1206 case EvqAttribute:
1207 case EvqVertexIn:
1208 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001209 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001210 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001211 {
1212 error(identifierLocation, "cannot be used with a structure",
1213 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001214 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001215 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001216
Jamie Madillb98c3a82015-07-23 14:26:04 -04001217 default:
1218 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001219 }
jchen10cc2a10e2017-05-03 14:05:12 +08001220 std::string reason(getBasicString(publicType.getBasicType()));
1221 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001222 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001223 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001224 {
1225 return;
1226 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001227
Andrei Volykhina5527072017-03-22 16:46:30 +03001228 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1229 publicType.qualifier != EvqConst) &&
1230 publicType.getBasicType() == EbtYuvCscStandardEXT)
1231 {
1232 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1233 getQualifierString(publicType.qualifier));
1234 return;
1235 }
1236
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001237 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1238 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001239 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1240 // But invalid shaders may still reach here with an unsized array declaration.
1241 if (!publicType.isUnsizedArray())
1242 {
1243 TType type(publicType);
1244 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1245 publicType.layoutQualifier);
1246 }
1247 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001248
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001249 // check for layout qualifier issues
1250 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001251
Martin Radev2cc85b32016-08-05 16:22:53 +03001252 if (IsImage(publicType.getBasicType()))
1253 {
1254
1255 switch (layoutQualifier.imageInternalFormat)
1256 {
1257 case EiifRGBA32F:
1258 case EiifRGBA16F:
1259 case EiifR32F:
1260 case EiifRGBA8:
1261 case EiifRGBA8_SNORM:
1262 if (!IsFloatImage(publicType.getBasicType()))
1263 {
1264 error(identifierLocation,
1265 "internal image format requires a floating image type",
1266 getBasicString(publicType.getBasicType()));
1267 return;
1268 }
1269 break;
1270 case EiifRGBA32I:
1271 case EiifRGBA16I:
1272 case EiifRGBA8I:
1273 case EiifR32I:
1274 if (!IsIntegerImage(publicType.getBasicType()))
1275 {
1276 error(identifierLocation,
1277 "internal image format requires an integer image type",
1278 getBasicString(publicType.getBasicType()));
1279 return;
1280 }
1281 break;
1282 case EiifRGBA32UI:
1283 case EiifRGBA16UI:
1284 case EiifRGBA8UI:
1285 case EiifR32UI:
1286 if (!IsUnsignedImage(publicType.getBasicType()))
1287 {
1288 error(identifierLocation,
1289 "internal image format requires an unsigned image type",
1290 getBasicString(publicType.getBasicType()));
1291 return;
1292 }
1293 break;
1294 case EiifUnspecified:
1295 error(identifierLocation, "layout qualifier", "No image internal format specified");
1296 return;
1297 default:
1298 error(identifierLocation, "layout qualifier", "unrecognized token");
1299 return;
1300 }
1301
1302 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1303 switch (layoutQualifier.imageInternalFormat)
1304 {
1305 case EiifR32F:
1306 case EiifR32I:
1307 case EiifR32UI:
1308 break;
1309 default:
1310 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1311 {
1312 error(identifierLocation, "layout qualifier",
1313 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1314 "image variables must be qualified readonly and/or writeonly");
1315 return;
1316 }
1317 break;
1318 }
1319 }
1320 else
1321 {
Olli Etuaho43364892017-02-13 16:00:12 +00001322 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001323 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1324 }
jchen104cdac9e2017-05-08 11:01:20 +08001325
1326 if (IsAtomicCounter(publicType.getBasicType()))
1327 {
1328 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1329 }
1330 else
1331 {
1332 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1333 }
Olli Etuaho43364892017-02-13 16:00:12 +00001334}
Martin Radev2cc85b32016-08-05 16:22:53 +03001335
Olli Etuaho43364892017-02-13 16:00:12 +00001336void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1337{
1338 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1339 int arraySize = type.isArray() ? type.getArraySize() : 1;
1340 if (IsImage(type.getBasicType()))
1341 {
1342 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1343 }
1344 else if (IsSampler(type.getBasicType()))
1345 {
1346 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1347 }
jchen104cdac9e2017-05-08 11:01:20 +08001348 else if (IsAtomicCounter(type.getBasicType()))
1349 {
1350 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1351 }
Olli Etuaho43364892017-02-13 16:00:12 +00001352 else
1353 {
1354 ASSERT(!IsOpaqueType(type.getBasicType()));
1355 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001356 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001357}
1358
Olli Etuaho856c4972016-08-08 11:38:39 +03001359void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1360 const TString &layoutQualifierName,
1361 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001362{
1363
1364 if (mShaderVersion < versionRequired)
1365 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001366 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001367 }
1368}
1369
Olli Etuaho856c4972016-08-08 11:38:39 +03001370bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1371 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001372{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001373 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001374 for (size_t i = 0u; i < localSize.size(); ++i)
1375 {
1376 if (localSize[i] != -1)
1377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001378 error(location,
1379 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1380 "global layout declaration",
1381 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001382 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001383 }
1384 }
1385
Olli Etuaho8a176262016-08-16 14:23:01 +03001386 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001387}
1388
Olli Etuaho43364892017-02-13 16:00:12 +00001389void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001390 TLayoutImageInternalFormat internalFormat)
1391{
1392 if (internalFormat != EiifUnspecified)
1393 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001394 error(location, "invalid layout qualifier: only valid when used with images",
1395 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001396 }
Olli Etuaho43364892017-02-13 16:00:12 +00001397}
1398
1399void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1400{
1401 if (binding != -1)
1402 {
1403 error(location,
1404 "invalid layout qualifier: only valid when used with opaque types or blocks",
1405 "binding");
1406 }
1407}
1408
jchen104cdac9e2017-05-08 11:01:20 +08001409void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1410{
1411 if (offset != -1)
1412 {
1413 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1414 "offset");
1415 }
1416}
1417
Olli Etuaho43364892017-02-13 16:00:12 +00001418void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1419{
1420 // Expects arraySize to be 1 when setting binding for only a single variable.
1421 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1422 {
1423 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1424 }
1425}
1426
1427void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1428 int binding,
1429 int arraySize)
1430{
1431 // Expects arraySize to be 1 when setting binding for only a single variable.
1432 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1433 {
1434 error(location, "sampler binding greater than maximum texture units", "binding");
1435 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001436}
1437
jchen10af713a22017-04-19 09:10:56 +08001438void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1439{
1440 int size = (arraySize == 0 ? 1 : arraySize);
1441 if (binding + size > mMaxUniformBufferBindings)
1442 {
1443 error(location, "interface block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1444 "binding");
1445 }
1446}
jchen104cdac9e2017-05-08 11:01:20 +08001447void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1448{
1449 if (binding >= mMaxAtomicCounterBindings)
1450 {
1451 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1452 "binding");
1453 }
1454}
jchen10af713a22017-04-19 09:10:56 +08001455
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001456void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1457 int objectLocationCount,
1458 const TLayoutQualifier &layoutQualifier)
1459{
1460 int loc = layoutQualifier.location;
1461 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1462 {
1463 error(location, "Uniform location out of range", "location");
1464 }
1465}
1466
Andrei Volykhina5527072017-03-22 16:46:30 +03001467void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1468{
1469 if (yuv != false)
1470 {
1471 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1472 }
1473}
1474
Olli Etuaho383b7912016-08-05 11:22:59 +03001475void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001476 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001477{
1478 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1479 {
1480 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1481 if (qual == EvqOut || qual == EvqInOut)
1482 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001483 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001484 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001485 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001486 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001487 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001488 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001489 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001490 }
1491 }
1492 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001493}
1494
Martin Radev70866b82016-07-22 15:27:42 +03001495void TParseContext::checkInvariantVariableQualifier(bool invariant,
1496 const TQualifier qualifier,
1497 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001498{
Martin Radev70866b82016-07-22 15:27:42 +03001499 if (!invariant)
1500 return;
1501
1502 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001503 {
Martin Radev70866b82016-07-22 15:27:42 +03001504 // input variables in the fragment shader can be also qualified as invariant
1505 if (!sh::CanBeInvariantESSL1(qualifier))
1506 {
1507 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1508 }
1509 }
1510 else
1511 {
1512 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1513 {
1514 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1515 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001516 }
1517}
1518
Arun Patole7e7e68d2015-05-22 12:02:25 +05301519bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001520{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001521 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001522 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1523 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001524}
1525
Arun Patole7e7e68d2015-05-22 12:02:25 +05301526bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001527{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001528 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001529}
1530
Jamie Madillb98c3a82015-07-23 14:26:04 -04001531void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1532 const char *extName,
1533 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001534{
1535 pp::SourceLocation srcLoc;
1536 srcLoc.file = loc.first_file;
1537 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001538 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001539}
1540
Jamie Madillb98c3a82015-07-23 14:26:04 -04001541void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1542 const char *name,
1543 const char *value,
1544 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001545{
1546 pp::SourceLocation srcLoc;
1547 srcLoc.file = loc.first_file;
1548 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001549 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001550}
1551
Martin Radev4c4c8e72016-08-04 12:25:34 +03001552sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001553{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001554 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001555 for (size_t i = 0u; i < result.size(); ++i)
1556 {
1557 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1558 {
1559 result[i] = 1;
1560 }
1561 else
1562 {
1563 result[i] = mComputeShaderLocalSize[i];
1564 }
1565 }
1566 return result;
1567}
1568
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569/////////////////////////////////////////////////////////////////////////////////
1570//
1571// Non-Errors.
1572//
1573/////////////////////////////////////////////////////////////////////////////////
1574
Jamie Madill5c097022014-08-20 16:38:32 -04001575const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1576 const TString *name,
1577 const TSymbol *symbol)
1578{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001579 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001580
1581 if (!symbol)
1582 {
1583 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001584 }
1585 else if (!symbol->isVariable())
1586 {
1587 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001588 }
1589 else
1590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001591 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001592
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001593 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001594 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001595 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001596 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001597 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001598
1599 // Reject shaders using both gl_FragData and gl_FragColor
1600 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001601 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001602 {
1603 mUsesFragData = true;
1604 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001605 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001606 {
1607 mUsesFragColor = true;
1608 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001609 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1610 {
1611 mUsesSecondaryOutputs = true;
1612 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001613
1614 // This validation is not quite correct - it's only an error to write to
1615 // both FragData and FragColor. For simplicity, and because users shouldn't
1616 // be rewarded for reading from undefined varaibles, return an error
1617 // if they are both referenced, rather than assigned.
1618 if (mUsesFragData && mUsesFragColor)
1619 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001620 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1621 if (mUsesSecondaryOutputs)
1622 {
1623 errorMessage =
1624 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1625 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1626 }
1627 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001628 }
Martin Radevb0883602016-08-04 17:48:58 +03001629
1630 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1631 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1632 qualifier == EvqWorkGroupSize)
1633 {
1634 error(location,
1635 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1636 "gl_WorkGroupSize");
1637 }
Jamie Madill5c097022014-08-20 16:38:32 -04001638 }
1639
1640 if (!variable)
1641 {
1642 TType type(EbtFloat, EbpUndefined);
1643 TVariable *fakeVariable = new TVariable(name, type);
1644 symbolTable.declare(fakeVariable);
1645 variable = fakeVariable;
1646 }
1647
1648 return variable;
1649}
1650
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001651TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1652 const TString *name,
1653 const TSymbol *symbol)
1654{
1655 const TVariable *variable = getNamedVariable(location, name, symbol);
1656
Olli Etuaho09b04a22016-12-15 13:30:26 +00001657 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1658 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1659 {
1660 // WEBGL_multiview spec
1661 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1662 "gl_ViewID_OVR");
1663 }
1664
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001665 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001666 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001667 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001668 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001669 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001670 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1671 mComputeShaderLocalSizeDeclared)
1672 {
1673 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1674 // needs to be added to the AST as a constant and not as a symbol.
1675 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1676 TConstantUnion *constArray = new TConstantUnion[3];
1677 for (size_t i = 0; i < 3; ++i)
1678 {
1679 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1680 }
1681
1682 ASSERT(variable->getType().getBasicType() == EbtUInt);
1683 ASSERT(variable->getType().getObjectSize() == 3);
1684
1685 TType type(variable->getType());
1686 type.setQualifier(EvqConst);
1687 return intermediate.addConstantUnion(constArray, type, location);
1688 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001689 else
1690 {
1691 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1692 variable->getType(), location);
1693 }
1694}
1695
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001696// Initializers show up in several places in the grammar. Have one set of
1697// code to handle them here.
1698//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001699// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001700bool TParseContext::executeInitializer(const TSourceLoc &line,
1701 const TString &identifier,
1702 const TPublicType &pType,
1703 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001704 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001705{
Olli Etuaho13389b62016-10-16 11:48:18 +01001706 ASSERT(initNode != nullptr);
1707 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001708 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709
Olli Etuaho2935c582015-04-08 14:32:06 +03001710 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001711 if (type.isUnsizedArray())
1712 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001713 // We have not checked yet whether the initializer actually is an array or not.
1714 if (initializer->isArray())
1715 {
1716 type.setArraySize(initializer->getArraySize());
1717 }
1718 else
1719 {
1720 // Having a non-array initializer for an unsized array will result in an error later,
1721 // so we don't generate an error message here.
1722 type.setArraySize(1u);
1723 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001724 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001725 if (!declareVariable(line, identifier, type, &variable))
1726 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001727 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729
Olli Etuahob0c645e2015-05-12 14:25:36 +03001730 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731 if (symbolTable.atGlobalLevel() &&
1732 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001733 {
1734 // Error message does not completely match behavior with ESSL 1.00, but
1735 // we want to steer developers towards only using constant expressions.
1736 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001737 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001738 }
1739 if (globalInitWarning)
1740 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001741 warning(
1742 line,
1743 "global variable initializers should be constant expressions "
1744 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1745 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001746 }
1747
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001748 //
1749 // identifier must be of type constant, a global, or a temporary
1750 //
1751 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301752 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1753 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001754 error(line, " cannot initialize this type of qualifier ",
1755 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001756 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001757 }
1758 //
1759 // test for and propagate constant
1760 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001761
Arun Patole7e7e68d2015-05-22 12:02:25 +05301762 if (qualifier == EvqConst)
1763 {
1764 if (qualifier != initializer->getType().getQualifier())
1765 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001766 std::stringstream reasonStream;
1767 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1768 << "'";
1769 std::string reason = reasonStream.str();
1770 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001771 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001772 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001773 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301774 if (type != initializer->getType())
1775 {
1776 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001778 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001779 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001780 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001781
1782 // Save the constant folded value to the variable if possible. For example array
1783 // initializers are not folded, since that way copying the array literal to multiple places
1784 // in the shader is avoided.
1785 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1786 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301787 if (initializer->getAsConstantUnion())
1788 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001789 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001790 ASSERT(*initNode == nullptr);
1791 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301792 }
1793 else if (initializer->getAsSymbolNode())
1794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 const TSymbol *symbol =
1796 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1797 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001799 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001800 if (constArray)
1801 {
1802 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001803 ASSERT(*initNode == nullptr);
1804 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001805 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001806 }
1807 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001808
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001809 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1810 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001811 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1812 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001813 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001814 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001815 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001816 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001817
Olli Etuaho914b79a2017-06-19 16:03:19 +03001818 return true;
1819}
1820
1821TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1822 const TString &identifier,
1823 TIntermTyped *initializer,
1824 const TSourceLoc &loc)
1825{
1826 checkIsScalarBool(loc, pType);
1827 TIntermBinary *initNode = nullptr;
1828 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1829 {
1830 // The initializer is valid. The init condition needs to have a node - either the
1831 // initializer node, or a constant node in case the initialized variable is const and won't
1832 // be recorded in the AST.
1833 if (initNode == nullptr)
1834 {
1835 return initializer;
1836 }
1837 else
1838 {
1839 TIntermDeclaration *declaration = new TIntermDeclaration();
1840 declaration->appendDeclarator(initNode);
1841 return declaration;
1842 }
1843 }
1844 return nullptr;
1845}
1846
1847TIntermNode *TParseContext::addLoop(TLoopType type,
1848 TIntermNode *init,
1849 TIntermNode *cond,
1850 TIntermTyped *expr,
1851 TIntermNode *body,
1852 const TSourceLoc &line)
1853{
1854 TIntermNode *node = nullptr;
1855 TIntermTyped *typedCond = nullptr;
1856 if (cond)
1857 {
1858 typedCond = cond->getAsTyped();
1859 }
1860 if (cond == nullptr || typedCond)
1861 {
Olli Etuahocce89652017-06-19 16:04:09 +03001862 if (type == ELoopDoWhile)
1863 {
1864 checkIsScalarBool(line, typedCond);
1865 }
1866 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1867 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1868 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1869 !typedCond->isVector()));
1870
Olli Etuaho914b79a2017-06-19 16:03:19 +03001871 node = new TIntermLoop(type, init, typedCond, expr, TIntermediate::EnsureBlock(body));
1872 node->setLine(line);
1873 return node;
1874 }
1875
Olli Etuahocce89652017-06-19 16:04:09 +03001876 ASSERT(type != ELoopDoWhile);
1877
Olli Etuaho914b79a2017-06-19 16:03:19 +03001878 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1879 ASSERT(declaration);
1880 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1881 ASSERT(declarator->getLeft()->getAsSymbolNode());
1882
1883 // The condition is a declaration. In the AST representation we don't support declarations as
1884 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1885 // the loop.
1886 TIntermBlock *block = new TIntermBlock();
1887
1888 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1889 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1890 block->appendStatement(declareCondition);
1891
1892 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1893 declarator->getRight()->deepCopy());
1894 TIntermLoop *loop =
1895 new TIntermLoop(type, init, conditionInit, expr, TIntermediate::EnsureBlock(body));
1896 block->appendStatement(loop);
1897 loop->setLine(line);
1898 block->setLine(line);
1899 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900}
1901
Olli Etuahocce89652017-06-19 16:04:09 +03001902TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1903 TIntermNodePair code,
1904 const TSourceLoc &loc)
1905{
1906 checkIsScalarBool(loc, cond);
1907
1908 // For compile time constant conditions, prune the code now.
1909 if (cond->getAsConstantUnion())
1910 {
1911 if (cond->getAsConstantUnion()->getBConst(0) == true)
1912 {
1913 return TIntermediate::EnsureBlock(code.node1);
1914 }
1915 else
1916 {
1917 return TIntermediate::EnsureBlock(code.node2);
1918 }
1919 }
1920
1921 TIntermIfElse *node = new TIntermIfElse(cond, TIntermediate::EnsureBlock(code.node1),
1922 TIntermediate::EnsureBlock(code.node2));
1923 node->setLine(loc);
1924
1925 return node;
1926}
1927
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001928void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1929{
1930 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1931 typeSpecifier->getBasicType());
1932
1933 if (mShaderVersion < 300 && typeSpecifier->array)
1934 {
1935 error(typeSpecifier->getLine(), "not supported", "first-class array");
1936 typeSpecifier->clearArrayness();
1937 }
1938}
1939
Martin Radev70866b82016-07-22 15:27:42 +03001940TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301941 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001942{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001943 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001944
Martin Radev70866b82016-07-22 15:27:42 +03001945 TPublicType returnType = typeSpecifier;
1946 returnType.qualifier = typeQualifier.qualifier;
1947 returnType.invariant = typeQualifier.invariant;
1948 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001949 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001950 returnType.precision = typeSpecifier.precision;
1951
1952 if (typeQualifier.precision != EbpUndefined)
1953 {
1954 returnType.precision = typeQualifier.precision;
1955 }
1956
Martin Radev4a9cd802016-09-01 16:51:51 +03001957 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1958 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001959
Martin Radev4a9cd802016-09-01 16:51:51 +03001960 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1961 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001962
Martin Radev4a9cd802016-09-01 16:51:51 +03001963 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001964
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001965 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001966 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001967 if (typeSpecifier.array)
1968 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001969 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001970 returnType.clearArrayness();
1971 }
1972
Martin Radev70866b82016-07-22 15:27:42 +03001973 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001974 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001975 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001976 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001977 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001978 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001979
Martin Radev70866b82016-07-22 15:27:42 +03001980 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001981 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001982 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001983 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001984 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001985 }
1986 }
1987 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001988 {
Martin Radev70866b82016-07-22 15:27:42 +03001989 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001990 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001991 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001992 }
Martin Radev70866b82016-07-22 15:27:42 +03001993 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1994 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001995 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001996 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1997 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001998 }
Martin Radev70866b82016-07-22 15:27:42 +03001999 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002000 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002001 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002002 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002003 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002004 }
2005
2006 return returnType;
2007}
2008
Olli Etuaho856c4972016-08-08 11:38:39 +03002009void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2010 const TPublicType &type,
2011 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002012{
2013 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002014 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002015 {
2016 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002017 }
2018
2019 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2020 switch (qualifier)
2021 {
2022 case EvqVertexIn:
2023 // ESSL 3.00 section 4.3.4
2024 if (type.array)
2025 {
2026 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002027 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002028 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002029 return;
2030 case EvqFragmentOut:
2031 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002032 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002033 {
2034 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002035 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002036 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002037 return;
2038 default:
2039 break;
2040 }
2041
2042 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2043 // restrictions.
2044 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002045 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2046 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002047 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2048 {
2049 error(qualifierLocation, "must use 'flat' interpolation here",
2050 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002051 }
2052
Martin Radev4a9cd802016-09-01 16:51:51 +03002053 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002054 {
2055 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2056 // These restrictions are only implied by the ESSL 3.00 spec, but
2057 // the ESSL 3.10 spec lists these restrictions explicitly.
2058 if (type.array)
2059 {
2060 error(qualifierLocation, "cannot be an array of structures",
2061 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002062 }
2063 if (type.isStructureContainingArrays())
2064 {
2065 error(qualifierLocation, "cannot be a structure containing an array",
2066 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002067 }
2068 if (type.isStructureContainingType(EbtStruct))
2069 {
2070 error(qualifierLocation, "cannot be a structure containing a structure",
2071 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002072 }
2073 if (type.isStructureContainingType(EbtBool))
2074 {
2075 error(qualifierLocation, "cannot be a structure containing a bool",
2076 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002077 }
2078 }
2079}
2080
Martin Radev2cc85b32016-08-05 16:22:53 +03002081void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2082{
2083 if (qualifier.getType() == QtStorage)
2084 {
2085 const TStorageQualifierWrapper &storageQualifier =
2086 static_cast<const TStorageQualifierWrapper &>(qualifier);
2087 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2088 !symbolTable.atGlobalLevel())
2089 {
2090 error(storageQualifier.getLine(),
2091 "Local variables can only use the const storage qualifier.",
2092 storageQualifier.getQualifierString().c_str());
2093 }
2094 }
2095}
2096
Olli Etuaho43364892017-02-13 16:00:12 +00002097void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002098 const TSourceLoc &location)
2099{
2100 if (memoryQualifier.readonly)
2101 {
2102 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002103 }
2104 if (memoryQualifier.writeonly)
2105 {
2106 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002107 }
Martin Radev049edfa2016-11-11 14:35:37 +02002108 if (memoryQualifier.coherent)
2109 {
2110 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002111 }
2112 if (memoryQualifier.restrictQualifier)
2113 {
2114 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002115 }
2116 if (memoryQualifier.volatileQualifier)
2117 {
2118 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002119 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002120}
2121
jchen104cdac9e2017-05-08 11:01:20 +08002122// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2123// intermediate tree.
2124void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2125 size_t size,
2126 bool forceAppend,
2127 const TSourceLoc &loc,
2128 TType &type)
2129{
2130 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2131 int offset;
2132 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2133 {
2134 offset = bindingState.appendSpan(size);
2135 }
2136 else
2137 {
2138 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2139 }
2140 if (offset == -1)
2141 {
2142 error(loc, "Offset overlapping", "atomic counter");
2143 return;
2144 }
2145 TLayoutQualifier qualifier = type.getLayoutQualifier();
2146 qualifier.offset = offset;
2147 type.setLayoutQualifier(qualifier);
2148}
2149
Olli Etuaho13389b62016-10-16 11:48:18 +01002150TIntermDeclaration *TParseContext::parseSingleDeclaration(
2151 TPublicType &publicType,
2152 const TSourceLoc &identifierOrTypeLocation,
2153 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002154{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002155 TType type(publicType);
2156 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2157 mDirectiveHandler.pragma().stdgl.invariantAll)
2158 {
2159 TQualifier qualifier = type.getQualifier();
2160
2161 // The directive handler has already taken care of rejecting invalid uses of this pragma
2162 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2163 // affected variable declarations:
2164 //
2165 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2166 // elsewhere, in TranslatorGLSL.)
2167 //
2168 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2169 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2170 // the way this is currently implemented we have to enable this compiler option before
2171 // parsing the shader and determining the shading language version it uses. If this were
2172 // implemented as a post-pass, the workaround could be more targeted.
2173 //
2174 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2175 // the specification, but there are desktop OpenGL drivers that expect that this is the
2176 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2177 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2178 {
2179 type.setInvariant(true);
2180 }
2181 }
2182
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002183 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2184 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002185
Olli Etuahobab4c082015-04-24 16:38:49 +03002186 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002187 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002188
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002189 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002190 if (emptyDeclaration)
2191 {
Martin Radevb8b01222016-11-20 23:25:53 +02002192 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002193 // In most cases we don't need to create a symbol node for an empty declaration.
2194 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2195 if (type.getBasicType() == EbtStruct)
2196 {
2197 symbol = intermediate.addSymbol(0, "", type, identifierOrTypeLocation);
2198 }
jchen104cdac9e2017-05-08 11:01:20 +08002199 else if (IsAtomicCounter(publicType.getBasicType()))
2200 {
2201 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2202 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002203 }
2204 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002205 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002206 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002207
Olli Etuaho856c4972016-08-08 11:38:39 +03002208 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002209
jchen104cdac9e2017-05-08 11:01:20 +08002210 if (IsAtomicCounter(publicType.getBasicType()))
2211 {
2212
2213 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2214 identifierOrTypeLocation, type);
2215 }
2216
Olli Etuaho2935c582015-04-08 14:32:06 +03002217 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002218 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002219
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002220 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002221 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002222 symbol = intermediate.addSymbol(variable->getUniqueId(), identifier, type,
2223 identifierOrTypeLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002224 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002225 }
2226
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002227 TIntermDeclaration *declaration = new TIntermDeclaration();
2228 declaration->setLine(identifierOrTypeLocation);
2229 if (symbol)
2230 {
2231 declaration->appendDeclarator(symbol);
2232 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002233 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002234}
2235
Olli Etuaho13389b62016-10-16 11:48:18 +01002236TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2237 const TSourceLoc &identifierLocation,
2238 const TString &identifier,
2239 const TSourceLoc &indexLocation,
2240 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002241{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002242 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002243
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002244 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2245 identifierLocation);
2246
2247 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002250
Olli Etuaho8a176262016-08-16 14:23:01 +03002251 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002252
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002253 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002254
Olli Etuaho856c4972016-08-08 11:38:39 +03002255 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002256 // Make the type an array even if size check failed.
2257 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2258 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002259
jchen104cdac9e2017-05-08 11:01:20 +08002260 if (IsAtomicCounter(publicType.getBasicType()))
2261 {
2262 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2263 identifierLocation, arrayType);
2264 }
2265
Olli Etuaho2935c582015-04-08 14:32:06 +03002266 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002267 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002268
Olli Etuaho13389b62016-10-16 11:48:18 +01002269 TIntermDeclaration *declaration = new TIntermDeclaration();
2270 declaration->setLine(identifierLocation);
2271
Olli Etuahoe7847b02015-03-16 11:56:12 +02002272 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002273 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002274 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002275 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002276 declaration->appendDeclarator(symbol);
2277 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002278
Olli Etuaho13389b62016-10-16 11:48:18 +01002279 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002280}
2281
Olli Etuaho13389b62016-10-16 11:48:18 +01002282TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2283 const TSourceLoc &identifierLocation,
2284 const TString &identifier,
2285 const TSourceLoc &initLocation,
2286 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002287{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002288 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002289
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002290 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2291 identifierLocation);
2292
2293 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002294
Olli Etuaho13389b62016-10-16 11:48:18 +01002295 TIntermDeclaration *declaration = new TIntermDeclaration();
2296 declaration->setLine(identifierLocation);
2297
2298 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002299 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002300 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002301 if (initNode)
2302 {
2303 declaration->appendDeclarator(initNode);
2304 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002305 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002306 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002307}
2308
Olli Etuaho13389b62016-10-16 11:48:18 +01002309TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002310 TPublicType &publicType,
2311 const TSourceLoc &identifierLocation,
2312 const TString &identifier,
2313 const TSourceLoc &indexLocation,
2314 TIntermTyped *indexExpression,
2315 const TSourceLoc &initLocation,
2316 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002317{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002318 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002319
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002320 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2321 identifierLocation);
2322
2323 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002324
Olli Etuaho8a176262016-08-16 14:23:01 +03002325 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002326
2327 TPublicType arrayType(publicType);
2328
Olli Etuaho856c4972016-08-08 11:38:39 +03002329 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002330 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2331 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002332 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002333 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002334 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002335 }
2336 // Make the type an array even if size check failed.
2337 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2338 arrayType.setArraySize(size);
2339
Olli Etuaho13389b62016-10-16 11:48:18 +01002340 TIntermDeclaration *declaration = new TIntermDeclaration();
2341 declaration->setLine(identifierLocation);
2342
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002343 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002344 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002345 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002346 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002347 if (initNode)
2348 {
2349 declaration->appendDeclarator(initNode);
2350 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002351 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002352
2353 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002354}
2355
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002356TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002357 const TTypeQualifierBuilder &typeQualifierBuilder,
2358 const TSourceLoc &identifierLoc,
2359 const TString *identifier,
2360 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002361{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002362 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002363
Martin Radev70866b82016-07-22 15:27:42 +03002364 if (!typeQualifier.invariant)
2365 {
2366 error(identifierLoc, "Expected invariant", identifier->c_str());
2367 return nullptr;
2368 }
2369 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2370 {
2371 return nullptr;
2372 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002373 if (!symbol)
2374 {
2375 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002376 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002377 }
Martin Radev70866b82016-07-22 15:27:42 +03002378 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002379 {
Martin Radev70866b82016-07-22 15:27:42 +03002380 error(identifierLoc, "invariant declaration specifies qualifier",
2381 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002382 }
Martin Radev70866b82016-07-22 15:27:42 +03002383 if (typeQualifier.precision != EbpUndefined)
2384 {
2385 error(identifierLoc, "invariant declaration specifies precision",
2386 getPrecisionString(typeQualifier.precision));
2387 }
2388 if (!typeQualifier.layoutQualifier.isEmpty())
2389 {
2390 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2391 }
2392
2393 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2394 ASSERT(variable);
2395 const TType &type = variable->getType();
2396
2397 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2398 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002399 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002400
2401 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2402
2403 TIntermSymbol *intermSymbol =
2404 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2405
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002406 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002407}
2408
Olli Etuaho13389b62016-10-16 11:48:18 +01002409void TParseContext::parseDeclarator(TPublicType &publicType,
2410 const TSourceLoc &identifierLocation,
2411 const TString &identifier,
2412 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002413{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002414 // If the declaration starting this declarator list was empty (example: int,), some checks were
2415 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002416 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002417 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002418 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2419 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002420 }
2421
Olli Etuaho856c4972016-08-08 11:38:39 +03002422 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002423
Olli Etuaho856c4972016-08-08 11:38:39 +03002424 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002425
Olli Etuaho2935c582015-04-08 14:32:06 +03002426 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002427 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002428 if (IsAtomicCounter(publicType.getBasicType()))
2429 {
2430 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2431 identifierLocation, type);
2432 }
Olli Etuaho43364892017-02-13 16:00:12 +00002433 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002434
Olli Etuaho43364892017-02-13 16:00:12 +00002435 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002436 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002437 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002438 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002439 declarationOut->appendDeclarator(symbol);
2440 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002441}
2442
Olli Etuaho13389b62016-10-16 11:48:18 +01002443void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2444 const TSourceLoc &identifierLocation,
2445 const TString &identifier,
2446 const TSourceLoc &arrayLocation,
2447 TIntermTyped *indexExpression,
2448 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002449{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002450 // If the declaration starting this declarator list was empty (example: int,), some checks were
2451 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002452 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002453 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002454 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2455 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002456 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002457
Olli Etuaho856c4972016-08-08 11:38:39 +03002458 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002459
Olli Etuaho856c4972016-08-08 11:38:39 +03002460 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002461
Olli Etuaho8a176262016-08-16 14:23:01 +03002462 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002463 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002464 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002465 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002466 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002467
jchen104cdac9e2017-05-08 11:01:20 +08002468 if (IsAtomicCounter(publicType.getBasicType()))
2469 {
2470 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2471 true, identifierLocation, arrayType);
2472 }
2473
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002474 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002475 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002476
Jamie Madillb98c3a82015-07-23 14:26:04 -04002477 TIntermSymbol *symbol =
2478 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002479 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002480 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002481
Olli Etuaho13389b62016-10-16 11:48:18 +01002482 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002483 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002484}
2485
Olli Etuaho13389b62016-10-16 11:48:18 +01002486void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2487 const TSourceLoc &identifierLocation,
2488 const TString &identifier,
2489 const TSourceLoc &initLocation,
2490 TIntermTyped *initializer,
2491 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002492{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002493 // If the declaration starting this declarator list was empty (example: int,), some checks were
2494 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002495 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002496 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002497 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2498 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002499 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002500
Olli Etuaho856c4972016-08-08 11:38:39 +03002501 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002502
Olli Etuaho13389b62016-10-16 11:48:18 +01002503 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002504 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002505 {
2506 //
2507 // build the intermediate representation
2508 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002510 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002511 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002512 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002513 }
2514}
2515
Olli Etuaho13389b62016-10-16 11:48:18 +01002516void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2517 const TSourceLoc &identifierLocation,
2518 const TString &identifier,
2519 const TSourceLoc &indexLocation,
2520 TIntermTyped *indexExpression,
2521 const TSourceLoc &initLocation,
2522 TIntermTyped *initializer,
2523 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002524{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002525 // If the declaration starting this declarator list was empty (example: int,), some checks were
2526 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002527 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002528 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002529 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2530 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002531 }
2532
Olli Etuaho856c4972016-08-08 11:38:39 +03002533 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002534
Olli Etuaho8a176262016-08-16 14:23:01 +03002535 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002536
2537 TPublicType arrayType(publicType);
2538
Olli Etuaho856c4972016-08-08 11:38:39 +03002539 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002540 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2541 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002542 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002543 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002544 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002545 }
2546 // Make the type an array even if size check failed.
2547 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2548 arrayType.setArraySize(size);
2549
2550 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002551 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002552 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553 {
2554 if (initNode)
2555 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002556 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002557 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558 }
2559}
2560
jchen104cdac9e2017-05-08 11:01:20 +08002561void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2562 const TSourceLoc &location)
2563{
2564 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2565 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2566 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2567 {
2568 error(location, "Requires both binding and offset", "layout");
2569 return;
2570 }
2571 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2572}
2573
Olli Etuahocce89652017-06-19 16:04:09 +03002574void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2575 const TPublicType &type,
2576 const TSourceLoc &loc)
2577{
2578 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2579 !getFragmentPrecisionHigh())
2580 {
2581 error(loc, "precision is not supported in fragment shader", "highp");
2582 }
2583
2584 if (!CanSetDefaultPrecisionOnType(type))
2585 {
2586 error(loc, "illegal type argument for default precision qualifier",
2587 getBasicString(type.getBasicType()));
2588 return;
2589 }
2590 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2591}
2592
Martin Radev70866b82016-07-22 15:27:42 +03002593void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002594{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002595 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002596 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002597
Martin Radev70866b82016-07-22 15:27:42 +03002598 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2599 typeQualifier.line);
2600
Jamie Madillc2128ff2016-07-04 10:26:17 -04002601 // It should never be the case, but some strange parser errors can send us here.
2602 if (layoutQualifier.isEmpty())
2603 {
2604 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002605 return;
2606 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002607
Martin Radev802abe02016-08-04 17:48:32 +03002608 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002609 {
Olli Etuaho43364892017-02-13 16:00:12 +00002610 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002611 return;
2612 }
2613
Olli Etuaho43364892017-02-13 16:00:12 +00002614 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2615
2616 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002617
2618 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2619
Andrei Volykhina5527072017-03-22 16:46:30 +03002620 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2621
jchen104cdac9e2017-05-08 11:01:20 +08002622 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2623
Martin Radev802abe02016-08-04 17:48:32 +03002624 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002625 {
Martin Radev802abe02016-08-04 17:48:32 +03002626 if (mComputeShaderLocalSizeDeclared &&
2627 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2628 {
2629 error(typeQualifier.line, "Work group size does not match the previous declaration",
2630 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002631 return;
2632 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002633
Martin Radev802abe02016-08-04 17:48:32 +03002634 if (mShaderVersion < 310)
2635 {
2636 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002637 return;
2638 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002639
Martin Radev4c4c8e72016-08-04 12:25:34 +03002640 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002641 {
2642 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002643 return;
2644 }
2645
2646 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2647 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2648
2649 const TConstantUnion *maxComputeWorkGroupSizeData =
2650 maxComputeWorkGroupSize->getConstPointer();
2651
2652 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2653 {
2654 if (layoutQualifier.localSize[i] != -1)
2655 {
2656 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2657 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2658 if (mComputeShaderLocalSize[i] < 1 ||
2659 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2660 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002661 std::stringstream reasonStream;
2662 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2663 << maxComputeWorkGroupSizeValue;
2664 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002665
Olli Etuaho4de340a2016-12-16 09:32:03 +00002666 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002667 return;
2668 }
2669 }
2670 }
2671
2672 mComputeShaderLocalSizeDeclared = true;
2673 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002674 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002675 {
2676 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2677 // specification.
2678 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2679 {
2680 error(typeQualifier.line, "Number of views does not match the previous declaration",
2681 "layout");
2682 return;
2683 }
2684
2685 if (layoutQualifier.numViews == -1)
2686 {
2687 error(typeQualifier.line, "No num_views specified", "layout");
2688 return;
2689 }
2690
2691 if (layoutQualifier.numViews > mMaxNumViews)
2692 {
2693 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2694 "layout");
2695 return;
2696 }
2697
2698 mNumViews = layoutQualifier.numViews;
2699 }
Martin Radev802abe02016-08-04 17:48:32 +03002700 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002701 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002702 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002703 {
Martin Radev802abe02016-08-04 17:48:32 +03002704 return;
2705 }
2706
2707 if (typeQualifier.qualifier != EvqUniform)
2708 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002709 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2710 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002711 return;
2712 }
2713
2714 if (mShaderVersion < 300)
2715 {
2716 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2717 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002718 return;
2719 }
2720
Olli Etuaho09b04a22016-12-15 13:30:26 +00002721 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002722
2723 if (layoutQualifier.matrixPacking != EmpUnspecified)
2724 {
2725 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2726 }
2727
2728 if (layoutQualifier.blockStorage != EbsUnspecified)
2729 {
2730 mDefaultBlockStorage = layoutQualifier.blockStorage;
2731 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002732 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002733}
2734
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002735TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2736 const TFunction &function,
2737 const TSourceLoc &location,
2738 bool insertParametersToSymbolTable)
2739{
Olli Etuahofe486322017-03-21 09:30:54 +00002740 TIntermFunctionPrototype *prototype =
2741 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002742 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2743 // point to the data that already exists in the symbol table.
2744 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2745 prototype->setLine(location);
2746
2747 for (size_t i = 0; i < function.getParamCount(); i++)
2748 {
2749 const TConstParameter &param = function.getParam(i);
2750
2751 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2752 // be used for unused args).
2753 if (param.name != nullptr)
2754 {
2755 TVariable *variable = new TVariable(param.name, *param.type);
2756
2757 // Insert the parameter in the symbol table.
2758 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2759 {
2760 error(location, "redefinition", variable->getName().c_str());
2761 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2762 continue;
2763 }
2764 TIntermSymbol *symbol = intermediate.addSymbol(
2765 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2766 prototype->appendParameter(symbol);
2767 }
2768 else
2769 {
2770 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2771 }
2772 }
2773 return prototype;
2774}
2775
Olli Etuaho16c745a2017-01-16 17:02:27 +00002776TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2777 const TFunction &parsedFunction,
2778 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002779{
Olli Etuaho476197f2016-10-11 13:59:08 +01002780 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2781 // first declaration. Either way the instance in the symbol table is used to track whether the
2782 // function is declared multiple times.
2783 TFunction *function = static_cast<TFunction *>(
2784 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2785 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002786 {
2787 // ESSL 1.00.17 section 4.2.7.
2788 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2789 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002790 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002791 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002792
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002793 TIntermFunctionPrototype *prototype =
2794 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002795
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002796 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002797
2798 if (!symbolTable.atGlobalLevel())
2799 {
2800 // ESSL 3.00.4 section 4.2.4.
2801 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002802 }
2803
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002804 return prototype;
2805}
2806
Olli Etuaho336b1472016-10-05 16:37:55 +01002807TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002808 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002809 TIntermBlock *functionBody,
2810 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002811{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002812 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002813 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2814 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002815 error(location, "function does not return a value:",
2816 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002817 }
2818
Olli Etuahof51fdd22016-10-03 10:03:40 +01002819 if (functionBody == nullptr)
2820 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002821 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002822 functionBody->setLine(location);
2823 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002824 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002825 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002826 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002827
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002828 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002829 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002830}
2831
Olli Etuaho476197f2016-10-11 13:59:08 +01002832void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2833 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002834 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002835{
Olli Etuaho476197f2016-10-11 13:59:08 +01002836 ASSERT(function);
2837 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002838 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002839 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002840
2841 if (builtIn)
2842 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002843 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002844 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002845 else
Jamie Madill185fb402015-06-12 15:48:48 -04002846 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002847 TFunction *prevDec = static_cast<TFunction *>(
2848 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2849
2850 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2851 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2852 // occurance.
2853 if (*function != prevDec)
2854 {
2855 // Swap the parameters of the previous declaration to the parameters of the function
2856 // definition (parameter names may differ).
2857 prevDec->swapParameters(**function);
2858
2859 // The function definition will share the same symbol as any previous declaration.
2860 *function = prevDec;
2861 }
2862
2863 if ((*function)->isDefined())
2864 {
2865 error(location, "function already has a body", (*function)->getName().c_str());
2866 }
2867
2868 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002869 }
Jamie Madill185fb402015-06-12 15:48:48 -04002870
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002871 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002872 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002873 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002874
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002875 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002876 setLoopNestingLevel(0);
2877}
2878
Jamie Madillb98c3a82015-07-23 14:26:04 -04002879TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002880{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002881 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002882 // We don't know at this point whether this is a function definition or a prototype.
2883 // The definition production code will check for redefinitions.
2884 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002885 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002886 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2887 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002888 //
2889 TFunction *prevDec =
2890 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302891
Martin Radevda6254b2016-12-14 17:00:36 +02002892 if (getShaderVersion() >= 300 &&
2893 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2894 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302895 {
Martin Radevda6254b2016-12-14 17:00:36 +02002896 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302897 // Therefore overloading or redefining builtin functions is an error.
2898 error(location, "Name of a built-in function cannot be redeclared as function",
2899 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302900 }
2901 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002902 {
2903 if (prevDec->getReturnType() != function->getReturnType())
2904 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002905 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002906 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002907 }
2908 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002910 if (prevDec->getParam(i).type->getQualifier() !=
2911 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002912 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002913 error(location,
2914 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002915 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002916 }
2917 }
2918 }
2919
2920 //
2921 // Check for previously declared variables using the same name.
2922 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002923 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002924 if (prevSym)
2925 {
2926 if (!prevSym->isFunction())
2927 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002928 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002929 }
2930 }
2931 else
2932 {
2933 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002934 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002935 }
2936
2937 // We're at the inner scope level of the function's arguments and body statement.
2938 // Add the function prototype to the surrounding scope instead.
2939 symbolTable.getOuterLevel()->insert(function);
2940
Olli Etuaho78d13742017-01-18 13:06:10 +00002941 // Raise error message if main function takes any parameters or return anything other than void
2942 if (function->getName() == "main")
2943 {
2944 if (function->getParamCount() > 0)
2945 {
2946 error(location, "function cannot take any parameter(s)", "main");
2947 }
2948 if (function->getReturnType().getBasicType() != EbtVoid)
2949 {
2950 error(location, "main function cannot return a value",
2951 function->getReturnType().getBasicString());
2952 }
2953 }
2954
Jamie Madill185fb402015-06-12 15:48:48 -04002955 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002956 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2957 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002958 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2959 //
2960 return function;
2961}
2962
Olli Etuaho9de84a52016-06-14 17:36:01 +03002963TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2964 const TString *name,
2965 const TSourceLoc &location)
2966{
2967 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2968 {
2969 error(location, "no qualifiers allowed for function return",
2970 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002971 }
2972 if (!type.layoutQualifier.isEmpty())
2973 {
2974 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002975 }
jchen10cc2a10e2017-05-03 14:05:12 +08002976 // make sure an opaque type is not involved as well...
2977 std::string reason(getBasicString(type.getBasicType()));
2978 reason += "s can't be function return values";
2979 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002980 if (mShaderVersion < 300)
2981 {
2982 // Array return values are forbidden, but there's also no valid syntax for declaring array
2983 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002984 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002985
2986 if (type.isStructureContainingArrays())
2987 {
2988 // ESSL 1.00.17 section 6.1 Function Definitions
2989 error(location, "structures containing arrays can't be function return values",
2990 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002991 }
2992 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002993
2994 // Add the function as a prototype after parsing it (we do not support recursion)
2995 return new TFunction(name, new TType(type));
2996}
2997
Olli Etuahocce89652017-06-19 16:04:09 +03002998TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
2999{
3000 checkIsNotReserved(loc, *name);
3001 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
3002 return new TFunction(name, returnType);
3003}
3004
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003005TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003006{
Olli Etuahocce89652017-06-19 16:04:09 +03003007 if (mShaderVersion < 300 && publicType.array)
3008 {
3009 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3010 "[]");
3011 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003012 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003013 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003014 error(publicType.getLine(), "constructor can't be a structure definition",
3015 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003016 }
3017
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003018 TType *type = new TType(publicType);
3019 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003020 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003021 error(publicType.getLine(), "cannot construct this type",
3022 getBasicString(publicType.getBasicType()));
3023 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003024 }
3025
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003026 return new TFunction(nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003027}
3028
Olli Etuahocce89652017-06-19 16:04:09 +03003029TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3030 const TString *name,
3031 const TSourceLoc &nameLoc)
3032{
3033 if (publicType.getBasicType() == EbtVoid)
3034 {
3035 error(nameLoc, "illegal use of type 'void'", name->c_str());
3036 }
3037 checkIsNotReserved(nameLoc, *name);
3038 TType *type = new TType(publicType);
3039 TParameter param = {name, type};
3040 return param;
3041}
3042
3043TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3044 const TSourceLoc &identifierLoc,
3045 TIntermTyped *arraySize,
3046 const TSourceLoc &arrayLoc,
3047 TPublicType *type)
3048{
3049 checkIsValidTypeForArray(arrayLoc, *type);
3050 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3051 type->setArraySize(size);
3052 return parseParameterDeclarator(*type, identifier, identifierLoc);
3053}
3054
Jamie Madillb98c3a82015-07-23 14:26:04 -04003055// This function is used to test for the correctness of the parameters passed to various constructor
3056// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003057//
Olli Etuaho856c4972016-08-08 11:38:39 +03003058// 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 +00003059//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003060TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003061 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303062 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063{
Olli Etuaho856c4972016-08-08 11:38:39 +03003064 if (type.isUnsizedArray())
3065 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003066 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003067 {
3068 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3069 type.setArraySize(1u);
3070 return TIntermTyped::CreateZero(type);
3071 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003072 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003073 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003074
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003075 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003076 {
Olli Etuaho72d10202017-01-19 15:58:30 +00003077 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003078 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003079
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003080 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003081 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003083 TIntermTyped *constConstructor =
3084 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003085 if (constConstructor)
3086 {
3087 return constConstructor;
3088 }
3089
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003090 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003091}
3092
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003093//
3094// Interface/uniform blocks
3095//
Olli Etuaho13389b62016-10-16 11:48:18 +01003096TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003097 const TTypeQualifierBuilder &typeQualifierBuilder,
3098 const TSourceLoc &nameLine,
3099 const TString &blockName,
3100 TFieldList *fieldList,
3101 const TString *instanceName,
3102 const TSourceLoc &instanceLine,
3103 TIntermTyped *arrayIndex,
3104 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003105{
Olli Etuaho856c4972016-08-08 11:38:39 +03003106 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003107
Olli Etuaho77ba4082016-12-16 12:01:18 +00003108 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003109
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003110 if (typeQualifier.qualifier != EvqUniform)
3111 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003112 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
3113 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003114 }
3115
Martin Radev70866b82016-07-22 15:27:42 +03003116 if (typeQualifier.invariant)
3117 {
3118 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3119 }
3120
Olli Etuaho43364892017-02-13 16:00:12 +00003121 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3122
jchen10af713a22017-04-19 09:10:56 +08003123 // add array index
3124 unsigned int arraySize = 0;
3125 if (arrayIndex != nullptr)
3126 {
3127 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3128 }
3129
3130 if (mShaderVersion < 310)
3131 {
3132 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3133 }
3134 else
3135 {
3136 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.layoutQualifier.binding,
3137 arraySize);
3138 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003139
Andrei Volykhina5527072017-03-22 16:46:30 +03003140 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3141
Jamie Madill099c0f32013-06-20 11:55:52 -04003142 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003143 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003144
Jamie Madill099c0f32013-06-20 11:55:52 -04003145 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3146 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003147 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003148 }
3149
Jamie Madill1566ef72013-06-20 11:55:54 -04003150 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3151 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003152 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04003153 }
3154
Olli Etuaho856c4972016-08-08 11:38:39 +03003155 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003156
Martin Radev2cc85b32016-08-05 16:22:53 +03003157 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3158
Arun Patole7e7e68d2015-05-22 12:02:25 +05303159 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
3160 if (!symbolTable.declare(blockNameSymbol))
3161 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003162 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003163 }
3164
Jamie Madill98493dd2013-07-08 14:39:03 -04003165 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303166 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3167 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003168 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303169 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003170 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303171 {
jchen10cc2a10e2017-05-03 14:05:12 +08003172 std::string reason("unsupported type - ");
3173 reason += fieldType->getBasicString();
3174 reason += " types are not allowed in interface blocks";
3175 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003176 }
3177
Jamie Madill98493dd2013-07-08 14:39:03 -04003178 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003179 switch (qualifier)
3180 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003181 case EvqGlobal:
3182 case EvqUniform:
3183 break;
3184 default:
3185 error(field->line(), "invalid qualifier on interface block member",
3186 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003187 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003188 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003189
Martin Radev70866b82016-07-22 15:27:42 +03003190 if (fieldType->isInvariant())
3191 {
3192 error(field->line(), "invalid qualifier on interface block member", "invariant");
3193 }
3194
Jamie Madilla5efff92013-06-06 11:56:47 -04003195 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003196 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003197 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003198 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003199
Jamie Madill98493dd2013-07-08 14:39:03 -04003200 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003201 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003202 error(field->line(), "invalid layout qualifier: cannot be used here",
3203 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003204 }
3205
Jamie Madill98493dd2013-07-08 14:39:03 -04003206 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003207 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003208 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003209 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003210 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003211 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003212 warning(field->line(),
3213 "extraneous layout qualifier: only has an effect on matrix types",
3214 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003215 }
3216
Jamie Madill98493dd2013-07-08 14:39:03 -04003217 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003218 }
3219
Jamie Madillb98c3a82015-07-23 14:26:04 -04003220 TInterfaceBlock *interfaceBlock =
3221 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
3222 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3223 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003224
3225 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003226 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003227
Jamie Madill98493dd2013-07-08 14:39:03 -04003228 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003229 {
3230 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3232 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003233 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303234 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003235
3236 // set parent pointer of the field variable
3237 fieldType->setInterfaceBlock(interfaceBlock);
3238
Arun Patole7e7e68d2015-05-22 12:02:25 +05303239 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003240 fieldVariable->setQualifier(typeQualifier.qualifier);
3241
Arun Patole7e7e68d2015-05-22 12:02:25 +05303242 if (!symbolTable.declare(fieldVariable))
3243 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003244 error(field->line(), "redefinition of an interface block member name",
3245 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003246 }
3247 }
3248 }
3249 else
3250 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003251 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003252
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003253 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003255 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003256
Arun Patole7e7e68d2015-05-22 12:02:25 +05303257 if (!symbolTable.declare(instanceTypeDef))
3258 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003259 error(instanceLine, "redefinition of an interface block instance name",
3260 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003261 }
3262
Jamie Madillb98c3a82015-07-23 14:26:04 -04003263 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003264 symbolName = instanceTypeDef->getName();
3265 }
3266
Olli Etuaho13389b62016-10-16 11:48:18 +01003267 TIntermSymbol *blockSymbol =
3268 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
3269 TIntermDeclaration *declaration = new TIntermDeclaration();
3270 declaration->appendDeclarator(blockSymbol);
3271 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003272
3273 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003274 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003275}
3276
Olli Etuaho383b7912016-08-05 11:22:59 +03003277void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003278{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003279 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003280
3281 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003282 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303283 if (mStructNestingLevel > 1)
3284 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003285 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003286 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003287}
3288
3289void TParseContext::exitStructDeclaration()
3290{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003291 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003292}
3293
Olli Etuaho8a176262016-08-16 14:23:01 +03003294void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003295{
Jamie Madillacb4b812016-11-07 13:50:29 -05003296 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303297 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003298 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003299 }
3300
Arun Patole7e7e68d2015-05-22 12:02:25 +05303301 if (field.type()->getBasicType() != EbtStruct)
3302 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003303 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003304 }
3305
3306 // We're already inside a structure definition at this point, so add
3307 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303308 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3309 {
Jamie Madill41a49272014-03-18 16:10:13 -04003310 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3312 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003313 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003314 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003315 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003316 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003317}
3318
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003319//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003320// Parse an array index expression
3321//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003322TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3323 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303324 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003325{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003326 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3327 {
3328 if (baseExpression->getAsSymbolNode())
3329 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303330 error(location, " left of '[' is not of type array, matrix, or vector ",
3331 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003332 }
3333 else
3334 {
3335 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3336 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003337
3338 TConstantUnion *unionArray = new TConstantUnion[1];
3339 unionArray->setFConst(0.0f);
3340 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3341 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003342 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003343
Jamie Madill21c1e452014-12-29 11:33:41 -05003344 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3345
Olli Etuaho36b05142015-11-12 13:10:42 +02003346 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3347 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3348 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3349 // index is a constant expression.
3350 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3351 {
3352 if (baseExpression->isInterfaceBlock())
3353 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003354 error(location,
3355 "array indexes for interface blocks arrays must be constant integral expressions",
3356 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003357 }
3358 else if (baseExpression->getQualifier() == EvqFragmentOut)
3359 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003360 error(location,
3361 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003362 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003363 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3364 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003365 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003366 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003367 }
3368
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003369 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003370 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003371 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3372 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3373 // constant fold expressions that are not constant expressions). The most compatible way to
3374 // handle this case is to report a warning instead of an error and force the index to be in
3375 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003376 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003377 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003378
3379 int safeIndex = -1;
3380
3381 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003382 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003383 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003384 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003385 if (mShaderSpec == SH_WEBGL2_SPEC)
3386 {
3387 // Error has been already generated if index is not const.
3388 if (indexExpression->getQualifier() == EvqConst)
3389 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003390 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003391 }
3392 safeIndex = 0;
3393 }
3394 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3395 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003396 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003397 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003398 "GL_EXT_draw_buffers is disabled",
3399 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003400 safeIndex = 0;
3401 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003402 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003403 // Only do generic out-of-range check if similar error hasn't already been reported.
3404 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003405 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003406 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3407 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003408 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003409 }
3410 }
3411 else if (baseExpression->isMatrix())
3412 {
3413 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003414 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003415 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003416 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003417 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003418 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003419 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3420 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003421 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003422 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003423
3424 ASSERT(safeIndex >= 0);
3425 // Data of constant unions can't be changed, because it may be shared with other
3426 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3427 // sanitized object.
3428 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003429 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003430 TConstantUnion *safeConstantUnion = new TConstantUnion();
3431 safeConstantUnion->setIConst(safeIndex);
3432 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003433 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003434
3435 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003436 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003437 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003438 else
3439 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003440 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003441 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003442 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003443}
3444
Olli Etuaho90892fb2016-07-14 14:44:51 +03003445int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3446 const TSourceLoc &location,
3447 int index,
3448 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003449 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003450{
3451 if (index >= arraySize || index < 0)
3452 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003453 std::stringstream reasonStream;
3454 reasonStream << reason << " '" << index << "'";
3455 std::string token = reasonStream.str();
3456 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003457 if (index < 0)
3458 {
3459 return 0;
3460 }
3461 else
3462 {
3463 return arraySize - 1;
3464 }
3465 }
3466 return index;
3467}
3468
Jamie Madillb98c3a82015-07-23 14:26:04 -04003469TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3470 const TSourceLoc &dotLocation,
3471 const TString &fieldString,
3472 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003473{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003474 if (baseExpression->isArray())
3475 {
3476 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003477 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003478 }
3479
3480 if (baseExpression->isVector())
3481 {
3482 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003483 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3484 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003485 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003486 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003487 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003488 }
3489
Olli Etuahob6fa0432016-09-28 16:28:05 +01003490 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003491 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003492 else if (baseExpression->getBasicType() == EbtStruct)
3493 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303494 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003495 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003496 {
3497 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003498 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003499 }
3500 else
3501 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003502 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003503 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003504 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003505 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003506 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003507 {
3508 fieldFound = true;
3509 break;
3510 }
3511 }
3512 if (fieldFound)
3513 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003514 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3515 index->setLine(fieldLocation);
3516 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003517 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003518 }
3519 else
3520 {
3521 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003522 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003523 }
3524 }
3525 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003526 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003527 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303528 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003529 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003530 {
3531 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003532 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003533 }
3534 else
3535 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003536 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003537 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003538 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003539 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003540 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003541 {
3542 fieldFound = true;
3543 break;
3544 }
3545 }
3546 if (fieldFound)
3547 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003548 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3549 index->setLine(fieldLocation);
3550 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003551 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003552 }
3553 else
3554 {
3555 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003556 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003557 }
3558 }
3559 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003560 else
3561 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003562 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003563 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003564 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303565 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003566 }
3567 else
3568 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303569 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003570 " field selection requires structure, vector, or interface block on left hand "
3571 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303572 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003573 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003574 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003575 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003576}
3577
Jamie Madillb98c3a82015-07-23 14:26:04 -04003578TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3579 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003580{
Martin Radev802abe02016-08-04 17:48:32 +03003581 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003582
3583 if (qualifierType == "shared")
3584 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003585 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003586 {
3587 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3588 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003589 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003590 }
3591 else if (qualifierType == "packed")
3592 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003593 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003594 {
3595 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3596 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003597 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003598 }
3599 else if (qualifierType == "std140")
3600 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003601 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003602 }
3603 else if (qualifierType == "row_major")
3604 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003605 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003606 }
3607 else if (qualifierType == "column_major")
3608 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003609 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003610 }
3611 else if (qualifierType == "location")
3612 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003613 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3614 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003615 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003616 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3617 mShaderType == GL_FRAGMENT_SHADER)
3618 {
3619 qualifier.yuv = true;
3620 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003621 else if (qualifierType == "rgba32f")
3622 {
3623 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3624 qualifier.imageInternalFormat = EiifRGBA32F;
3625 }
3626 else if (qualifierType == "rgba16f")
3627 {
3628 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3629 qualifier.imageInternalFormat = EiifRGBA16F;
3630 }
3631 else if (qualifierType == "r32f")
3632 {
3633 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3634 qualifier.imageInternalFormat = EiifR32F;
3635 }
3636 else if (qualifierType == "rgba8")
3637 {
3638 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3639 qualifier.imageInternalFormat = EiifRGBA8;
3640 }
3641 else if (qualifierType == "rgba8_snorm")
3642 {
3643 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3644 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3645 }
3646 else if (qualifierType == "rgba32i")
3647 {
3648 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3649 qualifier.imageInternalFormat = EiifRGBA32I;
3650 }
3651 else if (qualifierType == "rgba16i")
3652 {
3653 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3654 qualifier.imageInternalFormat = EiifRGBA16I;
3655 }
3656 else if (qualifierType == "rgba8i")
3657 {
3658 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3659 qualifier.imageInternalFormat = EiifRGBA8I;
3660 }
3661 else if (qualifierType == "r32i")
3662 {
3663 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3664 qualifier.imageInternalFormat = EiifR32I;
3665 }
3666 else if (qualifierType == "rgba32ui")
3667 {
3668 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3669 qualifier.imageInternalFormat = EiifRGBA32UI;
3670 }
3671 else if (qualifierType == "rgba16ui")
3672 {
3673 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3674 qualifier.imageInternalFormat = EiifRGBA16UI;
3675 }
3676 else if (qualifierType == "rgba8ui")
3677 {
3678 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3679 qualifier.imageInternalFormat = EiifRGBA8UI;
3680 }
3681 else if (qualifierType == "r32ui")
3682 {
3683 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3684 qualifier.imageInternalFormat = EiifR32UI;
3685 }
3686
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003687 else
3688 {
3689 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003690 }
3691
Jamie Madilla5efff92013-06-06 11:56:47 -04003692 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003693}
3694
Martin Radev802abe02016-08-04 17:48:32 +03003695void TParseContext::parseLocalSize(const TString &qualifierType,
3696 const TSourceLoc &qualifierTypeLine,
3697 int intValue,
3698 const TSourceLoc &intValueLine,
3699 const std::string &intValueString,
3700 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003701 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003702{
Olli Etuaho856c4972016-08-08 11:38:39 +03003703 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003704 if (intValue < 1)
3705 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003706 std::stringstream reasonStream;
3707 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3708 std::string reason = reasonStream.str();
3709 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003710 }
3711 (*localSize)[index] = intValue;
3712}
3713
Olli Etuaho09b04a22016-12-15 13:30:26 +00003714void TParseContext::parseNumViews(int intValue,
3715 const TSourceLoc &intValueLine,
3716 const std::string &intValueString,
3717 int *numViews)
3718{
3719 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3720 // specification.
3721 if (intValue < 1)
3722 {
3723 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3724 }
3725 *numViews = intValue;
3726}
3727
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3729 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303731 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003732{
Martin Radev802abe02016-08-04 17:48:32 +03003733 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003734
Martin Radev802abe02016-08-04 17:48:32 +03003735 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003736
Martin Radev802abe02016-08-04 17:48:32 +03003737 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003738 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003739 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003740 if (intValue < 0)
3741 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003742 error(intValueLine, "out of range: location must be non-negative",
3743 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003744 }
3745 else
3746 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003747 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003748 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003749 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003750 }
Olli Etuaho43364892017-02-13 16:00:12 +00003751 else if (qualifierType == "binding")
3752 {
3753 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3754 if (intValue < 0)
3755 {
3756 error(intValueLine, "out of range: binding must be non-negative",
3757 intValueString.c_str());
3758 }
3759 else
3760 {
3761 qualifier.binding = intValue;
3762 }
3763 }
jchen104cdac9e2017-05-08 11:01:20 +08003764 else if (qualifierType == "offset")
3765 {
3766 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3767 if (intValue < 0)
3768 {
3769 error(intValueLine, "out of range: offset must be non-negative",
3770 intValueString.c_str());
3771 }
3772 else
3773 {
3774 qualifier.offset = intValue;
3775 }
3776 }
Martin Radev802abe02016-08-04 17:48:32 +03003777 else if (qualifierType == "local_size_x")
3778 {
3779 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3780 &qualifier.localSize);
3781 }
3782 else if (qualifierType == "local_size_y")
3783 {
3784 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3785 &qualifier.localSize);
3786 }
3787 else if (qualifierType == "local_size_z")
3788 {
3789 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3790 &qualifier.localSize);
3791 }
Olli Etuaho95468d12017-05-04 11:14:34 +03003792 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00003793 mShaderType == GL_VERTEX_SHADER)
3794 {
3795 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3796 }
Martin Radev802abe02016-08-04 17:48:32 +03003797 else
3798 {
3799 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003800 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003801
Jamie Madilla5efff92013-06-06 11:56:47 -04003802 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003803}
3804
Olli Etuaho613b9592016-09-05 12:05:53 +03003805TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3806{
3807 return new TTypeQualifierBuilder(
3808 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3809 mShaderVersion);
3810}
3811
Olli Etuahocce89652017-06-19 16:04:09 +03003812TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
3813 const TSourceLoc &loc)
3814{
3815 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
3816 return new TStorageQualifierWrapper(qualifier, loc);
3817}
3818
3819TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
3820{
3821 if (getShaderType() == GL_VERTEX_SHADER)
3822 {
3823 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
3824 }
3825 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
3826}
3827
3828TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
3829{
3830 if (declaringFunction())
3831 {
3832 return new TStorageQualifierWrapper(EvqIn, loc);
3833 }
3834 if (getShaderType() == GL_FRAGMENT_SHADER)
3835 {
3836 if (mShaderVersion < 300)
3837 {
3838 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3839 }
3840 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
3841 }
3842 if (getShaderType() == GL_VERTEX_SHADER)
3843 {
3844 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
3845 {
3846 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
3847 }
3848 return new TStorageQualifierWrapper(EvqVertexIn, loc);
3849 }
3850 return new TStorageQualifierWrapper(EvqComputeIn, loc);
3851}
3852
3853TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
3854{
3855 if (declaringFunction())
3856 {
3857 return new TStorageQualifierWrapper(EvqOut, loc);
3858 }
3859 if (mShaderVersion < 300)
3860 {
3861 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
3862 }
3863 if (getShaderType() != GL_VERTEX_SHADER && getShaderType() != GL_FRAGMENT_SHADER)
3864 {
3865 error(loc, "storage qualifier supported in vertex and fragment shaders only", "out");
3866 }
3867 if (getShaderType() == GL_VERTEX_SHADER)
3868 {
3869 return new TStorageQualifierWrapper(EvqVertexOut, loc);
3870 }
3871 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
3872}
3873
3874TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
3875{
3876 if (!declaringFunction())
3877 {
3878 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
3879 }
3880 return new TStorageQualifierWrapper(EvqInOut, loc);
3881}
3882
Jamie Madillb98c3a82015-07-23 14:26:04 -04003883TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003884 TLayoutQualifier rightQualifier,
3885 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003886{
Martin Radevc28888b2016-07-22 15:27:42 +03003887 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003888 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003889}
3890
Olli Etuahocce89652017-06-19 16:04:09 +03003891TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
3892{
3893 checkIsNotReserved(loc, *identifier);
3894 TType *type = new TType(EbtVoid, EbpUndefined);
3895 return new TField(type, identifier, loc);
3896}
3897
3898TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
3899 const TSourceLoc &loc,
3900 TIntermTyped *arraySize,
3901 const TSourceLoc &arraySizeLoc)
3902{
3903 checkIsNotReserved(loc, *identifier);
3904
3905 TType *type = new TType(EbtVoid, EbpUndefined);
3906 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
3907 type->setArraySize(size);
3908
3909 return new TField(type, identifier, loc);
3910}
3911
Olli Etuaho4de340a2016-12-16 09:32:03 +00003912TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3913 const TFieldList *newlyAddedFields,
3914 const TSourceLoc &location)
3915{
3916 for (TField *field : *newlyAddedFields)
3917 {
3918 for (TField *oldField : *processedFields)
3919 {
3920 if (oldField->name() == field->name())
3921 {
3922 error(location, "duplicate field name in structure", field->name().c_str());
3923 }
3924 }
3925 processedFields->push_back(field);
3926 }
3927 return processedFields;
3928}
3929
Martin Radev70866b82016-07-22 15:27:42 +03003930TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3931 const TTypeQualifierBuilder &typeQualifierBuilder,
3932 TPublicType *typeSpecifier,
3933 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003934{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003935 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003936
Martin Radev70866b82016-07-22 15:27:42 +03003937 typeSpecifier->qualifier = typeQualifier.qualifier;
3938 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003939 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003940 typeSpecifier->invariant = typeQualifier.invariant;
3941 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303942 {
Martin Radev70866b82016-07-22 15:27:42 +03003943 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003944 }
Martin Radev70866b82016-07-22 15:27:42 +03003945 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003946}
3947
Jamie Madillb98c3a82015-07-23 14:26:04 -04003948TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3949 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003950{
Martin Radev4a9cd802016-09-01 16:51:51 +03003951 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3952 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003953
Martin Radev4a9cd802016-09-01 16:51:51 +03003954 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003955
Martin Radev4a9cd802016-09-01 16:51:51 +03003956 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003957
Arun Patole7e7e68d2015-05-22 12:02:25 +05303958 for (unsigned int i = 0; i < fieldList->size(); ++i)
3959 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003960 //
3961 // Careful not to replace already known aspects of type, like array-ness
3962 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303963 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003964 type->setBasicType(typeSpecifier.getBasicType());
3965 type->setPrimarySize(typeSpecifier.getPrimarySize());
3966 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003967 type->setPrecision(typeSpecifier.precision);
3968 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003969 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003970 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003971 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003972
3973 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303974 if (type->isArray())
3975 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003976 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003977 }
3978 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003979 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003980 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303981 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003982 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003983 }
3984
Martin Radev4a9cd802016-09-01 16:51:51 +03003985 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003986 }
3987
Jamie Madill98493dd2013-07-08 14:39:03 -04003988 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003989}
3990
Martin Radev4a9cd802016-09-01 16:51:51 +03003991TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3992 const TSourceLoc &nameLine,
3993 const TString *structName,
3994 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003995{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303996 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003998
Jamie Madill9b820842015-02-12 10:40:10 -05003999 // Store a bool in the struct if we're at global scope, to allow us to
4000 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004001 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004002
Jamie Madill98493dd2013-07-08 14:39:03 -04004003 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004004 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004005 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05304006 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
4007 if (!symbolTable.declare(userTypeDef))
4008 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004009 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004010 }
4011 }
4012
4013 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004014 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004015 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004016 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004017 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004018 switch (qualifier)
4019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004020 case EvqGlobal:
4021 case EvqTemporary:
4022 break;
4023 default:
4024 error(field.line(), "invalid qualifier on struct member",
4025 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004026 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004027 }
Martin Radev70866b82016-07-22 15:27:42 +03004028 if (field.type()->isInvariant())
4029 {
4030 error(field.line(), "invalid qualifier on struct member", "invariant");
4031 }
jchen104cdac9e2017-05-08 11:01:20 +08004032 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4033 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004034 {
4035 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4036 }
4037
Olli Etuaho43364892017-02-13 16:00:12 +00004038 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4039
4040 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004041
4042 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004043 }
4044
Martin Radev4a9cd802016-09-01 16:51:51 +03004045 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuahocce89652017-06-19 16:04:09 +03004046 typeSpecifierNonArray.initializeStruct(structureType, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004047 exitStructDeclaration();
4048
Martin Radev4a9cd802016-09-01 16:51:51 +03004049 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004050}
4051
Jamie Madillb98c3a82015-07-23 14:26:04 -04004052TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004053 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004054 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004055{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004056 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004057 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004058 init->isVector())
4059 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004060 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4061 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004062 return nullptr;
4063 }
4064
Olli Etuahoac5274d2015-02-20 10:19:08 +02004065 if (statementList)
4066 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004067 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004068 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004069 return nullptr;
4070 }
4071 }
4072
Olli Etuahoa3a36662015-02-17 13:46:51 +02004073 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
4074 if (node == nullptr)
4075 {
4076 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004077 return nullptr;
4078 }
4079 return node;
4080}
4081
4082TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4083{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004084 if (mSwitchNestingLevel == 0)
4085 {
4086 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004087 return nullptr;
4088 }
4089 if (condition == nullptr)
4090 {
4091 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004092 return nullptr;
4093 }
4094 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004095 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004096 {
4097 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004098 }
4099 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004100 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4101 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4102 // fold in case labels.
4103 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004104 {
4105 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004106 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02004107 TIntermCase *node = intermediate.addCase(condition, loc);
4108 if (node == nullptr)
4109 {
4110 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004111 return nullptr;
4112 }
4113 return node;
4114}
4115
4116TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4117{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004118 if (mSwitchNestingLevel == 0)
4119 {
4120 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004121 return nullptr;
4122 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02004123 TIntermCase *node = intermediate.addCase(nullptr, loc);
4124 if (node == nullptr)
4125 {
4126 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02004127 return nullptr;
4128 }
4129 return node;
4130}
4131
Jamie Madillb98c3a82015-07-23 14:26:04 -04004132TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4133 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004134 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004135{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004136 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004137
4138 switch (op)
4139 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004140 case EOpLogicalNot:
4141 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4142 child->isVector())
4143 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004144 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004145 return nullptr;
4146 }
4147 break;
4148 case EOpBitwiseNot:
4149 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4150 child->isMatrix() || child->isArray())
4151 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004152 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004153 return nullptr;
4154 }
4155 break;
4156 case EOpPostIncrement:
4157 case EOpPreIncrement:
4158 case EOpPostDecrement:
4159 case EOpPreDecrement:
4160 case EOpNegative:
4161 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004162 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4163 child->getBasicType() == EbtBool || child->isArray() ||
4164 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004165 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004166 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004167 return nullptr;
4168 }
4169 // Operators for built-ins are already type checked against their prototype.
4170 default:
4171 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004172 }
4173
Olli Etuahof119a262016-08-19 15:54:22 +03004174 TIntermUnary *node = new TIntermUnary(op, child);
4175 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004176
Olli Etuaho77ba4082016-12-16 12:01:18 +00004177 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03004178 if (foldedNode)
4179 return foldedNode;
4180
4181 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004182}
4183
Olli Etuaho09b22472015-02-11 11:47:26 +02004184TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4185{
Olli Etuahocce89652017-06-19 16:04:09 +03004186 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004187 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004188 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004189 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004190 return child;
4191 }
4192 return node;
4193}
4194
Jamie Madillb98c3a82015-07-23 14:26:04 -04004195TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4196 TIntermTyped *child,
4197 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004198{
Olli Etuaho856c4972016-08-08 11:38:39 +03004199 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004200 return addUnaryMath(op, child, loc);
4201}
4202
Jamie Madillb98c3a82015-07-23 14:26:04 -04004203bool TParseContext::binaryOpCommonCheck(TOperator op,
4204 TIntermTyped *left,
4205 TIntermTyped *right,
4206 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004207{
jchen10b4cf5652017-05-05 18:51:17 +08004208 // Check opaque types are not allowed to be operands in expressions other than array indexing
4209 // and structure member selection.
4210 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4211 {
4212 switch (op)
4213 {
4214 case EOpIndexDirect:
4215 case EOpIndexIndirect:
4216 break;
4217 case EOpIndexDirectStruct:
4218 UNREACHABLE();
4219
4220 default:
4221 error(loc, "Invalid operation for variables with an opaque type",
4222 GetOperatorString(op));
4223 return false;
4224 }
4225 }
jchen10cc2a10e2017-05-03 14:05:12 +08004226
Olli Etuaho244be012016-08-18 15:26:02 +03004227 if (left->getType().getStruct() || right->getType().getStruct())
4228 {
4229 switch (op)
4230 {
4231 case EOpIndexDirectStruct:
4232 ASSERT(left->getType().getStruct());
4233 break;
4234 case EOpEqual:
4235 case EOpNotEqual:
4236 case EOpAssign:
4237 case EOpInitialize:
4238 if (left->getType() != right->getType())
4239 {
4240 return false;
4241 }
4242 break;
4243 default:
4244 error(loc, "Invalid operation for structs", GetOperatorString(op));
4245 return false;
4246 }
4247 }
4248
Olli Etuaho94050052017-05-08 14:17:44 +03004249 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4250 {
4251 switch (op)
4252 {
4253 case EOpIndexDirectInterfaceBlock:
4254 ASSERT(left->getType().getInterfaceBlock());
4255 break;
4256 default:
4257 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4258 return false;
4259 }
4260 }
4261
Olli Etuahod6b14282015-03-17 14:31:35 +02004262 if (left->isArray() || right->isArray())
4263 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004264 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004265 {
4266 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4267 return false;
4268 }
4269
4270 if (left->isArray() != right->isArray())
4271 {
4272 error(loc, "array / non-array mismatch", GetOperatorString(op));
4273 return false;
4274 }
4275
4276 switch (op)
4277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004278 case EOpEqual:
4279 case EOpNotEqual:
4280 case EOpAssign:
4281 case EOpInitialize:
4282 break;
4283 default:
4284 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4285 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004286 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004287 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004288 if (left->getArraySize() != right->getArraySize())
4289 {
4290 error(loc, "array size mismatch", GetOperatorString(op));
4291 return false;
4292 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004293 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004294
4295 // Check ops which require integer / ivec parameters
4296 bool isBitShift = false;
4297 switch (op)
4298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004299 case EOpBitShiftLeft:
4300 case EOpBitShiftRight:
4301 case EOpBitShiftLeftAssign:
4302 case EOpBitShiftRightAssign:
4303 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4304 // check that the basic type is an integer type.
4305 isBitShift = true;
4306 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4307 {
4308 return false;
4309 }
4310 break;
4311 case EOpBitwiseAnd:
4312 case EOpBitwiseXor:
4313 case EOpBitwiseOr:
4314 case EOpBitwiseAndAssign:
4315 case EOpBitwiseXorAssign:
4316 case EOpBitwiseOrAssign:
4317 // It is enough to check the type of only one operand, since later it
4318 // is checked that the operand types match.
4319 if (!IsInteger(left->getBasicType()))
4320 {
4321 return false;
4322 }
4323 break;
4324 default:
4325 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004326 }
4327
4328 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4329 // So the basic type should usually match.
4330 if (!isBitShift && left->getBasicType() != right->getBasicType())
4331 {
4332 return false;
4333 }
4334
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004335 // Check that:
4336 // 1. Type sizes match exactly on ops that require that.
4337 // 2. Restrictions for structs that contain arrays or samplers are respected.
4338 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004339 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004340 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004341 case EOpAssign:
4342 case EOpInitialize:
4343 case EOpEqual:
4344 case EOpNotEqual:
4345 // ESSL 1.00 sections 5.7, 5.8, 5.9
4346 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4347 {
4348 error(loc, "undefined operation for structs containing arrays",
4349 GetOperatorString(op));
4350 return false;
4351 }
4352 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4353 // we interpret the spec so that this extends to structs containing samplers,
4354 // similarly to ESSL 1.00 spec.
4355 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4356 left->getType().isStructureContainingSamplers())
4357 {
4358 error(loc, "undefined operation for structs containing samplers",
4359 GetOperatorString(op));
4360 return false;
4361 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004362
Olli Etuahoe1805592017-01-02 16:41:20 +00004363 if ((left->getNominalSize() != right->getNominalSize()) ||
4364 (left->getSecondarySize() != right->getSecondarySize()))
4365 {
4366 error(loc, "dimension mismatch", GetOperatorString(op));
4367 return false;
4368 }
4369 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004370 case EOpLessThan:
4371 case EOpGreaterThan:
4372 case EOpLessThanEqual:
4373 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004374 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004375 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004376 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004377 return false;
4378 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004379 break;
4380 case EOpAdd:
4381 case EOpSub:
4382 case EOpDiv:
4383 case EOpIMod:
4384 case EOpBitShiftLeft:
4385 case EOpBitShiftRight:
4386 case EOpBitwiseAnd:
4387 case EOpBitwiseXor:
4388 case EOpBitwiseOr:
4389 case EOpAddAssign:
4390 case EOpSubAssign:
4391 case EOpDivAssign:
4392 case EOpIModAssign:
4393 case EOpBitShiftLeftAssign:
4394 case EOpBitShiftRightAssign:
4395 case EOpBitwiseAndAssign:
4396 case EOpBitwiseXorAssign:
4397 case EOpBitwiseOrAssign:
4398 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4399 {
4400 return false;
4401 }
4402
4403 // Are the sizes compatible?
4404 if (left->getNominalSize() != right->getNominalSize() ||
4405 left->getSecondarySize() != right->getSecondarySize())
4406 {
4407 // If the nominal sizes of operands do not match:
4408 // One of them must be a scalar.
4409 if (!left->isScalar() && !right->isScalar())
4410 return false;
4411
4412 // In the case of compound assignment other than multiply-assign,
4413 // the right side needs to be a scalar. Otherwise a vector/matrix
4414 // would be assigned to a scalar. A scalar can't be shifted by a
4415 // vector either.
4416 if (!right->isScalar() &&
4417 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4418 return false;
4419 }
4420 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004421 default:
4422 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004423 }
4424
Olli Etuahod6b14282015-03-17 14:31:35 +02004425 return true;
4426}
4427
Olli Etuaho1dded802016-08-18 18:13:13 +03004428bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4429 const TType &left,
4430 const TType &right)
4431{
4432 switch (op)
4433 {
4434 case EOpMul:
4435 case EOpMulAssign:
4436 return left.getNominalSize() == right.getNominalSize() &&
4437 left.getSecondarySize() == right.getSecondarySize();
4438 case EOpVectorTimesScalar:
4439 return true;
4440 case EOpVectorTimesScalarAssign:
4441 ASSERT(!left.isMatrix() && !right.isMatrix());
4442 return left.isVector() && !right.isVector();
4443 case EOpVectorTimesMatrix:
4444 return left.getNominalSize() == right.getRows();
4445 case EOpVectorTimesMatrixAssign:
4446 ASSERT(!left.isMatrix() && right.isMatrix());
4447 return left.isVector() && left.getNominalSize() == right.getRows() &&
4448 left.getNominalSize() == right.getCols();
4449 case EOpMatrixTimesVector:
4450 return left.getCols() == right.getNominalSize();
4451 case EOpMatrixTimesScalar:
4452 return true;
4453 case EOpMatrixTimesScalarAssign:
4454 ASSERT(left.isMatrix() && !right.isMatrix());
4455 return !right.isVector();
4456 case EOpMatrixTimesMatrix:
4457 return left.getCols() == right.getRows();
4458 case EOpMatrixTimesMatrixAssign:
4459 ASSERT(left.isMatrix() && right.isMatrix());
4460 // We need to check two things:
4461 // 1. The matrix multiplication step is valid.
4462 // 2. The result will have the same number of columns as the lvalue.
4463 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4464
4465 default:
4466 UNREACHABLE();
4467 return false;
4468 }
4469}
4470
Jamie Madillb98c3a82015-07-23 14:26:04 -04004471TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4472 TIntermTyped *left,
4473 TIntermTyped *right,
4474 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004475{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004476 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004477 return nullptr;
4478
Olli Etuahofc1806e2015-03-17 13:03:11 +02004479 switch (op)
4480 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004481 case EOpEqual:
4482 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004483 case EOpLessThan:
4484 case EOpGreaterThan:
4485 case EOpLessThanEqual:
4486 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004487 break;
4488 case EOpLogicalOr:
4489 case EOpLogicalXor:
4490 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004491 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4492 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004493 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004494 {
4495 return nullptr;
4496 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004497 // Basic types matching should have been already checked.
4498 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004499 break;
4500 case EOpAdd:
4501 case EOpSub:
4502 case EOpDiv:
4503 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004504 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4505 !right->getType().getStruct());
4506 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004507 {
4508 return nullptr;
4509 }
4510 break;
4511 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004512 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4513 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004514 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004515 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004516 {
4517 return nullptr;
4518 }
4519 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004520 default:
4521 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004522 }
4523
Olli Etuaho1dded802016-08-18 18:13:13 +03004524 if (op == EOpMul)
4525 {
4526 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4527 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4528 {
4529 return nullptr;
4530 }
4531 }
4532
Olli Etuaho3fdec912016-08-18 15:08:06 +03004533 TIntermBinary *node = new TIntermBinary(op, left, right);
4534 node->setLine(loc);
4535
Olli Etuaho3fdec912016-08-18 15:08:06 +03004536 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004537 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004538 if (foldedNode)
4539 return foldedNode;
4540
4541 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004542}
4543
Jamie Madillb98c3a82015-07-23 14:26:04 -04004544TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4545 TIntermTyped *left,
4546 TIntermTyped *right,
4547 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004548{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004549 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004550 if (node == 0)
4551 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004552 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4553 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004554 return left;
4555 }
4556 return node;
4557}
4558
Jamie Madillb98c3a82015-07-23 14:26:04 -04004559TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4560 TIntermTyped *left,
4561 TIntermTyped *right,
4562 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004563{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004564 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004565 if (node == 0)
4566 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004567 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4568 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004569 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004570 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004571 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4572 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004573 }
4574 return node;
4575}
4576
Olli Etuaho13389b62016-10-16 11:48:18 +01004577TIntermBinary *TParseContext::createAssign(TOperator op,
4578 TIntermTyped *left,
4579 TIntermTyped *right,
4580 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004581{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004582 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004583 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004584 if (op == EOpMulAssign)
4585 {
4586 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4587 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4588 {
4589 return nullptr;
4590 }
4591 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004592 TIntermBinary *node = new TIntermBinary(op, left, right);
4593 node->setLine(loc);
4594
Olli Etuaho3fdec912016-08-18 15:08:06 +03004595 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004596 }
4597 return nullptr;
4598}
4599
Jamie Madillb98c3a82015-07-23 14:26:04 -04004600TIntermTyped *TParseContext::addAssign(TOperator op,
4601 TIntermTyped *left,
4602 TIntermTyped *right,
4603 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004604{
Olli Etuahocce89652017-06-19 16:04:09 +03004605 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02004606 TIntermTyped *node = createAssign(op, left, right, loc);
4607 if (node == nullptr)
4608 {
4609 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004610 return left;
4611 }
4612 return node;
4613}
4614
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004615TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4616 TIntermTyped *right,
4617 const TSourceLoc &loc)
4618{
Corentin Wallez0d959252016-07-12 17:26:32 -04004619 // WebGL2 section 5.26, the following results in an error:
4620 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004621 if (mShaderSpec == SH_WEBGL2_SPEC &&
4622 (left->isArray() || left->getBasicType() == EbtVoid ||
4623 left->getType().isStructureContainingArrays() || right->isArray() ||
4624 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004625 {
4626 error(loc,
4627 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4628 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004629 }
4630
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004631 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004632}
4633
Olli Etuaho49300862015-02-20 14:54:49 +02004634TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4635{
4636 switch (op)
4637 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004638 case EOpContinue:
4639 if (mLoopNestingLevel <= 0)
4640 {
4641 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004642 }
4643 break;
4644 case EOpBreak:
4645 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4646 {
4647 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004648 }
4649 break;
4650 case EOpReturn:
4651 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4652 {
4653 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004654 }
4655 break;
Olli Etuahocce89652017-06-19 16:04:09 +03004656 case EOpKill:
4657 if (mShaderType != GL_FRAGMENT_SHADER)
4658 {
4659 error(loc, "discard supported in fragment shaders only", "discard");
4660 }
4661 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004662 default:
Olli Etuahocce89652017-06-19 16:04:09 +03004663 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004664 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004665 }
Olli Etuahocce89652017-06-19 16:04:09 +03004666 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02004667}
4668
Jamie Madillb98c3a82015-07-23 14:26:04 -04004669TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03004670 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004671 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004672{
Olli Etuahocce89652017-06-19 16:04:09 +03004673 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02004674 {
Olli Etuahocce89652017-06-19 16:04:09 +03004675 ASSERT(op == EOpReturn);
4676 mFunctionReturnsValue = true;
4677 if (mCurrentFunctionType->getBasicType() == EbtVoid)
4678 {
4679 error(loc, "void function cannot return a value", "return");
4680 }
4681 else if (*mCurrentFunctionType != expression->getType())
4682 {
4683 error(loc, "function return is not matching type:", "return");
4684 }
Olli Etuaho49300862015-02-20 14:54:49 +02004685 }
Olli Etuahocce89652017-06-19 16:04:09 +03004686 TIntermBranch *node = new TIntermBranch(op, expression);
4687 node->setLine(loc);
4688 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02004689}
4690
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004691void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4692{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004693 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004694 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004695 TIntermNode *offset = nullptr;
4696 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004697 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4698 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4699 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004700 {
4701 offset = arguments->back();
4702 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004703 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004704 {
4705 // A bias parameter might follow the offset parameter.
4706 ASSERT(arguments->size() >= 3);
4707 offset = (*arguments)[2];
4708 }
4709 if (offset != nullptr)
4710 {
4711 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4712 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4713 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004714 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004715 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004716 }
4717 else
4718 {
4719 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4720 size_t size = offsetConstantUnion->getType().getObjectSize();
4721 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4722 for (size_t i = 0u; i < size; ++i)
4723 {
4724 int offsetValue = values[i].getIConst();
4725 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4726 {
4727 std::stringstream tokenStream;
4728 tokenStream << offsetValue;
4729 std::string token = tokenStream.str();
4730 error(offset->getLine(), "Texture offset value out of valid range",
4731 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004732 }
4733 }
4734 }
4735 }
4736}
4737
Martin Radev2cc85b32016-08-05 16:22:53 +03004738// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4739void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4740{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004741 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004742 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4743
4744 if (name.compare(0, 5, "image") == 0)
4745 {
4746 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004747 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004748
Olli Etuaho485eefd2017-02-14 17:40:06 +00004749 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004750
4751 if (name.compare(5, 5, "Store") == 0)
4752 {
4753 if (memoryQualifier.readonly)
4754 {
4755 error(imageNode->getLine(),
4756 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004757 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004758 }
4759 }
4760 else if (name.compare(5, 4, "Load") == 0)
4761 {
4762 if (memoryQualifier.writeonly)
4763 {
4764 error(imageNode->getLine(),
4765 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004766 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004767 }
4768 }
4769 }
4770}
4771
4772// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4773void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4774 const TFunction *functionDefinition,
4775 const TIntermAggregate *functionCall)
4776{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004777 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004778
4779 const TIntermSequence &arguments = *functionCall->getSequence();
4780
4781 ASSERT(functionDefinition->getParamCount() == arguments.size());
4782
4783 for (size_t i = 0; i < arguments.size(); ++i)
4784 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004785 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4786 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004787 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4788 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4789
4790 if (IsImage(functionArgumentType.getBasicType()))
4791 {
4792 const TMemoryQualifier &functionArgumentMemoryQualifier =
4793 functionArgumentType.getMemoryQualifier();
4794 const TMemoryQualifier &functionParameterMemoryQualifier =
4795 functionParameterType.getMemoryQualifier();
4796 if (functionArgumentMemoryQualifier.readonly &&
4797 !functionParameterMemoryQualifier.readonly)
4798 {
4799 error(functionCall->getLine(),
4800 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004801 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004802 }
4803
4804 if (functionArgumentMemoryQualifier.writeonly &&
4805 !functionParameterMemoryQualifier.writeonly)
4806 {
4807 error(functionCall->getLine(),
4808 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004809 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004810 }
Martin Radev049edfa2016-11-11 14:35:37 +02004811
4812 if (functionArgumentMemoryQualifier.coherent &&
4813 !functionParameterMemoryQualifier.coherent)
4814 {
4815 error(functionCall->getLine(),
4816 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004817 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004818 }
4819
4820 if (functionArgumentMemoryQualifier.volatileQualifier &&
4821 !functionParameterMemoryQualifier.volatileQualifier)
4822 {
4823 error(functionCall->getLine(),
4824 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004825 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004826 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004827 }
4828 }
4829}
4830
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004831TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004832{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004833 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004834}
4835
4836TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004837 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004838 TIntermNode *thisNode,
4839 const TSourceLoc &loc)
4840{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004841 if (thisNode != nullptr)
4842 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004843 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004844 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004845
4846 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004847 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004848 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004849 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004850 }
4851 else
4852 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03004853 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004854 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4855 }
4856}
4857
4858TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4859 TIntermSequence *arguments,
4860 TIntermNode *thisNode,
4861 const TSourceLoc &loc)
4862{
4863 TConstantUnion *unionArray = new TConstantUnion[1];
4864 int arraySize = 0;
4865 TIntermTyped *typedThis = thisNode->getAsTyped();
4866 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4867 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4868 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4869 // So accessing fnCall->getName() below is safe.
4870 if (fnCall->getName() != "length")
4871 {
4872 error(loc, "invalid method", fnCall->getName().c_str());
4873 }
4874 else if (!arguments->empty())
4875 {
4876 error(loc, "method takes no parameters", "length");
4877 }
4878 else if (typedThis == nullptr || !typedThis->isArray())
4879 {
4880 error(loc, "length can only be called on arrays", "length");
4881 }
4882 else
4883 {
4884 arraySize = typedThis->getArraySize();
4885 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004886 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004887 // This code path can be hit with expressions like these:
4888 // (a = b).length()
4889 // (func()).length()
4890 // (int[3](0, 1, 2)).length()
4891 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4892 // expression.
4893 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4894 // spec section 5.9 which allows "An array, vector or matrix expression with the
4895 // length method applied".
4896 error(loc, "length can only be called on array names, not on array expressions",
4897 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004898 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004899 }
4900 unionArray->setIConst(arraySize);
4901 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4902}
4903
4904TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4905 TIntermSequence *arguments,
4906 const TSourceLoc &loc)
4907{
4908 // First find by unmangled name to check whether the function name has been
4909 // hidden by a variable name or struct typename.
4910 // If a function is found, check for one with a matching argument list.
4911 bool builtIn;
4912 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4913 if (symbol != nullptr && !symbol->isFunction())
4914 {
4915 error(loc, "function name expected", fnCall->getName().c_str());
4916 }
4917 else
4918 {
4919 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4920 mShaderVersion, &builtIn);
4921 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004922 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004923 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4924 }
4925 else
4926 {
4927 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004928 //
4929 // A declared function.
4930 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004931 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004932 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004933 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004934 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004935 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004936 if (builtIn && op != EOpNull)
4937 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004938 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004939 if (fnCandidate->getParamCount() == 1)
4940 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004941 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004942 TIntermNode *unaryParamNode = arguments->front();
4943 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004944 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004945 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004946 }
4947 else
4948 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004949 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004950 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004951 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004952
4953 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004954 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304955
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004956 // See if we can constant fold a built-in. Note that this may be possible even
4957 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004958 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004959 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304960 if (foldedNode)
4961 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004962 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304963 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004964 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004965 }
4966 }
4967 else
4968 {
4969 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004970 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004971
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004972 // If builtIn == false, the function is user defined - could be an overloaded
4973 // built-in as well.
4974 // if builtIn == true, it's a builtIn function with no op associated with it.
4975 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004976 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004977 {
Olli Etuahofe486322017-03-21 09:30:54 +00004978 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004979 checkTextureOffsetConst(callNode);
4980 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004981 }
4982 else
4983 {
Olli Etuahofe486322017-03-21 09:30:54 +00004984 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004985 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004986 }
4987
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004988 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004989
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004990 callNode->setLine(loc);
4991
4992 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004993 }
4994 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004995 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004996
4997 // Error message was already written. Put on a dummy node for error recovery.
4998 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004999}
5000
Jamie Madillb98c3a82015-07-23 14:26:04 -04005001TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005002 TIntermTyped *trueExpression,
5003 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005004 const TSourceLoc &loc)
5005{
Olli Etuaho856c4972016-08-08 11:38:39 +03005006 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03005007
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005008 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005009 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005010 binaryOpError(loc, "?:", trueExpression->getCompleteString(),
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005011 falseExpression->getCompleteString());
5012 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005013 }
Olli Etuahode318b22016-10-25 16:18:25 +01005014 if (IsOpaqueType(trueExpression->getBasicType()))
5015 {
5016 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005017 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005018 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5019 // Note that structs containing opaque types don't need to be checked as structs are
5020 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005021 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005022 return falseExpression;
5023 }
5024
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005025 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005026 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005027 // ESSL 3.00.6 section 5.7:
5028 // Ternary operator support is optional for arrays. No certainty that it works across all
5029 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5030 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005031 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005032 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005033 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005034 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005035 }
Olli Etuaho94050052017-05-08 14:17:44 +03005036 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5037 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005038 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005039 return falseExpression;
5040 }
5041
Corentin Wallez0d959252016-07-12 17:26:32 -04005042 // WebGL2 section 5.26, the following results in an error:
5043 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005044 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005045 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005046 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005047 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005048 }
5049
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005050 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03005051}
Olli Etuaho49300862015-02-20 14:54:49 +02005052
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005053//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005054// Parse an array of strings using yyparse.
5055//
5056// Returns 0 for success.
5057//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005058int PaParseStrings(size_t count,
5059 const char *const string[],
5060 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305061 TParseContext *context)
5062{
Yunchao He4f285442017-04-21 12:15:49 +08005063 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005064 return 1;
5065
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005066 if (glslang_initialize(context))
5067 return 1;
5068
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005069 int error = glslang_scan(count, string, length, context);
5070 if (!error)
5071 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005072
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005073 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005074
alokp@chromium.org6b495712012-06-29 00:06:58 +00005075 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005076}
Jamie Madill45bcc782016-11-07 13:58:48 -05005077
5078} // namespace sh